Debian Codegraph @

man/chunk_hook.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/hooks-extra.R
3 \name{hook_pdfcrop}
4 \alias{hook_pdfcrop}
5 \alias{hook_optipng}
6 \alias{hook_pngquant}
7 \alias{hook_mogrify}
8 \alias{hook_plot_custom}
9 \alias{hook_purl}
10 \title{Built-in chunk hooks to extend knitr}
11 \usage{
12 hook_pdfcrop(before, ...)
13
14 hook_optipng(...)
15
16 hook_pngquant(...)
17
18 hook_mogrify(...)
19
20 hook_plot_custom(before, options, envir)
21
22 hook_purl(before, options, ...)
23 }
24 \arguments{
25 \item{before, options, envir, ...}{See \emph{References} below.}
26 }
27 \description{
28 Hook functions are called when the corresponding chunk options are not
29 \code{NULL} to do additional jobs beside the R code in chunks. This package
30 provides a few useful hooks, which can also serve as examples of how to
31 define chunk hooks in \pkg{knitr}.
32 }
33 \details{
34 The function \code{hook_pdfcrop()} calls \code{\link{plot_crop}()} to crop
35 the white margins of PDF plots.
36
37 The function \code{hook_optipng()} calls the program \command{optipng} to
38 optimize PNG images. Note the chunk option \code{optipng} can be used to
39 provide additional parameters to the program \command{optipng}, e.g.
40 \code{optipng = '-o7'}.
41
42 The function \code{hook_pngquant()} calls the program \command{pngquant} to
43 optimize PNG images. Note the chunk option \code{pngquant} can be used to
44 provide additional parameters to the program \command{pngquant}, e.g.
45 \code{pngquant = '--speed=1 --quality=0-50'}.
46
47 The function \code{hook_mogrify()} calls the program \command{mogrify}. Note
48 the chunk option \code{mogrify} can be used to provide additional parameters
49 to the program \command{mogrify} (with default \code{-trim} to trim PNG
50 files).
51
52 When the plots are not recordable via \code{grDevices::\link{recordPlot}()}
53 and we save the plots to files manually via other functions (e.g. \pkg{rgl}
54 plots), we can use the chunk hook \code{hook_plot_custom} to help write code
55 for graphics output into the output document.
56
57 The hook \code{hook_purl()} can be used to write the code chunks to an R
58 script. It is an alternative approach to \code{\link{purl}}, and can be more
59 reliable when the code chunks depend on the execution of them (e.g.
60 \code{\link{read_chunk}()}, or \code{\link{opts_chunk}$set(eval = FALSE)}).
61 To enable this hook, it is recommended to associate it with the chunk option
62 \code{purl}, i.e. \code{knit_hooks$set(purl = hook_purl)}. When this hook is
63 enabled, an R script will be written while the input document is being
64 \code{\link{knit}}. Currently the code chunks that are not R code or have the
65 chunk option \code{purl=FALSE} are ignored. Please note when the cache is
66 turned on (the chunk option \code{cache = TRUE}), no chunk hooks will be
67 executed, hence \code{hook_purl()} will not work, either. To solve this
68 problem, we need \code{cache = 2} instead of \code{TRUE} (see
69 \url{https://yihui.org/knitr/demo/cache/} for the meaning of \code{cache =
70 2}).
71 }
72 \note{
73 The two hook functions \code{hook_rgl()} and \code{hook_webgl()} were
74 moved from \pkg{knitr} to the \pkg{rgl} package (>= v0.95.1247) after
75 \pkg{knitr} v1.10.5, and you can \code{library(rgl)} to get them.
76 }
77 \examples{
78 if (require("rgl") && exists("hook_rgl")) knit_hooks$set(rgl = hook_rgl)
79 # then in code chunks, use the option rgl=TRUE
80 }
81 \references{
82 \url{https://yihui.org/knitr/hooks/#chunk-hooks}
83 }
84 \seealso{
85 \code{rgl::\link[rgl:snapshot]{rgl.snapshot}},
86 \code{rgl::\link[rgl:postscript]{rgl.postscript}},
87 \code{rgl::\link[rgl]{hook_rgl}},
88 \code{rgl::\link[rgl:hook_rgl]{hook_webgl}}
89 }