Skip to content

Commit

Permalink
add arguments print and print.args to record()
Browse files Browse the repository at this point in the history
  • Loading branch information
yihui committed May 6, 2024
1 parent 0409e9a commit 98ced94
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 3 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: xfun
Type: Package
Title: Supporting Functions for Packages Maintained by 'Yihui Xie'
Version: 0.43.14
Version: 0.43.15
Authors@R: c(
person("Yihui", "Xie", role = c("aut", "cre", "cph"), email = "[email protected]", comment = c(ORCID = "0000-0003-0645-5666")),
person("Wush", "Wu", role = "ctb"),
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

- Added an S3 generic function `record_print()`, which is similar to `knitr::knit_print()` but for the purpose of printing visible values in `record()`.

- The `record()` funciton gained new arguments `print` and `print.args` to support custom printing functions and arguments.

- Added a function `md_table()`, which is a minimal Markdown table generator.

- 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).
Expand Down
15 changes: 14 additions & 1 deletion R/record.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@
#' execution).
#' @param cache A list of options for caching. See the `path`, `id`, and `...`
#' arguments of [cache_exec()].
#' @param print A (typically S3) function that takes the value of an expression
#' in the code as input and returns output. The default is [record_print()].
#' @param print.args A list of arguments for the `print` function. By default,
#' the whole list is not passed directly to the function, but only an element
#' in the list with a name identical to the first class name of the returned
#' value of the expression, e.g., `list(data.frame = list(digits = 3), matrix
#' = list())`. This makes it possible to apply different print arguments to
#' objects of different classes. If the whole list is intended to be passed to
#' the print function directly, wrap the list in [I()].
#' @param verbose `2` means to always print the value of each expression in the
#' code, no matter if the value is [invisible()] or not; `1` means to always
#' print the value of the last expression; `0` means no special handling
Expand All @@ -45,6 +54,7 @@
record = function(
code = NULL, dev = 'png', dev.path = 'xfun-record', dev.ext = dev_ext(dev),
dev.args = list(), message = TRUE, warning = TRUE, error = NA, cache = list(),
print = record_print, print.args = list(),
verbose = getOption('xfun.record.verbose', 0), envir = parent.frame()
) {
new_result = function(x = list()) structure(x, class = 'xfun_record_results')
Expand Down Expand Up @@ -219,7 +229,10 @@ record = function(
out = handle_output(handle_eval(withVisible(eval(expr, envir))))
# print value (via record_print()) if visible
if (!is_error(out) && out$visible) {
out = handle_eval(record_print(out$value))
p_args = print.args
# index print args by first class of out unless args are wrapped in I()
if (!inherits(print.args, 'AsIs')) p_args = p_args[[class(out)[1]]]
out = handle_eval(do.call(print, c(list(out$value), p_args)))
if (length(out) && !is_error(out)) {
if (is.list(out)) lapply(out, add_result) else add_result(out)
}
Expand Down
8 changes: 7 additions & 1 deletion man/fenced_block.Rd

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

12 changes: 12 additions & 0 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 98ced94

Please sign in to comment.