Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

forkTimeout

Evaluate an R expression with a hard time limit by forking a process


Description

This function uses parallel::mcparallel(), so the time limit is not enforced on Windows. However, unlike functions using setTimeLimit(), the time limit is enforced even on native code.

Usage

forkTimeout(
  expr,
  timeout,
  unsupported = c("warning", "error", "message", "silent"),
  onTimeout = NULL
)

Arguments

expr

expression to be evaluated.

timeout

number of seconds to wait for the expression to evaluate.

unsupported

a character vector of length 1 specifying how to handle a platform that does not support parallel::mcparallel(),

"warning" or "message"

Issue a warning or a message, respectively, then evaluate the expression without the time limit enforced.

"error"

Stop with an error.

"silent"

Evaluate the expression without the time limit enforced, without any notice.

Partial matching is used.

onTimeout

Value to be returned on time-out.

Value

Result of evaluating expr if completed, onTimeout otherwise.

Note

onTimeout can itself be an expression, so it is, for example, possible to stop with an error by passing onTimeout=stop().

Note that this function is not completely transparent: side-effects may behave in unexpected ways. In particular, RNG state will not be updated.

Examples

forkTimeout({Sys.sleep(1); TRUE}, 2) # TRUE
forkTimeout({Sys.sleep(1); TRUE}, 0.5) # NULL (except on Windows)

statnet.common

Common R Scripts and Utilities Used by the Statnet Project Software

v4.4.1
GPL-3 + file LICENSE
Authors
Pavel N. Krivitsky [aut, cre] (<https://orcid.org/0000-0002-9101-3362>), Skye Bender-deMoll [ctb]
Initial release
2020-10-03

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.