第 1 章 R Markdown
1.1 设置选项钩子附上图片下载链接
通过设置选项钩子,可以在图片输出设备多于 1 个时候,提供图片的下载链接。
knitr::opts_hooks$set(dev = function(options){
if (length(options$dev) > 1){
x = paste0("[", options$dev, "](", knitr::fig_path(options$dev), ")",
collapse = " | ")
options$fig.cap = paste(options$fig.cap, x, sep = " ")
}
options
})
下图中,使用了 dev = c('png', 'pdf', "tiff", "svg")
等 4 种图像设备,由于 dev
选项钩子的存在,将自动在图注中添加图片的下载链接。
plot(1:10)
图注也可以使用文本引用,不影响钩子的应用效果。
plot(1:10)
1.2 使用自定义图形设备
All possible devices supported by knitr are: “bmp”, “postscript”, “pdf”, “png”, “svg”, “jpeg”, “pictex”, “tiff”, “win.metafile”, “cairo_pdf”, “cairo_ps”, “quartz_pdf”, “quartz_png”, “quartz_jpeg”, “quartz_tiff”, “quartz_gif”, “quartz_psd”, “quartz_bmp”, “CairoJPEG”, “CairoPNG”, “CairoPS”, “CairoPDF”, “CairoSVG”, “CairoTIFF”, “Cairo_pdf”, “Cairo_png”, “Cairo_ps”, “Cairo_svg”, “svglite”, “ragg_png”, and “tikz”.
From: https://bookdown.org/yihui/rmarkdown-cookbook/graphical-device.html
是不是可以使用 ppt
设备呢?
library(export)
The dev
option can also be the name of a function. If so, the function seems like function(file, width, height)
, which has been described at https://yihui.org/knitr/options/#plots.
Therefore, a new PPTX device can be defined as:
# a pptx device
pptx = function(file, width, height){
export::graph2ppt(file = file, width = width, height = height)
}
Notably, if multiple devices are used, you may need to specify the fig.ext
option. For example, dev = c('png', 'pdf', 'pptx'), fig.ext=c("png","pdf","pptx")
. See https://yihui.org/knitr/demo/graphics/#a-note-on-custom-graphical-devices.
plot(1:10)