0

I've seen this question which answers how to exit a function when needed. But I'm working with a complex R script which I'm debugging. I run it in VScode, and would be nice if there was a command to simply stop running the script altogether, as using exit(1) in python. See the code below for example.

rm(list=ls()) # Ensuring workspace starts off clean

a <- 1 # Do stuff I'm confident about
b <- 2 # Stuff I need to check if worked

cat(b)

Function_to_Stop_Execution_Here() # What can I for about this?

Stuff_I_dont_want_to_run()
Mefitico
  • 816
  • 1
  • 12
  • 41

3 Answers3

1

If you want to stop a script, use stop (which raises an error).

If you possibly want to restart, but need to examine the working state, consider browser

a <- 1 # Do stuff I'm confident about
b <- 2 # Stuff I need to check if worked

cat(b)
browser() # or stop("Some error message")

But if there is stuff you don't want to run below this, it's probably best to delete it.

Hugh
  • 15,521
  • 12
  • 57
  • 100
0

Try using quit or q. ]

However, quit will also completely quit R. If you want to return to the R console after your function to stop execution, you can use stopifnot to purposefully throw an error.

Ryan Zhang
  • 1,856
  • 9
  • 19
0

You might use quit() with argument save='ask' and type 'c' in console then.

a <- 1 # Do stuff I'm confident about
b <- 2 # Stuff I need to check if worked

cat(b)

quit(save='ask')
jay.sf
  • 60,139
  • 8
  • 53
  • 110