|
7 | 7 | #' * `"none"`: Just runs the call in the current session and measures the elapsed time.
|
8 | 8 | #' Does not keep a log, output is printed directly to the console.
|
9 | 9 | #' Works well together with [traceback()].
|
| 10 | +#' * `"try-catch"`: Similar to `"none"`, but catches error. Output is printed to the console and |
| 11 | +#' not logged. |
10 | 12 | #' * `"evaluate"`: Uses the package \CRANpkg{evaluate} to call the function, measure time and do the logging.
|
11 | 13 | #' * `"callr"`: Uses the package \CRANpkg{callr} to call the function, measure time and do the logging.
|
12 | 14 | #' This encapsulation spawns a separate R session in which the function is called.
|
|
54 | 56 | encapsulate = function(method, .f, .args = list(), .opts = list(), .pkgs = character(),
|
55 | 57 | .seed = NA_integer_, .timeout = Inf) {
|
56 | 58 |
|
57 |
| - assert_choice(method, c("none", "evaluate", "callr")) |
| 59 | + assert_choice(method, c("none", "try-catch", "evaluate", "callr")) |
58 | 60 | assert_list(.args, names = "unique")
|
59 | 61 | assert_list(.opts, names = "unique")
|
60 | 62 | assert_character(.pkgs, any.missing = FALSE)
|
61 | 63 | assert_count(.seed, na.ok = TRUE)
|
62 | 64 | assert_number(.timeout, lower = 0)
|
63 | 65 | log = NULL
|
64 | 66 |
|
65 |
| - if (method == "none") { |
| 67 | + if (method %in% c("none", "try-catch")) { |
66 | 68 | require_namespaces(.pkgs)
|
67 | 69 |
|
68 | 70 | now = proc.time()[[3L]]
|
69 |
| - result = invoke(.f, .args = .args, .opts = .opts, .seed = .seed, .timeout = .timeout) |
| 71 | + if (method == "none") { |
| 72 | + result = invoke(.f, .args = .args, .opts = .opts, .seed = .seed, .timeout = .timeout) |
| 73 | + } else { |
| 74 | + result = try(invoke(.f, .args = .args, .opts = .opts, .seed = .seed, .timeout = .timeout)) |
| 75 | + if (inherits(result, "try-error")) { |
| 76 | + result = NULL |
| 77 | + } |
| 78 | + } |
70 | 79 | elapsed = proc.time()[[3L]] - now
|
71 | 80 | } else if (method == "evaluate") {
|
72 | 81 | require_namespaces(c("evaluate", .pkgs))
|
|
0 commit comments