r - 如何在 knitr 和 RStudio 中为 word 和 html 设置不同的全局选项?

标签 r knitr rstudio

我正在使用 RStudio 0.98.932 和 knitr 1.6。想为 word 和 html 设置不同的全局 knitr 选项。例如,想将 fig.width 和 fig.height 设置为 word 为 6,html 为 11。

如果它可用于 rmd 文件的输出格式,我可以编写一些代码来切换设置。我该怎么做?感谢您的任何建议。

最佳答案

尝试将此代码块放在 Rmd 文档的开头。

```{r setup, cache=FALSE, include=FALSE}
library(knitr)
output <- opts_knit$get("rmarkdown.pandoc.to")
if (output=="html") opts_chunk$set(fig.width=11, fig.height=11)
if (output=="docx") opts_chunk$set(fig.width=6,  fig.height=6)
```
opts_knit$get() 返回的包选项之一是 markdown.pandoc.to .这显然设置为 "html" , "docx" , 或 "latex"取决于所选的输出格式(HTML、Word 或 PDF)。所以你可以测试它并设置 block 选项fig.widthfig.height因此。

关于r - 如何在 knitr 和 RStudio 中为 word 和 html 设置不同的全局选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25236850/

相关文章:

r - 编译 pdf 输出时knitr -pandoc-citeproc 错误

r - 在 R 中使用 forecast() 时获取预测值

r - 在R中将条形图转换为饼图

r - 结合训练+测试数据并在 R 中运行交叉验证

r - 渲染 rmarkdown 文档时保留辅助 TeX 文件

RStudio 未检测到 Rtools

r - 在格子图中的关键矩形内设置关键文本

r - 如何在 r markdown 中指定自定义纸张尺寸

open-source - 如何从 RStudio 的内置 R 控制台运行 R 脚本?

r - 在 Shiny R 中显示雷达图