114

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

ulidtko
  • 14,740
  • 10
  • 56
  • 88

12 Answers12

88

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.

jan-glx
  • 7,611
  • 2
  • 43
  • 63
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
  • +1 for `.Rprofile`, didn't know about it but thats really useful! – Sacha Epskamp Feb 14 '11 at 22:26
  • 2
    @Sacha So take a look on http://stackoverflow.com/questions/1189759/expert-r-users-whats-in-your-rprofile – Marek Feb 15 '11 at 08:32
  • 2
    After I installed the Defaults package and added the above code to my Rprofile.site file (running R 2.15.1 on Windows 7), I get the following error: "Error in bindingIsLocked(name, as.environment(find(name))) : could not find function 'find'" – John D. Cook Jul 02 '12 at 13:41
  • @JohnD.Cook: good point. Only the `base` package is available at that point, so I'd have to think about how to work around that. – Joshua Ulrich Jul 02 '12 at 13:54
  • @JohnD.Cook: See my edit for a solution to your comment. Thanks for pointing out my error. – Joshua Ulrich Jul 05 '12 at 00:43
  • Run `install.packages("Defaults")` in R to install Defaults package first. It worked then on debian stable 6 with R 2.15 – Jonas Stein Jan 11 '13 at 10:35
  • 2
    The Defaults package has apparently been removed. – Praxeolitic Nov 29 '14 at 09:43
  • 1
    I'm getting "Error: could not find function "assignInNamespace"". R 3.1.2 on OS X – Josh Hansen Jun 16 '15 at 23:42
  • 5
    @Josh Hansen add library(utils) to your .Rprofile to find assignInNamespace – FXQuantTrader Jul 12 '15 at 15:24
  • 2
    I'm also getting `Error in utils::assignInNamespace("q", function(save = "no", status = 0, : locked binding of ‘q’ cannot be changed` with `R v3.4.2` when I add it to my `.Rprofile` but do not get this error when I run the command in the terminal interactively. – O.rka Feb 28 '18 at 19:04
  • @O.rka: are you putting the code inside an expression passed to `local()`? I get the same error when I do that, but it works if I put the code outside the `local()` call. – Joshua Ulrich Mar 04 '18 at 17:41
  • 1
    Let me **warn** you folks, using `library(foobar)` in `~/.Rprofile` has broken me a several builds invoked via `?install.packages` (during native C or Fortran compilation, the driver/wrapper scripts in R start to fail). Be ready to comment out, just in case. – ulidtko Jan 22 '21 at 16:56
52

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.

mreq
  • 6,414
  • 4
  • 37
  • 57
  • 4
    Or as @aL3xa specified in a comment above use `R --vanilla` to combine `Combine --no-save, --no-restore, --no-site-file, --no-init-file and --no-environ`. – Paul Rougieux Jan 26 '16 at 07:20
  • Don't forget to `reboot` server after updating `~/.bashrc` – atsyplenkov Aug 21 '22 at 07:41
  • 1
    I would use `alias r="R --no-save"`. I don't want to press `Shift` while typing `R`. @atsyplenkov: You don't need to reboot. You just need to type `source ~/.bash_aliases`. – Nav Sep 14 '22 at 09:50
29

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)

Dennis
  • 56,821
  • 26
  • 143
  • 139
Hugo Ideler
  • 10,094
  • 2
  • 20
  • 22
13

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

Andrie
  • 176,377
  • 47
  • 447
  • 496
9

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

Pafnucy
  • 608
  • 1
  • 13
  • 17
8

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)

hugomg
  • 68,213
  • 24
  • 160
  • 246
flyingfinger
  • 690
  • 12
  • 16
  • I like it as `wq <- function() { q(save="yes")}` to mirror vi. (also, if you're writing an overwrite function, why not make it send a fixed argument) – Dannid Oct 05 '20 at 17:48
6

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.

IRTFM
  • 258,963
  • 21
  • 364
  • 487
6

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

Dr Nisha Arora
  • 632
  • 1
  • 10
  • 23
3

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

Stedy
  • 7,359
  • 14
  • 57
  • 77
3

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.

snakeoilsales
  • 113
  • 1
  • 6
2

You can create an alias for the R command:

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

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

Avi
  • 21,182
  • 26
  • 82
  • 121
wizmer
  • 881
  • 1
  • 8
  • 23
-3

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().

Quentin Hayot
  • 7,786
  • 6
  • 45
  • 62
Bijoy J
  • 11
  • 1
    I do **never ever** directly edit anything under `/usr/bin` — and strongly discourage you from doing so. That's the system area, "unauthorized personnel is prohibited". – ulidtko Dec 02 '15 at 21:35
  • 4
    @ulidtko If it's my system, I'm authorized :) I think the real reason this isn't a good idea is because it's going to get overwritten by future upgrades. – Michael Mior Sep 25 '17 at 16:56
  • @MichaelMior well yes, you're absolutely correct; however: humans mess up. All the time. It's simply a guideline; unless you have a good reason, leave `/usr` to robots (I mean, the package managers) since they already manage that area. There're ways to not interfere. Otherwise, you're purposefully seeking adventures. – ulidtko Sep 26 '17 at 09:14