Open canna1998 opened 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")
?
png(file = "test.pdf")
You saving to
png()
using aDid you try
png("test.png")
orpdf("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.
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.
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:
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')