简体   繁体   中英

How to set different global options in knitr and RStudio for word and html?

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.

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.

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)
```

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM