How can I make a vanilla R session?

This is a continuation to clarify the previous question, How can I provide a consistent R environment among different users on the same server?

I would like to introduce a "vanilla" R session from R, for example. similar to what I would get if I ran R using the R --vanilla . For example, I would like to write a script that does not mix with the user's specific user preferences.

In particular, I would like the following

  • does not read history, profile, or R environment files.
  • does not reload data or objects from previous sessions

help("vanilla") returns nothing, and I am not familiar enough with the user preferences area to learn how to exit them.

Is there a way to enter a new, vanilla environment? ( ?new.env doesn't seem to help)

+4
source share
2 answers

You can't just make your current vanilla session, but you can start a new vanilla session R from inside R, like this

 > .Last <- function() system("R --vanilla") > q("no") 

I think you are likely to run into the problem using the above, because after restarting R, the rest of your script will not be executed. In the following code, R will run .Last until it completes. .Last will report that it will restart without reading the file of the site or environment file, and will not print a start message. After a restart, it will run your code (and also perform some other cleanup).

 wd <- getwd() setwd(tempdir()) assign(".First", function() { #require("yourPackage") file.remove(".RData") # already been loaded rm(".Last", pos=.GlobalEnv) #otherwise, won't be able to quit R without it restarting setwd(wd) ## Add your code here message("my code is running.\n") }, pos=.GlobalEnv) assign(".Last", function() { system("R --no-site-file --no-environ --quiet") }, pos=.GlobalEnv) save.image() # so we can load it back when R restarts q("no") 
+3
source

IMHO, reproducible research and interactive sessions are not combined with each other. You should consider writing executable scripts called from the command line, rather than from an open interactive session. At the top of the script, add --vanilla to shebang:

 #!/path/to/Rscript --vanilla 

If your script should read the inputs (arguments or parameters), you can use ?commandArgs or one of the two getopt or optparse packages to parse them from the command line.

If the user really needs to do his work in an interactive session, he can still do it and call the script through system() : your script will still use its own vanilla session. There's just a bit of extra work going through the entrances and exits.

+5
source

All Articles