4

What function (if any) is called when pressing Ctrl+D to exit R repl? I saw in a few questions, such as:

code that led me to believe it's calling either function q or quit.

The reason I want to override is to make the pesky:

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

prompt on exit go away. However, overriding the function in .Rprofile such as:

quit <- function(...) {
  print(1)
}

and similarly for q did not work - i.e. pressing Ctrl-D did not actually print number 1, went straight to the prompt.

The solutions presented in the above links did not seem to work. R version used:

R version 2.15.2 (2012-10-26) -- "Trick or Treat"
Copyright (C) 2012 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
Platform: x86_64-pc-linux-gnu (64-bit)
Community
  • 1
  • 1
icyrock.com
  • 27,952
  • 4
  • 66
  • 85
  • BTW It's high time you upgraded. A lot of R packages will not run under that Jurassic version you're running! – Carl Witthoft Dec 13 '13 at 12:36
  • @CarlWitthoft Yeah, I know :) Unfortunately, that's the latest available in the repositories at the moment for Xubuntu 13.04 - yeah, need to upgrade that too... :) – icyrock.com Dec 14 '13 at 01:00
  • Several years later, is there yet any satisfactory answer to this question? – nsheff Sep 14 '17 at 15:25

1 Answers1

5

Ctrl-D does not call any function, it is special keyboard interrupt.

Try pressing Ctrl-D, then answer c for cancel. If you press the up arrow to get the last command, you'll see it's not there.

To override the pesky Save workspace image? [y/n/c]:, see the answer to this question:

To summarise you have three options:

Calling R --no-save instead of R,

Loading the following in the interactive R session (won't work from .Rprofile):

require(Defaults)
setDefaults(q, save="no")
useDefaults(q)

Or put the following in your .Rprofile:

# Set hook to be run when Defaults is attached
setHook(packageEvent("Defaults", "attach"),
  function(...) { setDefaults(q, save="no"); useDefaults(q) })
# add Defaults to the default packages loaded on startup
old <- getOption("defaultPackages");
options(defaultPackages = c(old, "Defaults"))

EDIT:

Here's another hack I can think of since the above haven't worked for your case. It's not an R solution, but might do the trick?

First, move you R executable file (for the purposes of this example i'm going to assume it's in /usr/bin/) to a new file, something like:

sudo mv /usr/bin/R /usr/bin/Rold

Now set up a new bash script as /usr/bin/R:

#!/bin/bash

/usr/bin/Rold --no-save "$@"

and chmod it to have the right permissions.

Community
  • 1
  • 1
Scott Ritchie
  • 10,293
  • 3
  • 28
  • 64
  • Thanks @Manetheran. AFAIK, Ctrl+D is not an interrupt, it just an EOT character (http://en.wikipedia.org/wiki/Control-D). So it looks like R is specifically asking for workspace save when EOT is encountered. The .Rprofile suggestion (which I already tried, see the links in my question) doesn't work. The only option that works is using `R --no-save`, however if an external program is running R that's not an option. Is there a pure R solution to this? – icyrock.com Dec 14 '13 at 01:22
  • I've added another answer. It's not an R solution, but a hack that should make the external program call `R --no-save`. – Scott Ritchie Dec 14 '13 at 03:25
  • 1
    [Package ‘Defaults’ was removed from the CRAN repository.](https://cran.r-project.org/web/packages/Defaults/index.html) (in 2014?) – aleksandr barakin Dec 16 '18 at 23:35