37

Every now and then I have to run a function that takes a lot of time and I need to interrupt the processing before it's complete. To do so, I click on the red sign of "stop" at the top of the console in Rstudio, which quite often returns this message below:

R is not responding to your request to interrupt processing so to stop the current operation you may need to terminate R entirely.

Terminating R will cause your R session to immediately abort. Active computations will be interrupted and unsaved source file changes and workspace objects will be discarded.

Do you want to terminate R now?

The problem is that I click "No" and then Rstudios seems to freeze completely. I would like to know if others face a similar issue and if there is any way to get around this.

Is there a way to stop a process in Rstudio quickly without losing the objects in the workspace?

Brian Stamper
  • 2,143
  • 1
  • 18
  • 41
rafa.pereira
  • 13,251
  • 6
  • 71
  • 109
  • 2
    It does occasionally become dysfunctional...I think it is probably not that uncommon. I have had it happen on Mac, PC and Linux machines. I use the task manager to purge it before restarting. – sconfluentus Mar 31 '17 at 20:35
  • when working with big datasets, the "occasionally" hurts... ;) I have this issue several times each day. data.table helps a bit though (uses C++ to my knowledge but is just super fast) – Jakob Aug 04 '21 at 19:22

2 Answers2

25

Unfortunately, RStudio is currently not able to interrupt R in a couple situations:

  1. R is executing an external program (e.g. you cannot interrupt system("sleep 10")),

  2. R is executing (for example) a C / C++ library call that doesn't provide R an opportunity to check for interrupts.

In such a case, the only option is to forcefully kill the R process -- hopefully this is something that could change in a future iteration of RStudio.


EDIT: RStudio v1.2 should now better handle interrupts in many of these contexts.

Kevin Ushey
  • 20,530
  • 5
  • 56
  • 88
  • Does that mean that it will stop at the earliest possible opportunity, or that it won't stop at all? E.g., if I try to stop R while it's executing a C/C++ library call, will it stop as soon as it finishes doing so, or will it continue to complete any other tasks it's been given? If it's the former, that seems like the worst of all worlds, because the unstoppable component might be the longest portion of time - so if you press stop and it doesn't work, you could be left waiting almost as much time as if you'd just let it run - but get no output. – Statsanalyst May 11 '18 at 18:26
  • 1
    It will stop as soon as the R event loop gets a chance to run, and discovers that the interrupt flag has been set. I agree this is not at all ideal :-/ – Kevin Ushey May 11 '18 at 18:48
  • 6
    Unfortunately, lots of R code is actually C or Fortran code and Ctrl+C often does not work. Even such things like drawing a single rectangle with rect() calls C code and cannot be stopped. This is a serious design limitation. – Met Apr 10 '19 at 19:28
3

This could happen when R is not working within R and is invoking an external library call. The only option is to close the project window. Fortunately, unsaved changes including objects are retained on opening RStudio again.

UDIT GUPTA
  • 39
  • 1