rmarkdown icon indicating copy to clipboard operation
rmarkdown copied to clipboard

Can't save plot in Rmarkdown

Open canna1998 opened this issue 9 months ago • 3 comments

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 avatar Sep 06 '23 05:09 canna1998