2
votes

I'm running an analysis in an R Notebook and I would like all plots created in R chunks to be saved as individual PDF files in addition to appearing in the .nb.html notebook output.

The problem

The problem I'm having is that, when the notebook is run, it does not save plots to the dir specified in the chunk option fig.path = "figures/" either when specified in the individual chunk header:

#```{r fig.path = "figures/"}
plot(x, y)
#```

or when specified with the global chunk options:

#```{r setup}
library(knitr)
opts_chunk$set(fig.path = "figures/")
#```

#```{r}
plot(x, y)
#```

In fact, there is no dir named *figures/ at all, anywhere. not in the project root directory nor in the dir where the .Rmd script is located. Even when I manually create the folder proj_root/figures/, plots are not output to here.

What I've tried

  • I tried setting the chunk options for a single chunk that outputs a plot to fig.path = "figures/", which does not produce anything
  • In a setup chunk, I've run these lines:
# load knitr package
library(knitr)
# set all subsequent chunks' working dir as the project root dir
opts_knit$set(root.dir = rprojroot::find_rstudio_root_file())
# set this first setup chunk working dir to project root dir (since the previous line does not affect the chunk it's run in)
setwd(rprojroot::find_rstudio_root_file())
# Check current working dir
getwd() # CORRECTLY OUTPUTS R.PROJECT ROOT DIR

# Set all chunks to output plots to the dir "figures/"
opts_chunk$set(fig.path = "figures/") # This should output all chunk plots to "project-root/figures/"

, which I assume should make the project-root/figures/ folder and output all chunk plots there, in addition to keeping them in the .nb.html report.

I've also tried also specifying dev = "pdf" in the global chunks option in opts_chunk$set(dev = "pdf", fig.path = "figures/"), but this doesn't change anything.

I have no idea what I'm doing wrong, and everywhere I've searched, none of the options work, including:

The knitr documentation is unhelpful as it only describes the fig.path = chunk option, but no troubleshooting if it doesn't work. Do I need to add other fig.*** = chunk options in order to make it work? Shouldn't it work with only setting the fig.path = option?

Does it have anything to do with my YAML output being html_notebook instead of outputting to markdown or html_document?

The closest post to my problem I have come across is this one: R Notebook: Include figures in report and save plots, but it has not been resolved since 2018 and I'm not sure how to 'promote' that post other than making a new one and referencing it.

1
You can save plots by explicitly calling functions such as ggsave().TC Zhang
Yeah I know, and this is what I've been using as a workaround. But my issue is with the knitr global chunk options not working as intended as that would be much easier to automatically save all plots than having to add several lines of code to every chunk, multiple if more than one plot.Leo
Maybe you could set self_contained: false ? bookdown.org/yihui/rmarkdown/…TC Zhang
Thanks for the suggest, but unfortunately I tried that as well. It did save a dir with a bunch of intermediate files, but the dir was in the dir of the .Rmd file (not where I want it) and more importantly did not contain any figures that I could find anywhere in the hierarchy... I will try to fiddle with that some more though.Leo

1 Answers

3
votes

I tested and the fig.path option works as expected. please test the following rmd:

Edit: Make sure the output type is set to output: html_document in the Rmd YAML header, and also click Knit to HTML to activate the saving of the figures. This is separate from, and can be done after, running the Rmd document interactively or with Run All. See comments below

---
title: "Untitled"
author: "TC"
date: "10/30/2019"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, fig.path="testit/")
```

## R Markdown

https://stackguides.com/questions/58600399/how-do-you-run-an-r-file-on-ubuntu?noredirect=1#comment103544013_58600399

```{r pressure, echo=FALSE}
plot(pressure)
```

```{r pressure2, echo=FALSE}
plot(pressure)
```


```{r echo=FALSE}
plot(pressure)
```

rmd source

and here is the generated figures under testit