How to disable "Save workspace image?" prompt in R?

RRead Eval-Print-Loop

R Problem Overview


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, because if I wished to save my work, I'd do that before trying to exit.

How to get rid of the prompt?


Note: see ?save.image

R Solutions


Solution 1 - R

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.

Solution 2 - R

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.

Solution 3 - R

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)

Solution 4 - R

If you are using Rgui, right-click on the icon you use to start R and click on "Properties", and add --no-save to the command that starts R.

(from http://tolstoy.newcastle.edu.au/R/help/05/03/1115.html)

If you are using a different editor than Rgui, you have to pass --no-save to the R command line when starting R

Solution 5 - R

Overwrite default option for save argument of quit function

formals(quit)$save <- formals(q)$save <- "no"

put this line in .Rprofile

Edit: added q, so there is no prompt no matter which variant is used

Solution 6 - R

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() { q(save="yes") }

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

Solution 7 - R

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.

Solution 8 - R

How about just avoiding the prompt by typing q('no') instead

Solution 9 - R

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.

Solution 10 - R

You can create an alias for the R command:

using bash: alias R='R --no-save'

using csh: alias R 'R --no-save'

Solution 11 - R

If you're using R Studio IDE, you can do this by re-setting the global option.

Go to Tools --> Global Options --> R General (Basic)

  1. Uncheck Restore .RData into your workspace at startup
  2. Save workspace image to .RData on exit -- Select 'Never' See screenshot below

Click on 'Apply' and then 'Ok'

I've written a detailed post on this topic here

In this post, I've addressed 'should we or should we not' save workspace image? I've written detailed answer to the following questions in the comments (make sure you read them all):

  1. How to set never save workspace image?
  2. what does it really mean when it says ‘save workspace image’.
  3. Why you should (almost always) not save workspace image?
  4. When should I save workspace image?
  5. If not saving the workspace image, what should I do? What are the best practices?

In some other posts, I've discussed that might be useful for R users are:

'What is reproducible work?' https://www.linkedin.com/feed/update/urn:li:activity:6789770117715640320

Why should you not use rm(list=ls())? R Best Practices https://www.linkedin.com/feed/update/urn:li:activity:6785805481131683840

Working directory https://www.linkedin.com/posts/drnishaarora_2-set-working-directory-r-studio-activity-6785423883408297984-NAoH

Solution 12 - R

If you feel adventurous enough, you could also edit the startup section at the end of /usr/bin/R, i.e. add --no-save to the exec calls. However, if you need to save your workspace, remember to save.image().

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionulidtkoView Question on Stackoverflow
Solution 1 - RJoshua UlrichView Answer on Stackoverflow
Solution 2 - RmreqView Answer on Stackoverflow
Solution 3 - RHugo IdelerView Answer on Stackoverflow
Solution 4 - RAndrieView Answer on Stackoverflow
Solution 5 - RPafnucyView Answer on Stackoverflow
Solution 6 - RflyingfingerView Answer on Stackoverflow
Solution 7 - RIRTFMView Answer on Stackoverflow
Solution 8 - RStedyView Answer on Stackoverflow
Solution 9 - RsnakeoilsalesView Answer on Stackoverflow
Solution 10 - RwizmerView Answer on Stackoverflow
Solution 11 - RDr Nisha AroraView Answer on Stackoverflow
Solution 12 - RBijoy JView Answer on Stackoverflow