Reputation: 1340
I am creating a website with
Rscript -e "rmarkdown::render_site()"
I am generating both html and pdf versions of documents. A plot generated in a chunk does not appear unless the pdf doc is generated before the html doc.
Here are the files:
index.Rmd
---
title: "My Website"
---
* [Test1 page](test1.html)
* [Test2 page](test2.html)
_site.yml
name: "my-website"
test1.Rmd (html generated first)
---
output:
html_document: default
pdf_document: default
---
```{r, message=FALSE, echo=FALSE}
library(ggplot2)
ggplot(mtcars, aes(x=mpg, y=disp)) + geom_point()
```
test2.Rmd (pdf generated first)
---
output:
pdf_document: default
html_document: default
---
```{r, message=FALSE, echo=FALSE}
library(ggplot2)
ggplot(mtcars, aes(x=mpg, y=disp)) + geom_point()
```
After running render_site()
via Rscript
, test1.html
is blank --- there is no test1_files
subdirectory. However, test2.html
shows this plot (and of course test2_files
exists):
This happens with both Rmarkdown 1.10 and 1.10.14, the development version as of October 31.
In a more complicated real life example, the plots don't appear even if I switch the document order, but I am hoping that the answer to this problem will help with the more complicated one.
UPDATE: In addition to the suggestions by @giocomai, a workaround is to compile test1.Rmd twice:
Rscript -e "rmarkdown::render_site()"
Rscript -e "rmarkdown::render_site('test1.Rmd')"
This seems to work even if you compile multiple single files. Presumably the clean-up is less aggressive in the single-file case.
Upvotes: 3
Views: 4326
Reputation: 3528
I could replicate your problem, and I think this is related to the fact that rmarkdown::render()
cleans the files after it creates a pdf output, as it thinks those files are useless, and render_site
copies the files to the _site
folder only after all output types have been rendered.
In rmarkdown::render()
there is an option to set clean=FALSE
, but it does not seem to be available to rmarkdown::render_site()
, as arguments are not passed to render
.
I think it would be worth filing it as an issue to Rmarkdown, as it shouldn't be too difficult to pass over the argument.
As a workaround, you can force cache = TRUE
in the chunks of the relevant Rmd document. So, for, example, the code chunk in your test1.Rmd would look like:
```{r, message=FALSE, echo=FALSE, cache = TRUE}
library(ggplot2)
ggplot(mtcars, aes(x=mpg, y=disp)) + geom_point()
```
notice the cache = TRUE
in the chunk options. With the cache enabled, the folder is preserved and it is correctly copied to the _site
folder.
You can also set knitr::opts_chunk$set(cache = TRUE)
for all chunks.
This solves your problem, but there should probably be more elegant solutions.
Upvotes: 2