R markdown chunk's dpi option ignored on "Run current chunk"

When I click "Run current chunk" using the following chunk options creates an image of 164 × 164 pixels in the preview below the code in Rstudio's R markdown development environment:

{r, mychunk, fig.width=1.75, fig.height=1.61, dpi=600, retina=1}

However, when I knit the whole markdown file the resolution scales with increase of the DPI as expected, giving me a 1050 × 966 pixel image. Also the output of

message(knitr::opts_current$get('dpi'))

does not reflect the the DPI set in the chunk's header, when clicking on "Run current chunk". The dpi option is actually NULL. I want my knitted report to have high-resolution images for publishing. Therefore I use the chunk options fig.height, fig.width (in inches) and dpi according to specifications of the publisher. But unfortunately, in the inline preview, it translates to tiny figures. Probably because of a fixed dpi value that is used to create the plots below the code chunks.

I think it would be important for this inline execution mode to respect the DPI set in the header, because otherwise it is extremely difficult to debug figures. Knitting the whole report every time I change the code to create the plot is not an option, because it takes too long.

Reproducible example:

---
title: "R Notebook"
output: html_document
---

```{r, mychunk, fig.width=1.75, fig.height=1.61, dpi=600, fig.retina=1}
library(ggplot2)
message(paste0("dpi=", knitr::opts_current$get('dpi')))
ggplot(cars, aes(speed, dist)) + geom_point()

I am able to reproduce this for

  • RStudio 1.1.383 on Ubuntu 16.04/GNOME Shell 3.18.5
  • RStudio 1.1.463 on Ubuntu 18.04/GNOME Shell 3.28.3

Welcome to the forum!

Can you add some info about what kind of output your are making with R Markdown and what sort of device you are using for your image (e.g., bmp, tiff)?

It can help people help you if you include a reproducible example (aka reprex), with data and code, that can reproduce the problem. You can read more about the elements of a reprex here: FAQ: What's a reproducible example (`reprex`) and how do I do one?

I think it can be a little tricky to give a reproducible example of R Markdown. Including the YAML header and any set-up chunk will be important along with at least one chunk that makes a plot that demonstrates the problem.

Hi, thanks for the feedback. I improved my question according to your suggestions.

Well, hopefully someone will come around with some ideas but I haven't hit on anything yet. :stuck_out_tongue_winking_eye: I did see at least a couple of versions of your question that are unanswered on Stack Overflow (like here and here).

I thought one work-around could be to save your plots with the appropriate size and dpi and then use knitr::include_graphics() to both display the image inline for troubleshooting and to include in the final output. The actual size in the HTML output was different, though (although the plot nominally looked the same to my eye).

```{r mychunk2}
library(ggplot2)
p1 = ggplot(cars, aes(speed, dist)) + geom_point()
ggsave("test1.png", plot = p1, width = 1.75, height = 1.61, dpi = 600)
knitr::include_graphics("test1.png")
```
1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.