| 1 |
#' Built-in chunk hooks to extend knitr |
| 2 |
#' |
| 3 |
#' Hook functions are called when the corresponding chunk options are not |
| 4 |
#' \code{NULL} to do additional jobs beside the R code in chunks. This package |
| 5 |
#' provides a few useful hooks, which can also serve as examples of how to |
| 6 |
#' define chunk hooks in \pkg{knitr}. |
| 7 |
#' |
| 8 |
#' The function \code{hook_pdfcrop()} calls \code{\link{plot_crop}()} to crop |
| 9 |
#' the white margins of PDF plots. |
| 10 |
#' |
| 11 |
#' The function \code{hook_optipng()} calls the program \command{optipng} to |
| 12 |
#' optimize PNG images. Note the chunk option \code{optipng} can be used to |
| 13 |
#' provide additional parameters to the program \command{optipng}, e.g. |
| 14 |
#' \code{optipng = '-o7'}. |
| 15 |
#' |
| 16 |
#' The function \code{hook_pngquant()} calls the program \command{pngquant} to |
| 17 |
#' optimize PNG images. Note the chunk option \code{pngquant} can be used to |
| 18 |
#' provide additional parameters to the program \command{pngquant}, e.g. |
| 19 |
#' \code{pngquant = '--speed=1 --quality=0-50'}. |
| 20 |
#' |
| 21 |
#' The function \code{hook_mogrify()} calls the program \command{mogrify}. Note |
| 22 |
#' the chunk option \code{mogrify} can be used to provide additional parameters |
| 23 |
#' to the program \command{mogrify} (with default \code{-trim} to trim PNG |
| 24 |
#' files). |
| 25 |
#' |
| 26 |
#' When the plots are not recordable via \code{grDevices::\link{recordPlot}()} |
| 27 |
#' and we save the plots to files manually via other functions (e.g. \pkg{rgl} |
| 28 |
#' plots), we can use the chunk hook \code{hook_plot_custom} to help write code |
| 29 |
#' for graphics output into the output document. |
| 30 |
#' |
| 31 |
#' The hook \code{hook_purl()} can be used to write the code chunks to an R |
| 32 |
#' script. It is an alternative approach to \code{\link{purl}}, and can be more |
| 33 |
#' reliable when the code chunks depend on the execution of them (e.g. |
| 34 |
#' \code{\link{read_chunk}()}, or \code{\link{opts_chunk}$set(eval = FALSE)}). |
| 35 |
#' To enable this hook, it is recommended to associate it with the chunk option |
| 36 |
#' \code{purl}, i.e. \code{knit_hooks$set(purl = hook_purl)}. When this hook is |
| 37 |
#' enabled, an R script will be written while the input document is being |
| 38 |
#' \code{\link{knit}}. Currently the code chunks that are not R code or have the |
| 39 |
#' chunk option \code{purl=FALSE} are ignored. Please note when the cache is |
| 40 |
#' turned on (the chunk option \code{cache = TRUE}), no chunk hooks will be |
| 41 |
#' executed, hence \code{hook_purl()} will not work, either. To solve this |
| 42 |
#' problem, we need \code{cache = 2} instead of \code{TRUE} (see |
| 43 |
#' \url{https://yihui.org/knitr/demo/cache/} for the meaning of \code{cache = |
| 44 |
#' 2}). |
| 45 |
#' @rdname chunk_hook |
| 46 |
#' @param before,options,envir,... See \emph{References} below. |
| 47 |
#' @references \url{https://yihui.org/knitr/hooks/#chunk-hooks} |
| 48 |
#' @seealso \code{rgl::\link[rgl:snapshot]{rgl.snapshot}}, |
| 49 |
#' \code{rgl::\link[rgl:postscript]{rgl.postscript}}, |
| 50 |
#' \code{rgl::\link[rgl]{hook_rgl}}, |
| 51 |
#' \code{rgl::\link[rgl:hook_rgl]{hook_webgl}} |
| 52 |
#' @note The two hook functions \code{hook_rgl()} and \code{hook_webgl()} were |
| 53 |
#' moved from \pkg{knitr} to the \pkg{rgl} package (>= v0.95.1247) after |
| 54 |
#' \pkg{knitr} v1.10.5, and you can \code{library(rgl)} to get them. |
| 55 |
#' @export |
| 56 |
#' @examples if (require('rgl') && exists('hook_rgl')) knit_hooks$set(rgl = hook_rgl) |
| 57 |
#' # then in code chunks, use the option rgl=TRUE |
| 58 |
hook_pdfcrop = function(before, ...) { |
| 59 |
# crops plots after a chunk is evaluated and plot files produced |
| 60 |
if (before) return() |
| 61 |
in_base_dir(for (f in get_plot_files()) plot_crop(f)) |
| 62 |
} |
| 63 |
|
| 64 |
get_plot_files = function() { |
| 65 |
unique(opts_knit$get('plot_files')) |
| 66 |
} |
| 67 |
|
| 68 |
#' @export |
| 69 |
#' @rdname chunk_hook |
| 70 |
hook_optipng = function(...) hook_png(..., cmd = 'optipng') |
| 71 |
|
| 72 |
hook_png = function( |
| 73 |
before, options, ..., cmd = c('optipng', 'pngquant', 'mogrify'), post_process = identity |
| 74 |
) { |
| 75 |
if (before) return() |
| 76 |
cmd = match.arg(cmd) |
| 77 |
if (!nzchar(Sys.which(cmd))) { |
| 78 |
warning('cannot find ', cmd, '; please install and put it in PATH'); return() |
| 79 |
} |
| 80 |
opts = options[[cmd]] |
| 81 |
if (isFALSE(opts)) return() |
| 82 |
if (is.null(opts) || isTRUE(opts)) opts = switch( |
| 83 |
cmd, optipng = '-quiet', pngquant = '--skip-if-larger', mogrify = '-trim' |
| 84 |
) |
| 85 |
if (cmd == 'pngquant') opts = c(opts, '--ext', '-fs8.png') |
| 86 |
|
| 87 |
paths = get_plot_files() |
| 88 |
paths = grep('[.]png$', paths, ignore.case = TRUE, value = TRUE) |
| 89 |
|
| 90 |
in_base_dir( |
| 91 |
lapply(paths, function(x) { |
| 92 |
system2(cmd, c(opts, shQuote(x))) |
| 93 |
post_process(x) |
| 94 |
}) |
| 95 |
) |
| 96 |
return() |
| 97 |
} |
| 98 |
|
| 99 |
#' @export |
| 100 |
#' @rdname chunk_hook |
| 101 |
hook_pngquant = function(...) { |
| 102 |
hook_png(..., cmd = 'pngquant', post_process = function(x) { |
| 103 |
# pngquant creates an output file with '-fs8.png' as the extension. |
| 104 |
x2 = sub("\\.png$", "-fs8.png", x) |
| 105 |
if (file.exists(x2)) file.rename(x2, x) |
| 106 |
}) |
| 107 |
} |
| 108 |
|
| 109 |
#' @export |
| 110 |
#' @rdname chunk_hook |
| 111 |
hook_mogrify = function(...) hook_png(..., cmd = 'mogrify') |
| 112 |
|
| 113 |
#' @export |
| 114 |
#' @rdname chunk_hook |
| 115 |
hook_plot_custom = function(before, options, envir){ |
| 116 |
if (before) return() # run hook after the chunk |
| 117 |
if (options$fig.show == 'hide') return() # do not show figures |
| 118 |
|
| 119 |
ext = dev2ext(options) |
| 120 |
hook = knit_hooks$get('plot') |
| 121 |
|
| 122 |
n = options$fig.num |
| 123 |
if (n == 0L) n = options$fig.num = 1L # make sure fig.num is at least 1 |
| 124 |
res = unlist(lapply(seq_len(n), function(i) { |
| 125 |
options$fig.cur = i |
| 126 |
hook(fig_path(ext, options, i), reduce_plot_opts(options)) |
| 127 |
}), use.names = FALSE) |
| 128 |
paste(res, collapse = '') |
| 129 |
} |
| 130 |
|
| 131 |
#" a hook function to write out code from chunks |
| 132 |
#' @export |
| 133 |
#' @rdname chunk_hook |
| 134 |
hook_purl = function(before, options, ...) { |
| 135 |
# at the moment, non-R chunks are ignored; it is unclear what I should do |
| 136 |
if (before || !options$purl || options$engine != 'R') return() |
| 137 |
|
| 138 |
output = .knitEnv$tangle.file |
| 139 |
if (isFALSE(.knitEnv$tangle.start)) { |
| 140 |
.knitEnv$tangle.start = TRUE |
| 141 |
unlink(output) |
| 142 |
# write out knit_params() data from YAML |
| 143 |
params = .knitEnv$tangle.params |
| 144 |
if (length(params)) write_utf8(params, output) |
| 145 |
.knitEnv$tangle.params = NULL |
| 146 |
} |
| 147 |
|
| 148 |
# `options` contains merged chunk options, but we need to check if |
| 149 |
# `error=TRUE` in local chunk options, so retrieve options from knit_code |
| 150 |
error = attr(knit_code$get(options$label), 'chunk_opts')[['error']] |
| 151 |
code = tangle_mask(options$code, options$eval, error) |
| 152 |
if (is.character(output)) { |
| 153 |
code = c( |
| 154 |
if (file.exists(output)) read_utf8(output), |
| 155 |
label_code(code, options) |
| 156 |
) |
| 157 |
write_utf8(code, output) |
| 158 |
} |
| 159 |
invisible() |
| 160 |
} |