Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't save plot in Rmarkdown #2510

Open
canna1998 opened this issue Sep 6, 2023 · 3 comments
Open

Can't save plot in Rmarkdown #2510

canna1998 opened this issue Sep 6, 2023 · 3 comments
Labels
question general questions - not an issue

Comments

@canna1998
Copy link

canna1998 commented Sep 6, 2023

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

@canna1998 canna1998 changed the title Can't save plot in rmarkdown Can't save plot in Rmarkdown Sep 6, 2023
@cderv
Copy link
Collaborator

cderv commented Sep 6, 2023

png(file = "test.pdf")

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

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

@cderv cderv added the question general questions - not an issue label Sep 6, 2023
@canna1998
Copy link
Author

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
Copy link
Collaborator

cderv commented Sep 7, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question general questions - not an issue
Projects
None yet
Development

No branches or pull requests

2 participants