File: chunk_hook.Rd

package info (click to toggle)
r-cran-knitr 1.50%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,864 kB
  • sloc: makefile: 16; sh: 10; javascript: 8
file content (89 lines) | stat: -rw-r--r-- 3,511 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/hooks-extra.R
\name{hook_pdfcrop}
\alias{hook_pdfcrop}
\alias{hook_optipng}
\alias{hook_pngquant}
\alias{hook_mogrify}
\alias{hook_plot_custom}
\alias{hook_purl}
\title{Built-in chunk hooks to extend knitr}
\usage{
hook_pdfcrop(before, ...)

hook_optipng(...)

hook_pngquant(...)

hook_mogrify(...)

hook_plot_custom(before, options, envir)

hook_purl(before, options, ...)
}
\arguments{
\item{before, options, envir, ...}{See \emph{References} below.}
}
\description{
Hook functions are called when the corresponding chunk options are not
\code{NULL} 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 \pkg{knitr}.
}
\details{
The function \code{hook_pdfcrop()} calls \code{\link{plot_crop}()} to crop
the white margins of PDF plots.

The function \code{hook_optipng()} calls the program \command{optipng} to
optimize PNG images. Note the chunk option \code{optipng} can be used to
provide additional parameters to the program \command{optipng}, e.g.
\code{optipng = '-o7'}.

The function \code{hook_pngquant()} calls the program \command{pngquant} to
optimize PNG images. Note the chunk option \code{pngquant} can be used to
provide additional parameters to the program \command{pngquant}, e.g.
\code{pngquant = '--speed=1 --quality=0-50'}.

The function \code{hook_mogrify()} calls the program \command{mogrify}.  Note
the chunk option \code{mogrify} can be used to provide additional parameters
to the program \command{mogrify} (with default \code{-trim} to trim PNG
files).

When the plots are not recordable via \code{grDevices::\link{recordPlot}()}
and we save the plots to files manually via other functions (e.g. \pkg{rgl}
plots), we can use the chunk hook \code{hook_plot_custom} to help write code
for graphics output into the output document.

The hook \code{hook_purl()} can be used to write the code chunks to an R
script. It is an alternative approach to \code{\link{purl}}, and can be more
reliable when the code chunks depend on the execution of them (e.g.
\code{\link{read_chunk}()}, or \code{\link{opts_chunk}$set(eval = FALSE)}).
To enable this hook, it is recommended to associate it with the chunk option
\code{purl}, i.e. \code{knit_hooks$set(purl = hook_purl)}. When this hook is
enabled, an R script will be written while the input document is being
\code{\link{knit}}. Currently the code chunks that are not R code or have the
chunk option \code{purl=FALSE} are ignored. Please note when the cache is
turned on (the chunk option \code{cache = TRUE}), no chunk hooks will be
executed, hence \code{hook_purl()} will not work, either. To solve this
problem, we need \code{cache = 2} instead of \code{TRUE} (see
\url{https://yihui.org/knitr/demo/cache/} for the meaning of \code{cache =
2}).
}
\note{
The two hook functions \code{hook_rgl()} and \code{hook_webgl()} were
  moved from \pkg{knitr} to the \pkg{rgl} package (>= v0.95.1247) after
  \pkg{knitr} v1.10.5, and you can \code{library(rgl)} to get them.
}
\examples{
if (require("rgl") && exists("hook_rgl")) knit_hooks$set(rgl = hook_rgl)
# then in code chunks, use the option rgl=TRUE
}
\references{
\url{https://yihui.org/knitr/hooks/#chunk-hooks}
}
\seealso{
\code{rgl::\link[rgl:snapshot]{rgl.snapshot}},
  \code{rgl::\link[rgl:postscript]{rgl.postscript}},
  \code{rgl::\link[rgl]{hook_rgl}},
  \code{rgl::\link[rgl:hook_rgl]{hook_webgl}}
}