How to disable “Save workspace image?” prompt in R?

前端 未结 11 1812
迷失自我
迷失自我 2020-11-29 23:51

When I exit the interactive R shell, it displays an annoying prompt every time:

>
>
Save workspace image? [y/n/c]: n

I\'m always answering \"no\" to it

相关标签:
11条回答
  • 2020-11-29 23:57

    You can pass the --no-save command line argument when you start R, or you can override the q function:

    utils::assignInNamespace(
      "q", 
      function(save = "no", status = 0, runLast = TRUE) 
      {
        .Internal(quit(save, status, runLast))
      }, 
      "base"
    )
    

    Put the above code in your .Rprofile so it will be run on startup for every session.

    0 讨论(0)
  • 2020-11-30 00:02

    Get the best of both strategies given by users 1 and 2:

    Default to not save by adding the following line to your ~/.bashrc:

    alias R='R --no-save'
    

    But give yourself an easy way to save on exit by adding this to ~/.Rprofile:

    qs <- function(save="yes") { q(save=save)}
    

    So now q() will quit without saving or prompting but qs() will save and quit (also without prompting)

    0 讨论(0)
  • 2020-11-30 00:03

    Haven't found the easiest Linux solution yet :)

    On ubuntu add the following line to your ~/.bashrc:

    alias R='R --no-save'
    

    Every time you start the R console with R, it will be passed the --no-save option.

    0 讨论(0)
  • 2020-11-30 00:06

    You can escape the "Save workspace image?" prompt with a Ctrl+D.

    Thus, if you do Ctrl+D twice in interactive R, then you exit R without saving your workspace.

    (Tested on Linux and OS X)

    0 讨论(0)
  • 2020-11-30 00:09

    If, like me, typing out a whole pair of brackets seems like too much effort to exit the repl you can try this:

    exit <- structure(list(), class = "exit_command")
    
    print.exit_command <- function(...) {
      q("no")  # exit without saving
    }
    

    This creates a new class, which causes R to exit when attempting to print said class. The upshot being that if you run exit in the R repl, the whole thing will exit (because it tries to print it).

    NB: You can add it to ~/.Rprofile to load at the start of every session.

    0 讨论(0)
  • 2020-11-30 00:12

    You could easily add a qq() function to the .Rprofile file

     qq <- function(save="no") { q(save=save)}
    

    I thought that the save option was available with options, but apparently Joshua's answer is best.

    0 讨论(0)
提交回复
热议问题