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")
source share