Depending on format output, I want all plots in rmarkdown
to be a certain size if the output is word
(officedown::rdocx_document:
) and a different size if the output is html
(bookdown::html_document2:
).
I was thinking this could be achieved by having two chunks at the start where only one is evaluated if the output is the one I want, something like this test.Rmd
file:
---
title: "test"
output:
bookdown::html_document2:
officedown::rdocx_document:
always_allow_html: true
---
```{r setup, include = FALSE}
library(tidyverse)
```
```{r htmltest, include = FALSE, eval = knitr::is_html_output()}
#if output if html, run this:
knitr::opts_chunk$set(echo = TRUE, fig.width = 10, fig.height = 8)
```
```{r wordtest, include = FALSE, eval = knitr::is_latex_output()}
#if output if rdocx_document, run this:
knitr::opts_chunk$set(echo = TRUE, fig.width = 5, fig.height = 5)
```
# plot1
```{r plot, include = TRUE, echo = FALSE, eval = TRUE}
iris %>%
ggplot(aes(x = Petal.Length, y = Sepal.Width)) +
geom_line()
```
# plot2
```{r plot2, include = TRUE, echo = FALSE, eval = TRUE}
iris %>%
ggplot(aes(x = Petal.Width, y = Sepal.Width)) +
geom_line()
```
# end
For the case of the html
, the following render
call works:
rmarkdown::render('test.Rmd', output_format = "html_document2")
but for the officedown::rdocx_document:
call, it doesn't work:
rmarkdown::render('test.Rmd', output_format = "rdocx_document")
This is to be expected as I test for knitr::is_latex_output()
in the code.
What is the equivalent of knitr::is_latex_output()
or knitr::is_html_output()
for officedown::rdocx_document:
?
This solution mentions that for word
, you can run include=knitr::pandoc_to("docx")
but I have tried that and it didn't work (I think its because its for standard word
output word_document:
and not officedown::rdocx_document:
)
Any suggestions (or comments on the approach I'm taking)?
thanks
edit
When I run and render using the following as @stefan suggested:
```{r wordtest, include = FALSE, eval = knitr::pandoc_to("docx")}
#if output if rdocx_document, run this:
knitr::opts_chunk$set(echo = TRUE, fig.width = 5, fig.height = 5)
```
I get the following Error: pandoc document conversion failed with error 1
. I don't get this when I render the html
format.
sessionInfo()
R version 4.2.1 (2022-06-23 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19045)
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] lubridate_1.9.2 forcats_1.0.0 stringr_1.5.0 dplyr_1.1.2 purrr_1.0.1 readr_2.1.4 tidyr_1.3.0 tibble_3.2.1 ggplot2_3.4.2 tidyverse_2.0.0 officer_0.6.3 flextable_0.9.4 officedown_0.3.1 bookdown_0.37 rmarkdown_2.25
pandoc_version()
[1] ‘3.1.1’