Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

break/exit script

Tags:

r

exit

break

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

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

I've tried break,browser, and quit and none of them stop the execution of the rest of the program (and quit stops the execution as well as completely quitting R, which is not something I want to happen). My last resort is creating an if-else statement as below:

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

but that seems like bad coding practice. Am I missing something?

like image 745
user2588829 Avatar asked Jul 24 '13 14:07

user2588829


People also ask

How do you exit a script?

One of the many known methods to exit a bash script while writing is the simple shortcut key, i.e., “Ctrl+X”. While at run time, you can exit the code using “Ctrl+Z”.

How do I stop a shell script?

To end a shell script and set its exit status, use the exit command. Give exit the exit status that your script should have. If it has no explicit status, it will exit with the status of the last command run.

How do I exit bash script error?

Exit When Any Command Fails This can actually be done with a single line using the set builtin command with the -e option. Putting this at the top of a bash script will cause the script to exit if any commands return a non-zero exit code.


1 Answers

You could use the stopifnot() function if you want the program to produce an error:

foo <- function(x) {     stopifnot(x > 500)     # rest of program } 
like image 60
Michael Malick Avatar answered Oct 18 '22 01:10

Michael Malick