0

I am attempting to save a plot in RStudio, but it is not saving and it is also not showing in the plots tab.

I am using the following code:

pdf(file="study1_screeplot.pdf")
print(plotnScree(nS_Study1))
while (!is.null(dev.list()))
dev.off()

The plot plotnScree(nS_Study1) successfully prints at the end of the code chunk, but as I say it will not show in the plots pane/tab nor save to file.

I have also tried:

pdf(file="study1_screeplot.pdf")
plotnScree(nS_Study1)
while (!is.null(dev.list()))
dev.off()

And:

scree_S1 <- plotnScree(nS_Study1)

pdf(file="study1_screeplot.pdf")
print(scree_S1)
while (!is.null(dev.list()))
dev.off()

And:

scree_S1 <- plotnScree(nS_Study1)

pdf(file="study1_screeplot.pdf")
scree_S1
while (!is.null(dev.list()))
dev.off()

Also, after reading many articles I have tried running:

dev.off() on its own (without the while statement) in the console and get the following error:

> dev.off()
Error in dev.off() : cannot shut down device 1 (the null device)

Here is a simple, reproducible example as requested in the comments:

data(USArrests)
# get eigenvalues and eigenvectors
ev <- eigen(cor(USArrests))
# get distribution of the eigenvalues of correlation matrix
ap <- parallel(subject=nrow(USArrests),var=ncol(USArrests),rep=100,cent=.05)
# make scree plot and get number of components using parallel analysis (aparallel argument)
nS_arrests <- nScree(x=ev$values, aparallel=ap$eigen$qevpea) # qevpea = the 95 centile
plotnScree(nS_arrests)

pdf(file="arrests_screeplot.pdf")
print(plotnScree(nS_arrests))
while (!is.null(dev.list()))
dev.off()
> sessionInfo()
R version 3.5.3 (2019-03-11)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Mojave 10.14.6

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] pscl_1.5.2           flexmix_2.3-15       bindrcpp_0.2.2       lmtest_0.9-36       
 [5] zoo_1.8-3            lme4_1.1-20          Matrix_1.2-15        FactoMineR_1.42     
 [9] factoextra_1.0.5.999 reshape2_1.4.3       xtable_1.8-3         stargazer_5.2.2     
[13] corpcor_1.6.9        pracma_2.2.5         nFactors_2.3.3.1     lattice_0.20-38     
[17] boot_1.3-20          MASS_7.3-51.1        fields_9.9           maps_3.3.0          
[21] spam_2.3-0           dotCall64_1.0-0      forcats_0.3.0        stringr_1.3.1       
[25] purrr_0.2.5          readr_1.1.1          tidyr_0.8.1          tibble_1.4.2        
[29] tidyverse_1.2.1      ggplot2_3.0.0        psych_1.8.12         dplyr_0.7.8         

loaded via a namespace (and not attached):
 [1] minqa_1.2.4           colorspace_1.3-2      modeltools_0.2-22     class_7.3-15         
 [5] rio_0.5.16            base64enc_0.1-3       rstudioapi_0.7        ggpubr_0.2           
 [9] rstan_2.18.2          ggrepel_0.8.0         gh_1.0.1              prodlim_2018.04.18   
[13] lubridate_1.7.4       xml2_1.2.0            splines_3.5.3         leaps_3.0            
[17] mnormt_1.5-5          knitr_1.20            jsonlite_1.5          nloptr_1.2.1         
[21] olsrr_0.5.2           broom_0.5.0           cluster_2.0.7-1       shiny_1.1.0          
[25] compiler_3.5.3        httr_1.3.1            backports_1.1.2       assertthat_0.2.0     
[29] lazyeval_0.2.1        cli_1.1.0             later_0.7.4           prettyunits_1.0.2    
[33] htmltools_0.3.6       tools_3.5.3           gtable_0.2.0          glue_1.3.0           
[37] Rcpp_1.0.0            carData_3.0-2         cellranger_1.1.0      nlme_3.1-137         
[41] timeDate_3043.102     gower_0.1.2           ps_1.3.0              openxlsx_4.1.0       
[45] rvest_0.3.2           mime_0.5              goftest_1.1-1         scales_1.0.0         
[49] ipred_0.9-7           hms_0.4.2             promises_1.0.1        parallel_3.5.3       
[53] inline_0.3.15         yaml_2.2.0            curl_3.2              gridExtra_2.3        
[57] loo_2.1.0             StanHeaders_2.18.1    rpart_4.1-13          stringi_1.2.4        
[61] nortest_1.0-4         checkmate_1.9.1       pkgbuild_1.0.3        zip_1.0.0            
[65] lava_1.6.3            matrixStats_0.54.0    rlang_0.4.0           pkgconfig_2.0.2      
[69] evaluate_0.11         bindr_0.1.1           recipes_0.1.4         labeling_0.3         
[73] processx_3.4.1        tidyselect_0.2.4      plyr_1.8.4            magrittr_1.5         
[77] R6_2.2.2              generics_0.0.2        pillar_1.3.0          haven_2.1.0          
[81] foreign_0.8-71        withr_2.1.2           survival_2.43-3       scatterplot3d_0.3-41 
[85] abind_1.4-5           nnet_7.3-12           modelr_0.1.2          crayon_1.3.4         
[89] car_3.0-2             rmarkdown_1.11        readxl_1.1.0          data.table_1.12.0    
[93] callr_3.3.2           digest_0.6.16         flashClust_1.01-2     GPArotation_2014.11-1
[97] httpuv_1.4.5          stats4_3.5.3          munsell_0.5.0
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. – MrFlick Nov 19 '19 at 17:23
  • Make sure the object returned in the function `plotnScree()` is the plot object. For instance, for a histogram: `plotnScree <- function(x) return(hist(x, plot=TRUE))`. This code should work: `pdf(file="study_screeplot.pdf");plotnScree(runif(1000));dev.off();` – JdeMello Nov 19 '19 at 17:27
  • @MrFlick I have added a simple reproducible example. – Kelsey Urgo Nov 19 '19 at 17:36
  • . @JdeMello yes the object returned from plotnScree() is the plot object. I can confirm this because the plot is indeed generated at the end of the code chunk in my markdown file in RStudio. However, it is not showing up in the plots pane or being output as a file. – Kelsey Urgo Nov 20 '19 at 12:23
  • Have you tried `pdf(file="study_screeplot.pdf");plotnScree(nS_arrests);dev.off();`? it worked just fine here. By the way, when writing a minimal working example, make sure you mention the packages you are using —in this case the package `nFactors`. – JdeMello Nov 20 '19 at 16:51
  • @JdeMello as I mentioned above I cannot run `dev.off()` on its own as you demonstrated because it returns the error `Error in dev.off() : cannot shut down device 1 (the null device)` I can run `pdf(file="arrests_screeplot.pdf"); plotnScree(nS_arrests); while (!is.null(dev.list())); dev.off()` but it doesn't save the file – Kelsey Urgo Nov 20 '19 at 20:26
  • Sorry, I don't know why it is not working then. It may be worthwhile to add to your question your R session output. Run `sessionInfo()` and paste the output to your question. J – JdeMello Nov 20 '19 at 20:28
  • @JdeMello Thanks for your help so far. I just added `sessionInfo()` to the question. – Kelsey Urgo Nov 20 '19 at 21:15

1 Answers1

0

This took me weeks to remedy, but found a rather simple and effective answer.

The code cannot be run line-by-line in RStudio. The following code chunk must be highlighted and run all together.

pdf(file="arrests_screeplot.pdf")
print(plotnScree(nS_arrests))
dev.off()

And voila! It worked.

I hope this helps others.