2

I am a beginner in R. I want to be able to interrupt the currently running script if a condition is true. The closest thing I have found is the ps_kill function, which crashes Rstudio.

df <- data.frame(one = c(1,2,NA,4,NA), two = c(NA,NA,8,NA,10))

if (sum(is.na(df)) > 3)
{
ps_kill(p = ps_handle())
}

Is there a function I could use to replace ps_kill, to interrupt the script without crashing Rstudio ?

Vinícius Félix
  • 8,448
  • 6
  • 16
  • 32
Shinipop
  • 23
  • 3
  • What about `break`? – jay.sf Sep 28 '21 at 09:17
  • Does this post answer your question? https://stackoverflow.com/questions/34787004/how-do-i-stop-end-halt-a-script-in-r – Jan Sep 28 '21 at 09:31
  • @Jan Yes it is exactly my question! I ended up doing an "if else" construction combined with quit(save="ask") as suggested by the answers :) – Shinipop Sep 28 '21 at 13:02
  • Does this answer your question? [How do I stop/end/halt a script in R?](https://stackoverflow.com/questions/34787004/how-do-i-stop-end-halt-a-script-in-r) – Jan Sep 28 '21 at 13:25
  • Have you tried `stopApp` as [suggested below](https://stackoverflow.com/a/69359386/13241545)? – Jan Sep 28 '21 at 13:26

4 Answers4

3

The stop function will throw an error and effectively terminate your script if you run it with Rscript or source. But keep in mind that this will terminate with an error. For instance:

# A function that will throw an error and quit
test <- function() {
  print("This is printed")
  stop()
  print("this is not printed")
}
test()

Note that you can recover from an error throwing code by wrapping it in a try call:

# This will not throw an error and will not print the second sentence
try(test(), silent = TRUE)

Another solution if you really want to close R and not just finish your script, is to use the function q. This is not recoverable (it closes the R session).

I hope this answer your question!

celbig
  • 76
  • 4
0

If you have long running code you can try the 'Stop' button right top side of your console panel in R studio.

As shown in this screenshot. https://prnt.sc/1txafh0

Hope this is what you're looking for!

SeriousK
  • 1
  • 1
  • Actually I would like the code to stop on its own when the condition is TRUE – Shinipop Sep 28 '21 at 09:02
  • My bad. You can try the stop function. https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/stop The example at the bottom has a similar usecase. – SeriousK Sep 28 '21 at 09:26
0

The stop() function returns an error if called, so you can use this. The only trick is that if you're using it in interactive mode, you need to wrap all the code that you want to skip in a set of braces, e.g.

df <- data.frame(one = c(1,2,NA,4,NA), two = c(NA,NA,8,NA,10))

{
  if(sum(is.na(df)) > 3) stop("More than three NAs")
  print("don't run this")
}

# Error: More than three NAs

Note the braces include the print line, otherwise stop would just carry on running the code after the line that causes an error, e.g.

if(sum(is.na(df)) > 3) stop("More than three NAs")
# Error: More than three NAs
print("don't run this")
# [1] "don't run this"
Miff
  • 7,486
  • 20
  • 20
  • 1
    I understand now why stop() didn't work when I used it haha. All of my script is in interactive mode so I will have to implement functions in it, so I can use stop(). Thank you for the help ! – Shinipop Sep 28 '21 at 09:28
0

If you are running in interactive mode (like you said in your comment), stopApp should terminate the process without causing an error.

Jan
  • 4,974
  • 3
  • 26
  • 43