Can you save R code files with .RData objects for version control? - r

There is no version control at my work (we have an outdated, centralized system with sensitive patient information, so we can't save things outside of it). When I save a .RData file from a script, I would like to be able to save the exact version of that .R file with it at that time. Is there a way to do this?
E.g. if I have an R script "run_analysis.R" that has the line
save(data,file='foo.RData')
Is there a way I can do something like
save(data,run_analysis.R,file='foo.RData')
so that if I pull up the data file a year later I'll know exactly what code was used to create it?

you could zip the foo.RData file together with the run_analysis.R file and store the zipped file.
the CRAN package [zip] (https://cran.r-project.org/web/packages/zip/zip.pdf) can be used to create the zip file from within r.

Related

Writing new data to an existing excel file that has an XML map attached, without losing the XML data in R

I am trying to write to an excel file that needs to be uploaded somewhere. The target software creates an excel file which has an XML map attached to it. I recreated the entire file structure in R using code, but any time I try to write to that excel file, i think R actually deletes the old file and creates a new one instead, because the XML map is gone the moment I start writing any data to it. Loading up the workbook also doesn't seem to bring in the xml map, only the workbook data and sheets.
Is there a way to write data to this existing file within R (or python) without losing the XML map? Now i need to generate a file and manually copy paste the data into the other excel file.
I've been trying with xlsx, readxl, xml2 packages.
In the past Ive deal with a similar problem. To my knowledge, almost all the R packages that interact with excel replace the entire file with a new one. Except the openxlsx package. You can replace specific sheets, and range of cells, whitout touching the rest (data, styling , etc..). One last comment is that I dont know much about XLM maps, but maybe you are lucky.
Here is the vignette:
https://cran.r-project.org/web/packages/openxlsx/vignettes/Introduction.html
Hope it helps

Output of saved .R files in r

I want to save my code in R. I did:
save(Data,file="Code_Data.R")
When I open the file in R again, the code looks like hieroglyphics.
How can I save the code in a way, that I can read the code in an editor or RStudio again?
save outputs a binary copy of the objects you tell it to save, not R code. Because you are naming this file with a ".R" extension, RStudio is blindly trying to open this binary file as R code, and you are seeing the results of that mess.
Technically, the R language doesn't care what the extension of the file is. As long as you know that the file contains, you can load it back in with the command load("Code_Data.R"). However, if you want to get RStudio to recognize that this is actually a file containing binary data and not R code, try saving the file with the canonical ".RData" extension:
save(Data, file="Code_Data.RData")
Using the ".RData" extension will also help you and other programmers who look at your code avoid this confusion in the future.

How to download and unzip a raster zipped file directly in r from github

I am trying to download 'Landsat.rar` file (included 6 Landsat bands) and unzip it directly in r, but It doesn't work as I expected. Thank you for your help!
library(raster)
ls_url<-"https://github.com/tuyenhavan/Landsat-Data/blob/LS7/Landsat.rar"
temp<-tempfile()
download.file(ls_url,temp)
unzip(temp,"tif$")
myls<-stack("tif$")
Especially if you are using Windows, it might be that you need to use the binary mode in download.file:
download.file(ls_url, temp, mode="wb")
otherwise the file gets corrupted.
Also, the URL you are using is incorrect. You used the one for the web interface. If you want to get the file itself you need to use (check the link associated with the "Download" button):
https://github.com/tuyenhavan/Landsat-Data/raw/LS7/Landsat.rar
Finally, unzip() doesn't know how to deal with rar archive files. If you created this archive yourself, use the zip format instead; or unrar the file with another program (that you could call from R using system()).

Opening an .R files EDIT: gzip files saved with .R extension. Is there any other way to access them?

I have a number of R files with an .R extension. I've tried various ways to see what is inside these file, including Xcode, vim, etc.
What I find is utterly indecipherable. For example, it looks like this Lçæ§o‡dµ’Ò6ÇìùëfiFŒÀ±y2Â8á∫˝É, but pages of it.
Is it safe to say that these files are fundamentally corrupt? Or should I be using R to open these files to see what's actually in them?
EDIT: I've never worked with a file like this. After using load() in R, how would I read the data? I have used
> data <- load("~/filename.RData")
> data
The output is [1] "filename".
EDIT2: It appears these are gzip files saved with an .R extension. I can using load() to read the data into R. Is there any other way I can access these data files?
"filename" is now loaded and it is stored in an object of the same name. You should be able to see what it is inside by running:
filename

How to point to a directory in an R package?

I am making my first attempts to write a R package. I am loading one csv file from hard drive and I am hoping to bundle up my R codes and my csv files into one package later.
My question is how can I load my csv file when my pakage is generated, I mean right now my file address is something like c:\R\mydirectory....\myfile.csv but after I sent it to someone else how can I have a relative address to that file?
Feel free to correct this question if it is not clear to others!
You can put your csv files in the data directory or in inst/extdata.
See the Writing R Extensions manual - Section 1.1.5 Data in packages.
To import the data you can use, e.g.,
R> data("achieve", package="flexclust")
or
R> read.table(system.file("data/achieve.txt", package = "flexclust"))
Look at the R help for package.skeleton: this function
automates some of the setup for a new source package. It creates directories, saves functions, data, and R code files to appropriate places, and creates skeleton help files and a ‘Read-and-delete-me’ file describing further steps in packaging.
The directory structure created by package.skeleton includes a data directory. If you put your data here it will be distributed with the package.

Resources