问题
I'm using an R Notebook and I'd like my plots to automatically save to disk when the code is run, as well as display inline.
knitr: include figures in report *and* output figures to separate files addresses this for R Markdown but the solution given doesn't work for an R Notebook. Is there a similar option for R Notebooks?
回答1:
Try setting the knitr fig.path option:
knitr::opts_chunk$set(fig.path = "path/to/figures/")
Where path/to/figures/
is the path to a subdirectory where your figures will be saved. The trailing slash is necessary. This should be a relative path, either relative to the RNotebook file or to the project directory. See here::here()
for a handy way to locate the project directory.
This will put each figure into that directory; figure names will be based on the chunk name (so name your chunks!)
回答2:
This is what eventually worked for me (see @TCZhang 's answer to my question here):
In addition to setting the knitr chunk fig.path="figures/"
option suggested by @DonJ, try setting output: html_document
, or just press the dropdown next to the Preview [Notebook]
button at the top and press Knit to HTML
. I think the reason this isn't working is that your output is set to output: html_notebook
.
I don't know why this doesn't work specifically when the doc is in R Notebook format. I would also prefer if this worked for output: html_notebook
, so it might be an issue we need to open with RStudio or knitr.
来源:https://stackoverflow.com/questions/49435594/r-notebook-include-figures-in-report-and-save-plots