How can I make my R session vanilla?

前端 未结 1 397
一个人的身影
一个人的身影 2021-02-09 09:16

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

相关标签:
1条回答
  • 2021-02-09 09:42

    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") 
    
    0 讨论(0)
提交回复
热议问题