Description Usage Arguments Details Value References Examples
The S3 generic function knit_print
is the default printing function in
knitr. The chunk option render
uses this function by default.
The main purpose of this S3 generic function is to customize printing of R
objects in code chunks. We can fall back to the normal printing behavior by
setting the chunk option render = normal_print
.
1 |
x |
an R object to be printed |
... |
additional arguments passed to the S3 method (currently ignored,
except two optional arguments |
Users can write custom methods based on this generic function. For example,
if we want to print all data frames as tables in the output, we can define a
method knit_print.data.frame
that turns a data.frame into a table (the
implementation may use other R packages or functions, e.g. xtable or
kable()
).
The value returned from the print method should be a character vector
or can be converted to a character value. You can wrap the value in
asis_output()
so that knitr writes the character value
as is in the output.
See vignette('knit_print', package = 'knitr')
.
1 2 3 4 5 6 7 8 | library(knitr)
# write tables for data frames
knit_print.data.frame = function(x, ...) {
res = paste(c("", "", kable(x, output = FALSE)), collapse = "\n")
asis_output(res)
}
# after you defined the above method, data frames will be printed as tables in
# knitr, which is different with the default print() behavior
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.