How to use the function `rstudioapi::savePlotAsImage`

library(rstudioapi)
savePlotAsImage('screenshot.png','png',width = 3,height = 4)
#> Error: RStudio not running

Created on 2018-12-19 by the reprex package (v0.2.1)

Session info
devtools::session_info()
#> - Session info ----------------------------------------------------------
#>  setting  value                                              
#>  version  R version 3.5.1 (2018-07-02)                       
#>  os       Windows 7 x64 SP 1                                 
#>  system   x86_64, mingw32                                    
#>  ui       RTerm                                              
#>  language (EN)                                               
#>  collate  Chinese (Simplified)_People's Republic of China.936
#>  ctype    Chinese (Simplified)_People's Republic of China.936
#>  tz       Asia/Taipei                                        
#>  date     2018-12-19                                         
#> 
#> - Packages --------------------------------------------------------------
#>  package     * version date       lib source        
#>  assertthat    0.2.0   2017-04-11 [1] CRAN (R 3.5.1)
#>  backports     1.1.3   2018-12-14 [1] CRAN (R 3.5.1)
#>  base64enc     0.1-3   2015-07-28 [1] CRAN (R 3.5.0)
#>  callr         3.0.0   2018-08-24 [1] CRAN (R 3.5.1)
#>  cli           1.0.1   2018-09-25 [1] CRAN (R 3.5.1)
#>  crayon        1.3.4   2017-09-16 [1] CRAN (R 3.5.1)
#>  desc          1.2.0   2018-05-01 [1] CRAN (R 3.5.1)
#>  devtools      2.0.1   2018-10-26 [1] CRAN (R 3.5.1)
#>  digest        0.6.18  2018-10-10 [1] CRAN (R 3.5.1)
#>  evaluate      0.12    2018-10-09 [1] CRAN (R 3.5.1)
#>  fs            1.2.6   2018-08-23 [1] CRAN (R 3.5.1)
#>  glue          1.3.0   2018-07-17 [1] CRAN (R 3.5.1)
#>  htmltools     0.3.6   2017-04-28 [1] CRAN (R 3.5.1)
#>  knitr         1.20    2018-02-20 [1] CRAN (R 3.5.1)
#>  magrittr      1.5     2014-11-22 [1] CRAN (R 3.5.1)
#>  memoise       1.1.0   2017-04-21 [1] CRAN (R 3.5.1)
#>  pkgbuild      1.0.2   2018-10-16 [1] CRAN (R 3.5.1)
#>  pkgload       1.0.2   2018-10-29 [1] CRAN (R 3.5.1)
#>  prettyunits   1.0.2   2015-07-13 [1] CRAN (R 3.5.1)
#>  processx      3.2.0   2018-08-16 [1] CRAN (R 3.5.1)
#>  ps            1.2.1   2018-11-06 [1] CRAN (R 3.5.1)
#>  R6            2.3.0   2018-10-04 [1] CRAN (R 3.5.1)
#>  Rcpp          1.0.0   2018-11-07 [1] CRAN (R 3.5.1)
#>  remotes       2.0.2   2018-10-30 [1] CRAN (R 3.5.1)
#>  rlang         0.3.0.1 2018-10-25 [1] CRAN (R 3.5.1)
#>  rmarkdown     1.10    2018-06-11 [1] CRAN (R 3.5.1)
#>  rprojroot     1.3-2   2018-01-03 [1] CRAN (R 3.5.1)
#>  rstudioapi  * 0.8     2018-10-02 [1] CRAN (R 3.5.1)
#>  sessioninfo   1.1.1   2018-11-05 [1] CRAN (R 3.5.1)
#>  stringi       1.2.4   2018-07-20 [1] CRAN (R 3.5.1)
#>  stringr       1.3.1   2018-05-10 [1] CRAN (R 3.5.1)
#>  usethis       1.4.0   2018-08-14 [1] CRAN (R 3.5.1)
#>  withr         2.1.2   2018-03-15 [1] CRAN (R 3.5.1)
#>  yaml          2.2.0   2018-07-25 [1] CRAN (R 3.5.1)
#> 
#> [1] C:/Program Files/R/R-3.5.1/library

I don't find the png file in the directory.

Do you get that same error message? "Error: RStudio not running", or something else.

Note that with width and height, these are in pixels.

With 3x4, you'll probably get an unhelpful warning message about the graphics state.

With the following, I got a nice file saved

library(rstudioapi)
hist(rnorm(100))
savePlotAsImage('screenshot.png',width = 300,height = 400)

Thanks, I type your code in the console and it works.

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

Just as a note, since reprex uses rmarkdown/knitr to run your code in a separate, non-interactive R session, functions from rstudioapi are not going to work (as far as the reprex session is concerned, RStudio is not running). This is one of the few situations where unfortunately, using reprex creates confusion rather than clarity!

For more info, see:

and also:

https://support.rstudio.com/hc/en-us/community/posts/203910987-versionInfo-not-working

1 Like