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 hook_rgl(before, options, envir)hook_pdfcrop(before, options, envir)
hook_optipng(before, options, envir)
hook_plot_custom(before, options, envir)
hook_webgl(before, options, envir)
hook_purl(before, options, envir)
hook_rgl()
can be set as a hook in dev
(graphical device), plots can be save to different formats
(postscript
: eps; pdf
: pdf; other devices
correspond to the default PNG format). The plot window will be adjusted
according to chunk options fig.width
and fig.height
. Filenames
are derived from chunk labels and the fig.path
option.The function hook_webgl()
is a wrapper for the
writeWebGL()
function in the
The function hook_pdfcrop()
can use the program par
to set appropriate margins. Note
Sys.which('pdfcrop')
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 program
The function hook_optipng()
calls the program optipng
can be used to
provide additional parameters to the program optipng = '-o7'
. See
When the plots are not recordable via recordPlot
and
we save the plots to files manually via other functions (e.g. hook_plot_custom
to help write code
for graphics output into the output document.
The hook hook_purl()
can be used to write the code chunks to an R
script. It is an alternative approach to purl
, and can be more
reliable when the code chunks depend on the execution of them (e.g.
read_chunk()
, or opts_chunk$set(eval = FALSE)
).
To enable this hook, it is recommended to associate it with the chunk option
purl
, i.e. knit_hooks$set(purl = hook_purl)
. When this hook is
enabled, an R script will be written while the input document is being
knit
. Currently the code chunks that are not R code or have the
chunk option purl=FALSE
are ignored. Please note when the cache is
turned on (the chunk option cache = TRUE
), no chunk hooks will be
executed, hence hook_purl()
will not work, either. To solve this
problem, we need cache = 2
instead of TRUE
(see
cache =
2
).
rgl.snapshot
, rgl.postscript
knit_hooks$set(rgl = hook_rgl)
# then in code chunks, use the option rgl=TRUE
Run the code above in your browser using DataLab