Skip to content

Commit

Permalink
add arguments message and warning to record(), and change the `er…
Browse files Browse the repository at this point in the history
…ror `argument
  • Loading branch information
yihui committed Apr 29, 2024
1 parent ef3d2d7 commit a5b3bd6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

- Added an argument `cache` to `record()` to make it possible to enable caching.

- Added arguments `message` and `warning` to `record()` to decide whether messages and warnings should be recorded.

- Changed the default value of the argument `error` of `record()` from `FALSE` to `NA`. Now `FALSE` means to suppress error messages, and `NA` means to throw errors normally. This is for consistency with the `message` and `warning` arguments.

- Exported the internal function `md5()` to calculate the MD5 checksums of R objects. The function is essentially a workaround for `tools::md5sum()` (see HenrikBengtsson/Wishlist-for-R#21).

- `write_utf8()` returns the `con` argument (typically a file path) now. Previously, it returns `NULL`.
Expand Down
24 changes: 13 additions & 11 deletions R/record.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
#' arguments are `list(units = 'in', onefile = FALSE, width = 7, height = 7,
#' res = 96)`. If any of these arguments is not present in the device
#' function, it will be dropped.
#' @param error Whether to record errors. If `TRUE`, errors will not stop the
#' execution and error messages will be recorded. If `FALSE`, errors will be
#' thrown normally.
#' @param message,warning,error If `TRUE`, record and store messages / warnings
#' / errors in the output. If `FALSE`, suppress them. If `NA`, do not process
#' them (messages will be emitted to the console, and errors will halt the
#' execution).
#' @param cache A list of options for caching. See the `path`, `id`, and `...`
#' arguments of [cache_exec()].
#' @param verbose `2` means to always print the value of each expression in the
Expand All @@ -42,8 +43,8 @@
#' plots = Filter(function(x) inherits(x, 'record_plot'), res)
#' file.remove(unlist(plots))
record = function(
code = NULL, dev = 'png', dev.path = 'xfun-record',
dev.ext = dev_ext(dev), dev.args = list(), error = FALSE, cache = list(),
code = NULL, dev = 'png', dev.path = 'xfun-record', dev.ext = dev_ext(dev),
dev.args = list(), message = TRUE, warning = TRUE, error = NA, cache = list(),
verbose = getOption('xfun.record.verbose', 0), envir = parent.frame()
) {
new_record = function(x = list()) structure(x, class = 'xfun_record_results')
Expand Down Expand Up @@ -161,7 +162,7 @@ record = function(
}
})

handle = if (error) try_silent else identity
handle = if (is.na(error)) identity else try_silent
# split code into individual expressions
codes = handle(split_source(code, merge_comments = TRUE, line_number = TRUE))
# code may contain syntax errors
Expand All @@ -170,16 +171,17 @@ record = function(
return(new_record(res))
}

handle_message = function(type) {
handle_message = function(type, add = TRUE) {
mf = sub('^(.)', 'muffle\\U\\1', type, perl = TRUE)
function(e) {
add_result(e$message, type)
if (is.na(add)) return()
if (isTRUE(add)) add_result(e$message, type)
if (type %in% c('message', 'warning')) invokeRestart(mf)
}
}
handle_m = handle_message('message')
handle_w = handle_message('warning')
handle_e = handle_message('error')
handle_m = handle_message('message', message)
handle_w = handle_message('warning', warning)
handle_e = handle_message('error', error)

n = length(codes)
for (i in seq_len(n)) {
Expand Down
11 changes: 7 additions & 4 deletions man/record.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a5b3bd6

Please sign in to comment.