rstudio / rmarkdown

Dynamic Documents for R
https://rmarkdown.rstudio.com
GNU General Public License v3.0
2.88k stars 979 forks source link

Can't save plot in Rmarkdown #2510

Open canna1998 opened 1 year ago

canna1998 commented 1 year ago

I has encountered a strange problem, and search it for many days: can't save plot to pdf or png file in interactive Rmarkdown.

This is the minimal example:

png(file = "test.png")
plot(x = 0:10, y = 10:20)
dev.off()

When I use "command+enter" run directly those code in Rmarkdown, the plot will show inline under the code, but doesn't save to file. dev.off() would show Error: Error in dev.off() : cannot shut down device 1 (the null device) "test.pdf" file has been generated, but when I open it, it shows “The file content is corrupted”.

By the way, 1. when using knit to render the output file, "test.pdf" will be generated correctly, 2. ggsave function works in interactive way mentioned above.

xfun::session_info('rmarkdown')

R version 4.0.5 (2021-03-31) Platform: x86_64-apple-darwin17.0 (64-bit) Running under: macOS 13.3.1, RStudio 2021.9.0.351 Locale: zh_CN.UTF-8 / zh_CN.UTF-8 / zh_CN.UTF-8 / C / zh_CN.UTF-8 / zh_CN.UTF-8 Package version: base64enc_0.1.3 bslib_0.4.0 cachem_1.0.6 digest_0.6.29 evaluate_0.21 fastmap_1.1.0 fontawesome_0.5.2 fs_1.5.2 glue_1.6.2 graphics_4.0.5 grDevices_4.0.5 highr_0.10 htmltools_0.5.2 jquerylib_0.1.4
jsonlite_1.8.0 knitr_1.40 magrittr_2.0.3 memoise_2.0.1 methods_4.0.5 R6_2.5.1 rappdirs_0.3.3
rlang_1.0.2 rmarkdown_2.24 sass_0.4.1 stats_4.0.5 stringi_1.7.6 stringr_1.4.1 tinytex_0.46
tools_4.0.5 utils_4.0.5 xfun_0.30 yaml_2.3.5
Pandoc version: 2.14.0.3

cderv commented 1 year ago

png(file = "test.pdf")

You saving to png() using a .pdf extension 🤔

Did you try png("test.png") or pdf("test.pdf") ?

canna1998 commented 1 year ago

png(file = "test.pdf")

You saving to png() using a .pdf extension 🤔

Did you try png("test.png") or pdf("test.pdf") ?

Sorry, I made a typo when I write the question. I have modified it. But this Error exists when the code is right.

image

cderv commented 1 year ago

dev.off() would show Error: Error in dev.off() : cannot shut down device 1 (the null device) "test.pdf" file has been generated, but when I open it, it shows “The file content is corrupted”.

I don't really know about the error. I suspect this is something with how the IDE is handling Graphic device. You file should be written to disk though and working.

FYI knitr is already saving plot to file when you do

```{r}
plot(x = 0:10, y = 10:20)


You can control which devices to use and how it is saved using figures options (https://yihui.org/knitr/options/#plots). 
So **knitr** is already handling graphic device when rendering, and RStudio IDE probably do also handles graphic device when interactively running R code (knitr is not involved in that case). 
You can access the saved file easily after knitting. If you do `self_contained: false` with **rmarkdown**, but you could also save by copying elsewhere the file you need, buy accessing it using `knitr::fig_chunk()` from another chunk. 

So you should report the warning in RStudio IDE repo if you only sees it in the IDE when interactively rendering. I don't see what can be done in **rmarkdown** regarding this.