Skip to content

Commit

Permalink
add an S3 method for knitr::kable()
Browse files Browse the repository at this point in the history
  • Loading branch information
yihui committed Apr 29, 2024
1 parent 6ed01ef commit f97914d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 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.9
Version: 0.43.10
Authors@R: c(
person("Yihui", "Xie", role = c("aut", "cre", "cph"), email = "xie@yihui.name", comment = c(ORCID = "0000-0003-0645-5666")),
person("Wush", "Wu", role = "ctb"),
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ S3method(print,xfun_record_results)
S3method(print,xfun_rename_seq)
S3method(print,xfun_strict_list)
S3method(record_print,default)
S3method(record_print,knitr_kable)
export(Rcmd)
export(Rscript)
export(Rscript_call)
Expand Down
13 changes: 11 additions & 2 deletions R/record.R
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,9 @@ merge_record = function(x) {
#' An S3 generic function to be called to print visible values in code when the
#' code is recorded by [record()]. It is similar to [knitr::knit_print()]. By
#' default, it captures the normal [print()] output and returns the result as a
#' character vector. Users and package authors can define other S3 methods to
#' extend this function.
#' character vector. The `knitr_kable` method is for printing [knitr::kable()]
#' output. Users and package authors can define other S3 methods to extend this
#' function.
#' @param x The value to be printed.
#' @param ... Other arguments to be passed to `record_print()` methods.
#' @return A `record_print()` method should return a character vector. The
Expand All @@ -277,6 +278,14 @@ record_print.default = function(x, ...) {
capture.output(if (isS4(x)) methods::show(x, ...) else print(x, ...))
}

#' @rdname record_print
#' @export
record_print.knitr_kable = function(x, ...) {
if ((fmt <- attr(x, 'format')) %in% c('html', 'latex'))
x = fenced_block(x, paste0('=', fmt))
record_class(c(x, ''), 'asis')
}

#' @param class A class name, e.g., `asis`, `message`, `plot` (for the `plot`
#' class, `x` should be a vector of file paths).
#' @rdname record_print
Expand Down
8 changes: 6 additions & 2 deletions man/record_print.Rd

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

0 comments on commit f97914d

Please sign in to comment.