0

I am trying to share some R code with colleagues who don't know R. I have created a batch file so they can just double-click it and run the R script without even opening R. But it creates a .RData file.

My question is, can I prevent R from creating the .RData file?

I've read here Disable saving history that I could disable it through RStudio global options but my colleagues are installing just R and won't need to ever open it, so I am looking for some kind of solution of the likes of options(...) that I can just put in my Rscript, or maybe something that could be speficied in the batch file call.

wernor
  • 391
  • 2
  • 16
  • When I run `R` from the console, it does not save to the `.Rhistory` file. How are your colleagues using R such that the `.Rhistory` file is being created? – r2evans Sep 19 '22 at 10:31
  • You should show us a sample script, but it's likely that all you need is `q(save = "no")`. – user2554330 Sep 19 '22 at 10:32
  • @r2evans you're right, sorry. The `.Rhistory` was generated after running it through RStudio. But the `.RData` does appear when executing the batch file to run the script. – wernor Sep 19 '22 at 10:57
  • Why not using rmarkdown for that? Since they "don't know R", your colleagues will be happy about your helpful and nicely formatted comments your report will come with. – jay.sf Sep 19 '22 at 11:03
  • How are your colleagues using R? Are they using Rgui/Rterm? Rscript? `R CMD BATCH`? `R --no-save` (suggested by reading `R --help`) should preclude saving the `~/.Rdata` file. – r2evans Sep 19 '22 at 11:04
  • @jay.sf I'm not actually reporting anything, my script is to update some excel files after doing some calculations in R, so my colleagues don't need to see anything, just double-click and then the excel files are updated. Thanks for the idea though! – wernor Sep 19 '22 at 11:08
  • @r2evans I'm doing it through a batch file with `R CMD BATCH "%file%" NUL`. Should I just add `--no-save` ? – wernor Sep 19 '22 at 11:09
  • @r2evans I'm doing it through a batch file with `R CMD BATCH "%file%" NUL`. I tried `R --no-save CMD BATCH "%file%" NUL` but the `.RData` keeps appearing. (`"%file%"` is the Rscript path) – wernor Sep 19 '22 at 11:18
  • Try `R` instead of `R CMD BATCH`; the latter does not support `--no-save`. The use of `R` instead of `R CMD BATCH` will require you to handle stdin and stdout/stderr yourself, not sure if that'd be a problem. – r2evans Sep 19 '22 at 11:26
  • I don't actually know what stdin or stdout/stderr are... Not sure how to do that. – wernor Sep 19 '22 at 11:32
  • I figured it out, I was messsing up the order. Thank you so much for your help! – wernor Sep 20 '22 at 06:57

1 Answers1

-1

For anyone curious, I figured it out thanks to @r2evans and this post: https://es.stackoverflow.com/questions/166211/evitar-que-r-cree-ficheros-r-data-y-r-history.

I just had to add --no-save to my batch call like this:

R CMD BATCH --no-save "%file%" NUL

where %file% is the Rscript path. The NUL part is so a .Rout file won't be created after running the batch file.

Also, doing

Rscript.exe "%file%" 

runs the Rscript and doesn't produce .RData or .Rout files. The difference is that R messages get printed on the command window as they would in an interactive R session.

wernor
  • 391
  • 2
  • 16