我不仅希望我的数据出现在我的knitr生成的报告中,而且我还想将它们输出到单独的文件中。为此,我添加了如下代码:
```{r}
#Plot figure in report
plot(x,y)
#Plot figure in file
pdf(file="MyFig.pdf")
plot(x,y)
dev.off()
```
这工作得很好,但我希望有一个更优雅的解决方案已经内置于knitr中。是否有一个块选项或类似的东西可以达到相同的结果?
如果您使用
self_contained: no
,请使用选项
html_document
;如果您使用 keep_tex: yes
,请使用
pdf_document
选项,这样 rmarkdown 在渲染输出文档后不会删除图形文件。
关键字
dev='pdf'
正如 Yihui 所解释的那样 http://yihui.name/knitr/options/
与我发现有用的其他选项一起:
```{r 'setup', echo = FALSE, cache = FALSE}
knitr::opts_chunk$set(
dev = c('pdf', 'png'),
fig.align = 'center', fig.height = 5, fig.width = 8.5,
pdf.options(encoding = "ISOLatin9.enc"))
```
尝试将图形保存为文件: https://www.njtierney.com/post/2018/02/28/ Three-r-tips/
“提示 1:保存您创建的图像”