Last chance! 50% off unlimited learning
Sale ends in
evalWithTimeout(..., envir=parent.frame(), timeout, cpu=timeout, elapsed=timeout, onTimeout=c("error", "warning", "silent"))
eval
().environment
in which the expression should
be evaluated.numeric
specifying the maximum number
of seconds the expression is allowed to run before being
interrupted by the timeout. The cpu
and elapsed
arguments can bcharacter
specifying what action to take if
a timeout event occurs.NULL
is returned if onTimeout
was
"warning"
or "silent"
.
If "error"
a TimeoutException
is thrown.setTimeLimit
() by first setting the
timeout limits, then evaluating the expression that may or may not
timeout. The method is guaranteed to reset the timeout limits to be
infitely long upon exiting, regardless whether it returns normally
or preemptively due to a timeout or an error.setTimeLimit
()# - - - - - - - - - - - - - - - - - - - - - - - - -
# Function that takes "a long" time to run
# - - - - - - - - - - - - - - - - - - - - - - - - -
foo <- function() {
print("Tic");
for (kk in 1:100) {
print(kk);
Sys.sleep(0.1);
}
print("Tac");
}
# - - - - - - - - - - - - - - - - - - - - - - - - -
# Evaluate code, if it takes too long, generate
# a timeout by throwing a TimeoutException.
# - - - - - - - - - - - - - - - - - - - - - - - - -
res <- NULL;
tryCatch({
res <- evalWithTimeout({
foo();
}, timeout=1.08);
}, TimeoutException=function(ex) {
cat("Timeout. Skipping.
");
})
# - - - - - - - - - - - - - - - - - - - - - - - - -
# Evaluate code, if it takes too long, generate
# a timeout returning NULL and generate a warning.
# - - - - - - - - - - - - - - - - - - - - - - - - -
res <- evalWithTimeout({
foo();
}, timeout=1.08, onTimeout="warning");
# - - - - - - - - - - - - - - - - - - - - - - - - -
# Evaluate code, if it takes too long, generate
# a timeout, and return silently NULL.
# - - - - - - - - - - - - - - - - - - - - - - - - -
res <- evalWithTimeout({
foo();
}, timeout=1.08, onTimeout="silent");
Run the code above in your browser using DataLab