如何在knitr和RStudio中为word和html设置不同的全局选项? [英] How to set different global options in knitr and RStudio for word and html?
问题描述
我正在使用RStudio 0.98.932和knitr 1.6.想要为word和html设置不同的全局编织选项.例如,要将word的fig.width和fig.height设置为6,将html设置为11.
I am using RStudio 0.98.932 and knitr 1.6. Would like to set different global knitr options for word and html. For example, want to set fig.width and fig.height as 6 for word and 11 for html.
如果rmd文件的输出格式可用,我可以编写一些代码来切换设置.我应该怎么做?感谢您的任何建议.
I can write some codes to switch the setting if it is available for the output format of a rmd file. How should I do this? Thanks for any suggestions.
推荐答案
尝试将此代码块放在Rmd文档的开头.
Try putting this code chunk at the beginning of the Rmd document.
```{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,Word或PDF),显然可以将其设置为"html"
,"docx"
或"latex"
.因此,您可以对其进行测试并相应地设置块选项fig.width
和fig.height
.
One of the package options returned by opts_knit$get()
is markdown.pandoc.to
. This is evidently set to "html"
, "docx"
, or "latex"
depending on the chosen output format (HTML, Word, or PDF). So you can test that and set the chunk options fig.width
and fig.height
accordingly.
这篇关于如何在knitr和RStudio中为word和html设置不同的全局选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!