Debian Codegraph @

man/knit_print.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/output.R
3 \name{knit_print}
4 \alias{knit_print}
5 \alias{normal_print}
6 \title{A custom printing function}
7 \usage{
8 knit_print(x, ...)
9
10 normal_print(x, ...)
11 }
12 \arguments{
13 \item{x}{An R object to be printed}
14
15 \item{...}{Additional arguments passed to the S3 method. Currently ignored,
16 except two optional arguments \code{options} and \code{inline}; see
17 the references below.}
18 }
19 \value{
20 The value returned from the print method should be a character vector
21 or can be converted to a character value. You can wrap the value in
22 \code{\link{asis_output}()} so that \pkg{knitr} writes the character value
23 as is in the output.
24 }
25 \description{
26 The S3 generic function \code{knit_print} is the default printing function in
27 \pkg{knitr}. The chunk option \code{render} uses this function by default.
28 The main purpose of this S3 generic function is to customize printing of R
29 objects in code chunks. We can fall back to the normal printing behavior by
30 setting the chunk option \code{render = normal_print}.
31 }
32 \details{
33 Users can write custom methods based on this generic function. For example,
34 if we want to print all data frames as tables in the output, we can define a
35 method \code{knit_print.data.frame} that turns a data.frame into a table (the
36 implementation may use other R packages or functions, e.g. \pkg{xtable} or
37 \code{\link{kable}()}).
38 }
39 \note{
40 It is recommended to leave a \code{...} argument in your method, to
41 allow future changes of the \code{knit_print()} API without breaking your
42 method.
43 }
44 \examples{
45 library(knitr)
46 # write tables for data frames
47 knit_print.data.frame = function(x, ...) {
48 res = paste(c("", "", kable(x, output = FALSE)), collapse = "\n")
49 asis_output(res)
50 }
51 # register the method
52 registerS3method("knit_print", "data.frame", knit_print.data.frame)
53 # after you define and register the above method, data frames will be printed
54 # as tables in knitr, which is different with the default print() behavior
55 }
56 \references{
57 See \code{vignette('knit_print', package = 'knitr')}.
58 }