3

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 ?

5
  • What about break? Commented Sep 28, 2021 at 9:17
  • Does this post answer your question? stackoverflow.com/questions/34787004/… Commented Sep 28, 2021 at 9: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 :) Commented Sep 28, 2021 at 13:02
  • Does this answer your question? How do I stop/end/halt a script in R? Commented Sep 28, 2021 at 13:25
  • Have you tried stopApp as suggested below? Commented Sep 28, 2021 at 13:26

4 Answers 4

4

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!

Sign up to request clarification or add additional context in comments.

1 Comment

I will try and implement functions into my script, in order to use that solution. Thank you :)
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"

1 Comment

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 !
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!

2 Comments

Actually I would like the code to stop on its own when the condition is TRUE
My bad. You can try the stop function. rdocumentation.org/packages/base/versions/3.6.2/topics/stop The example at the bottom has a similar usecase.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.