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?
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”.
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.
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.
You could use the stopifnot()
function if you want the program to produce an error:
foo <- function(x) { stopifnot(x > 500) # rest of program }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With