It seems unusual to me to use the source in this way - it fills the environment from which it is launched, with any characters that it assigns, so several calls to the source can easily have unforeseen side effects. Instead, I will follow @ JoshuaUlrich's recommendations and formulate the script as a function (reusable, perhaps more modular). But for what it's worth, create and report a "condition" in "model.R"
cond = structure(list(message="I'm done"), class=c("exit", "condition")) signalCondition(cond)
and catch while searching
tryCatch(source("model.R"), exit=function(cond) { message("finished because:", conditionMessage(cond)) })
Minimize changes to model.R by defining exit()
exit <- function(message=character(), class="exit") { cond <- structure(list(message=message), class=c(class, "condition")) signalCondition(cond) } for (i in 1:5) { ## model.R simply invokes exit() or exit("I'm done") or exit(class="alt") ## if tryCatch wanted to handle conditions of class 'alt', too. tryCatch(source("model.R"), exit=message) }
I think the invokeRestart() solution is more straight forward; merit above - the ability to indicate the reason for the early exit.
source share