EVOLUTION-MANAGER
Edit File: knit_print.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: A custom printing function</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for knit_print {knitr}"><tr><td>knit_print {knitr}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>A custom printing function</h2> <h3>Description</h3> <p>The S3 generic function <code>knit_print</code> is the default printing function in <span class="pkg">knitr</span>. The chunk option <code>render</code> 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 <code>render = normal_print</code>. </p> <h3>Usage</h3> <pre> knit_print(x, ...) normal_print(x, ...) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>x</code></td> <td> <p>An R object to be printed</p> </td></tr> <tr valign="top"><td><code>...</code></td> <td> <p>Additional arguments passed to the S3 method. Currently ignored, except two optional arguments <code>options</code> and <code>inline</code>; see the references below.</p> </td></tr> </table> <h3>Details</h3> <p>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 <code>knit_print.data.frame</code> that turns a data.frame into a table (the implementation may use other R packages or functions, e.g. <span class="pkg">xtable</span> or <code><a href="kable.html">kable</a>()</code>). </p> <h3>Value</h3> <p>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 <code><a href="asis_output.html">asis_output</a>()</code> so that <span class="pkg">knitr</span> writes the character value as is in the output. </p> <h3>Note</h3> <p>It is recommended to leave a <code>...</code> argument in your method, to allow future changes of the <code>knit_print()</code> API without breaking your method. </p> <h3>References</h3> <p>See <code>vignette('knit_print', package = 'knitr')</code>. </p> <h3>Examples</h3> <pre> 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) } # register the method registerS3method("knit_print", "data.frame", knit_print.data.frame) # after you define and register the above method, data frames will be printed as # tables in knitr, which is different with the default print() behavior </pre> <hr /><div style="text-align: center;">[Package <em>knitr</em> version 1.29 <a href="00Index.html">Index</a>]</div> </body></html>