EVOLUTION-MANAGER
Edit File: chunk_hook.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: Built-in chunk hooks to extend knitr</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 hook_pdfcrop {knitr}"><tr><td>hook_pdfcrop {knitr}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Built-in chunk hooks to extend knitr</h2> <h3>Description</h3> <p>Hook functions are called when the corresponding chunk options are not <code>NULL</code> to do additional jobs beside the R code in chunks. This package provides a few useful hooks, which can also serve as examples of how to define chunk hooks in <span class="pkg">knitr</span>. </p> <h3>Usage</h3> <pre> hook_pdfcrop(before, options, envir) hook_optipng(before, options, envir) hook_pngquant(before, options, envir) hook_mogrify(before, options, envir) hook_plot_custom(before, options, envir) hook_purl(before, options, envir) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>before, options, envir</code></td> <td> <p>See <em>References</em> below.</p> </td></tr> </table> <h3>Details</h3> <p>The function <code>hook_pdfcrop()</code> can use the program <code>pdfcrop</code> to crop the extra white margin when the plot format is PDF to make better use of the space in the output document, otherwise we often have to struggle with <code>graphics::<a href="../../graphics/html/par.html">par</a>()</code> to set appropriate margins. Note <code>pdfcrop</code> often comes with a LaTeX distribution such as MiKTeX or TeXLive, and you may not need to install it separately (use <code>Sys.which('pdfcrop')</code> to check it; if it not empty, you are able to use it). Similarly, when the plot format is not PDF (e.g. PNG), the <span class="pkg">magick</span> package is used to crop the plot. </p> <p>The function <code>hook_optipng()</code> calls the program <code>optipng</code> to optimize PNG images. Note the chunk option <code>optipng</code> can be used to provide additional parameters to the program <code>optipng</code>, e.g. <code>optipng = '-o7'</code>. </p> <p>The function <code>hook_pngquant()</code> calls the program <code>pngquant</code> to optimize PNG images. Note the chunk option <code>pngquant</code> can be used to provide additional parameters to the program <code>pngquant</code>, e.g. <code>pngquant = '--speed=1 --quality=0-50'</code>. </p> <p>The function <code>hook_mogrify()</code> calls the program <code>mogrify</code>. Note the chunk option <code>mogrify</code> can be used to provide additional parameters to the program <code>mogrify</code> (with default <code>-trim</code> to trim PNG files). </p> <p>When the plots are not recordable via <code>grDevices::<a href="../../grDevices/html/recordplot.html">recordPlot</a>()</code> and we save the plots to files manually via other functions (e.g. <span class="pkg">rgl</span> plots), we can use the chunk hook <code>hook_plot_custom</code> to help write code for graphics output into the output document. </p> <p>The hook <code>hook_purl()</code> can be used to write the code chunks to an R script. It is an alternative approach to <code><a href="knit.html">purl</a></code>, and can be more reliable when the code chunks depend on the execution of them (e.g. <code><a href="read_chunk.html">read_chunk</a>()</code>, or <code><a href="opts_chunk.html">opts_chunk</a>$set(eval = FALSE)</code>). To enable this hook, it is recommended to associate it with the chunk option <code>purl</code>, i.e. <code>knit_hooks$set(purl = hook_purl)</code>. When this hook is enabled, an R script will be written while the input document is being <code><a href="knit.html">knit</a></code>. Currently the code chunks that are not R code or have the chunk option <code>purl=FALSE</code> are ignored. Please note when the cache is turned on (the chunk option <code>cache = TRUE</code>), no chunk hooks will be executed, hence <code>hook_purl()</code> will not work, either. To solve this problem, we need <code>cache = 2</code> instead of <code>TRUE</code> (see <a href="https://yihui.org/knitr/demo/cache/">https://yihui.org/knitr/demo/cache/</a> for the meaning of <code>cache = 2</code>). </p> <h3>Note</h3> <p>The two hook functions <code>hook_rgl()</code> and <code>hook_webgl()</code> were moved from <span class="pkg">knitr</span> to the <span class="pkg">rgl</span> package (>= v0.95.1247) after <span class="pkg">knitr</span> v1.10.5, and you can <code>library(rgl)</code> to get them. </p> <h3>References</h3> <p><a href="https://yihui.org/knitr/hooks/#chunk_hooks">https://yihui.org/knitr/hooks/#chunk_hooks</a> </p> <h3>See Also</h3> <p><code>rgl::<a href="../../rgl/html/snapshot.html">rgl.snapshot</a></code>, <code>rgl::<a href="../../rgl/html/postscript.html">rgl.postscript</a></code>, <code>rgl::<a href="../../rgl/html/hook_rgl.html">hook_rgl</a></code>, <code>rgl::<a href="../../rgl/html/hook_rgl.html">hook_webgl</a></code> </p> <h3>Examples</h3> <pre> if (require("rgl") && exists("hook_rgl")) knit_hooks$set(rgl = hook_rgl) # then in code chunks, use the option rgl=TRUE </pre> <hr /><div style="text-align: center;">[Package <em>knitr</em> version 1.29 <a href="00Index.html">Index</a>]</div> </body></html>