Break / exit script

I have a program that does some data analysis and is several hundred lines long.

Very early in the program, I want to do some quality control, and if there is not enough data, I want the program to end and return to the R console. Otherwise, I want the rest of the code to be executed.

I tried break , browser and quit , and none of them stopped execution of the rest of the program (and quit stops execution, and also completely leaves R, which is not what I want to happen). In the latter case, I create an if-else , as shown below:

  if(n < 500){} else{*insert rest of program here*} 

but this seems like bad coding practice. Did I miss something?

+46
r break exit
Jul 24 '13 at 14:41
source share
7 answers

You can use the stopifnot() function if you want the program to generate an error:

 foo <- function(x) { stopifnot(x > 500) # rest of program } 
+41
Jul 24 '13 at 14:56
source share

Cancel the if-else construct:

 if(n >= 500) { # do stuff } # no need for else 
+8
Jul 24 '13 at 14:46
source share

Edit: It seems that OP is working with a long script, in this case you only need to wrap the script part after quality control with

 if (n >= 500) { .... long running code here } 

If you exit the function , you probably just want return() , explicitly or implicitly.

For example, explicit double return

 foo <- function(x) { if(x < 10) { return(NA) } else { xx <- seq_len(x) xx <- cumsum(xx) } xx ## return(xx) is implied here } > foo(5) [1] 0 > foo(10) [1] 1 3 6 10 15 21 28 36 45 55 
The term return() means that the last line looks as if you did return(xx) , but it’s a bit more efficient to refuse return() .

Some consider using multiple incorrect return styles; in long functions, keeping track of where the exit function can become complex or error prone. Therefore, an alternative is to have a single return point, but change the returned object using the if () else () clause. Such a modification of foo() would be

 foo <- function(x) { ## out is NA or cumsum(xx) depending on x out <- if(x < 10) { NA } else { xx <- seq_len(x) cumsum(xx) } out ## return(out) is implied here } > foo(5) [1] NA > foo(10) [1] 1 3 6 10 15 21 28 36 45 55 
+5
Jul 24. '13 at 14:47
source share

Not really, but here is a way to implement the exit() in R, which works for me.

 exit <- function() { .Internal(.invokeRestart(list(NULL, NULL), NULL)) } print("this is the last message") exit() print("you should not see this") 

Only slightly tested, but when I run this, I see this is the last message , and then the script breaks without an error message.

+4
May 10 '17 at 13:43
source share

You might just want to stop executing a long script at some point. i.e. for example, you want to hard code exit () in C or Python.

 print("this is the last message") stop() print("you should not see this") 
+2
Apr 19 '15 at 23:07 on
source share

You can use the pskill function in the R "tools" package to interrupt the current process and return to the console. Specifically, I have the following function defined in the launch file, which I run at the beginning of each script. However, you can also copy it directly at the beginning of your code. Then insert halt() anywhere in your code to stop script execution on the fly. This feature works well on GNU / Linux and, judging by the R documentation, it should also work on Windows (but I haven't tested it).

 # halt: interrupts the current R process; a short iddle time prevents R from # outputting further results before the SIGINT (= Ctrl-C) signal is received halt <- function(hint = "Process stopped.\n") { writeLines(hint) require(tools, quietly = TRUE) processId <- Sys.getpid() pskill(processId, SIGINT) iddleTime <- 1.00 Sys.sleep(iddleTime) } 
+1
Aug 20 '14 at 11:30
source share

Here:

 if(n < 500) { # quit() # or # stop("this is some message") } else { *insert rest of program here* } 

Both quit () and stop (message) will exit your script. If you select a script from the R command line, then quit () will also exit R.

-one
Feb 23 '14 at 21:07
source share



All Articles