This is a follow up for clarification of a previous question, How can I ensure a consistent R environment among different users on the same server?
I\'d like to enter a
You can't just make your current session vanilla, but you can start a fresh vanilla R session from within R like this
> .Last <- function() system("R --vanilla")
> q("no")
I think you'll probably run into a problem using the above as is because after R restarts, the rest of your script will not execute. With the following code, R will run .Last
before it quits. .Last
will tell it to restart without reading the site file or environment file, and without printing startup messages. Upon restarting, it will run your code (as well as doing 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")