Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exit a sourced R script

Tags:

r

Is there a function to exit the current R script, but not exit the whole execution?

I mean, in a situation like this:

for (i in ...) { 
    ...
    source("model.R")
}

In file model.R, I need an exit function that would end the exection of the model.R:

...
exit 
...

So stop and stopifnot are out of question...

like image 536
Tomas Avatar asked Aug 14 '14 16:08

Tomas


1 Answers

Seems that by far the easiest solution is to wrap the whole code in the sourced script into a loop and then break from it:

model.R (sourced script):

repeat {

...
break # this will exit the sourced script
...

break # just to prevent infinite loop in case the above break is removed
}

Thanks Joshua Ulrich for inspiration (he proposed to use a function).

like image 178
Tomas Avatar answered Oct 12 '22 00:10

Tomas