5

Sometimes R takes a lot of time to finish a trivial task, like when I ask it to print something and it turns out that it is too long or complicated and R just sits there at 100% CPU utilization. What are my options? Ctrl-C does not help. Is there a way to kill R so that it saves the workspace beforehand?

PS. I am running under Emacs/ESS on Mac OS X. I know about process management under Unix (C-c, C-z, bg/bg, kill &c). I wonder about R-specific tricks (e.g., "if you send signal SIGUSR1 to R, it will silently save workspace and exit immediately" - believe it or not, I wrote the previous sentence before I found the linked answer :-).

Community
  • 1
  • 1
sds
  • 58,617
  • 29
  • 161
  • 278
  • 1
    Are you running the R GUI, Rscript from the command line, RStudio, ...? – Alex A. Mar 05 '15 at 21:58
  • 1
    did you already checked all the previous answers http://stackoverflow.com/questions/8370548/how-can-i-interrupt-a-running-code-in-r-with-a-keyboard-command http://stackoverflow.com/questions/9337825/how-to-stop-a-running-r-command-in-linux-other-than-ctrlc http://unix.stackexchange.com/questions/97968/breaking-from-a-program-running-in-an-interactive-session-in-a-screen-window –  Mar 05 '15 at 22:14

2 Answers2

3

If Ctrl-c (or the stop sign button in Rstudio or RGui) is not working then your options are limited. If you must save your work you will just have to wait. If you're in Linux you can move it to the background by pressing Ctrl-z followed by the bg command. When you want to check on it run fg to bring it back up. In Windows press the minimize button :)

You can still kill the process to stop it, but you will lose any unsaved work.

To kill the process in Linux:

  • Hit Ctrl-z to move R to the background
  • Run ps and find the process id
  • Run kill [id] to kill the process (if it is still not stopping use kill -KILL [id])

To kill the process in Windows:

  • Hit Ctr-Shift-Esc to bring up task manager
  • Go the the processes tab
  • Select the R session process (rsession.exe for RStudio)
  • Click "End Process"
Danny
  • 3,077
  • 2
  • 23
  • 26
  • these are general unix suggestions, I am looking for an R-specific advice – sds Mar 06 '15 at 01:06
  • Yeah, I'm sorry. Unfortunately I don't think a great solution to this problem exists; just the OS work-arounds. One special-case I've found a solution for is when it is just stuck printing a giant matrix or something to the console. You can limit the maximum print length with `options(max.print=1000)`. Good luck! – Danny Mar 06 '15 at 18:13
  • I have `options(max.print=100)` and I still routinely get stuck in print. The problem is that R *first* converts the object to a string representation, and only *then* looks at `max.print`. – sds Mar 06 '15 at 18:15
  • Yeah for really big/wide stuff it can still be a problem. In that case I usually end up using the "head" function or for wide matrix data I use data[1:10,1:10]. – Danny Mar 06 '15 at 18:23
  • sure - but what if you type `data RET` - now you are stuck forever! – sds Mar 06 '15 at 18:24
  • Hmm, not sure I can help with that. How big is the data you're dealing with and what are it's dimensions? Is it all in memory or are you swapping out to the hard drive? Only idea I can think of is if there's some way to disable R's auto-printing of values that you execute without an explicit function. But that would get very tiresome and I can't find anything for that anyway. – Danny Mar 06 '15 at 19:51
0

When you run code in RStudio, there should be a little 'Stop sign' in the console. Try clicking that. Most times you won't see this stop sign unless the code that you are running takes a while.

Danny M.
  • 281
  • 1
  • 12