【发布时间】:2020-03-15 06:23:04
【问题描述】:
我正在尝试在 RStudio 中保存绘图,但它没有保存,也没有显示在绘图选项卡中。
我正在使用以下代码:
pdf(file="study1_screeplot.pdf")
print(plotnScree(nS_Study1))
while (!is.null(dev.list()))
dev.off()
情节 plotnScree(nS_Study1) 在代码块的末尾成功打印,但正如我所说,它不会显示在情节窗格/选项卡中,也不会保存到文件中。
我也试过了:
pdf(file="study1_screeplot.pdf")
plotnScree(nS_Study1)
while (!is.null(dev.list()))
dev.off()
还有:
scree_S1 <- plotnScree(nS_Study1)
pdf(file="study1_screeplot.pdf")
print(scree_S1)
while (!is.null(dev.list()))
dev.off()
还有:
scree_S1 <- plotnScree(nS_Study1)
pdf(file="study1_screeplot.pdf")
scree_S1
while (!is.null(dev.list()))
dev.off()
另外,在阅读了我尝试运行的许多文章后:
dev.off() 在控制台中单独使用(没有 while 语句)并得到以下错误:
> dev.off()
Error in dev.off() : cannot shut down device 1 (the null device)
这是 cmets 中要求的一个简单、可重现的示例:
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
【问题讨论】:
-
如果您包含一个简单的reproducible example 以及可用于测试和验证可能的解决方案的示例输入,则更容易为您提供帮助。
-
确保函数
plotnScree()返回的对象是绘图对象。例如,对于直方图:plotnScree <- function(x) return(hist(x, plot=TRUE))。此代码应该可以工作:pdf(file="study_screeplot.pdf");plotnScree(runif(1000));dev.off(); -
@MrFlick 我添加了一个简单的可重现示例。
-
。 @JdeMello 是的,从 plotnScree() 返回的对象是绘图对象。我可以确认这一点,因为该图确实是在 RStudio 中我的降价文件中的代码块末尾生成的。但是,它没有显示在绘图窗格中或作为文件输出。
-
你试过
pdf(file="study_screeplot.pdf");plotnScree(nS_arrests);dev.off();吗?它在这里工作得很好。顺便说一句,在编写一个最小的工作示例时,请确保提及您正在使用的包 - 在本例中为包nFactors。