R:减少sessionInfo输出

时间:2018-04-04 21:28:45

标签: r

sessionInfo()打印如下:

R version 3.4.3 (2017-11-30)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=English_United Kingdom.1252  LC_CTYPE=English_United Kingdom.1252   
[3] LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C                           
[5] LC_TIME=English_United Kingdom.1252    

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

other attached packages:
 [1] ggmap_2.7.900       gdtools_0.1.7       bindrcpp_0.2        crosstalk_1.0.0    
 [5] leaflet_1.1.0       networkD3_0.4       d3heatmap_0.6.1.2   dygraphs_1.1.1.4   
 [9] ggiraph_0.4.2       plotly_4.7.1        ggplot2_2.2.1.9000  highcharter_0.5.0  
[13] formattable_0.2.0.1 DT_0.4              kableExtra_0.7.0    stringr_1.3.0      
[17] tidyr_0.8.0         dplyr_0.7.4         markdown_0.8        bookdown_0.7       
[21] rmarkdown_1.9       knitr_1.20         

loaded via a namespace (and not attached):
 [1] nlme_3.1-131.1      bitops_1.0-6        xts_0.10-2          devtools_1.13.5    
 [5] lubridate_1.7.3     RColorBrewer_1.1-2  httr_1.3.1          rprojroot_1.3-2    
 [9] tools_3.4.3         backports_1.1.2     R6_2.2.2            lazyeval_0.2.1     
[13] colorspace_1.3-2    sp_1.2-7            withr_2.1.2         mnormt_1.5-5       
[17] git2r_0.21.0        curl_3.1            compiler_3.4.3      rvest_0.3.2        
[21] xml2_1.2.0          officer_0.2.2       labeling_0.3        scales_0.5.0.9000  
[25] psych_1.7.8         readr_1.1.1         digest_0.6.15       foreign_0.8-69     
[29] R.utils_2.6.0       jpeg_0.1-8          base64enc_0.1-3     pkgconfig_2.0.1    
[33] htmltools_0.3.6     maps_3.2.0          highr_0.6           rvg_0.1.8          
[37] htmlwidgets_1.0     rlang_0.2.0.9001    TTR_0.23-3          quantmod_0.4-12    
[41] xaringan_0.6        shiny_1.0.5         bindr_0.1.1         zoo_1.8-1          
[45] jsonlite_1.5        zip_1.0.0           R.oo_1.21.0         magrittr_1.5       
[49] rlist_0.4.6.1       geosphere_1.5-7     Rcpp_0.12.16        munsell_0.4.3      
[53] proto_1.0.0         R.methodsS3_1.7.1   stringi_1.1.7       yaml_2.1.18        
[57] plyr_1.8.4          grid_3.4.3          parallel_3.4.3      lattice_0.20-35    
[61] mapproj_1.2-5       hms_0.4.2           pillar_1.2.1        igraph_1.2.1       
[65] uuid_0.1-2          rjson_0.2.15        reshape2_1.4.3      glue_1.2.0         
[69] evaluate_0.10.1     data.table_1.10.4-3 png_0.1-7           httpuv_1.3.6.2     
[73] RgoogleMaps_1.4.1   gtable_0.2.0        purrr_0.2.4         assertthat_0.2.0   
[77] xfun_0.1            mime_0.5            xtable_1.8-2        broom_0.4.3        
[81] viridisLite_0.3.0   tibble_1.4.2        memoise_1.1.0  

这是太多的信息。如何跳过最后一个类别(loaded via a namespace (and not attached):)?

2 个答案:

答案 0 :(得分:3)

您可以从sessionInfo()中删除项目,该项目是具有类属性的列表,然后使用相关方法打印生成的对象:

getS3method("print","sessionInfo")(sessionInfo()[-7])
R version 3.4.1 (2017-06-30)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=French_France.1252  LC_CTYPE=French_France.1252    LC_MONETARY=French_France.1252
[4] LC_NUMERIC=C                   LC_TIME=French_France.1252    

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

other attached packages:
 [1] data.table_1.10.4 tidytext_0.1.8    bindrcpp_0.2      dplyr_0.7.4       readr_1.1.1       tidyr_0.7.2      
 [7] tibble_1.3.4      ggplot2_2.2.1     tidyverse_1.1.1   purrr_0.2.4       grofit_1.1.1-1    swirl_2.4.3      
[13] lobstr_0.0.0.9000 Rcpp_0.12.16      rlang_0.2.0

这也适用于相同的输出:

utils:::print.sessionInfo(sessionInfo()[-7]) # thanks @r2evans

`class<-`(sessionInfo()[-7],"sessionInfo")

要知道要从输出中删除哪个部分,请检查:

names(sessionInfo())
 [1] "R.version"  "platform"   "locale"     "running"    "basePkgs"   "otherPkgs"  "loadedOnly" "matprod"    "BLAS"      
[10] "LAPACK"

答案 1 :(得分:1)

如果您阅读?sessionInfo,您会看到它返回"sessionInfo"的对象,这实际上只是一个带有很多“东西”的命名列表。不幸的是,唯一的参数(package=NULL)不限于你想要的。

当你意识到它返回一个被分类的list时,你就可以开始寻找print.sessionInfo,R的类特定功能机制。我找了它并且没找到它,但是如果你仔细观察它的包裹,你会看到utils:::print.sessionInfo。它的来源实际上非常易读(不长且混淆)。

我建议您复制该功能,删除最后5行中的4行,然后使用它。例如:

mySIprint <- function(x, locale = TRUE, ...) {
    mkLabel <- function(L, n) {
        vers <- sapply(L[[n]], function(x) x[["Version"]])
        pkg <- sapply(L[[n]], function(x) x[["Package"]])
        paste(pkg, vers, sep = "_")
    }
    # ... don't forget this code that I'm snipping out for brevity
    if (!is.null(x$otherPkgs)) {
        cat("\nother attached packages:\n")
        print(mkLabel(x, "otherPkgs"), quote = FALSE, ...)
    }
    #if (!is.null(x$loadedOnly)) {
    #    cat("\nloaded via a namespace (and not attached):\n")
    #    print(mkLabel(x, "loadedOnly"), quote = FALSE, ...)
    #}
    invisible(x)
}

mySIprint(sessionInfo())
# R version 3.4.3 (2017-11-30)
# Platform: x86_64-w64-mingw32/x64 (64-bit)
# Running under: Windows 10 x64 (build 16299)
#
# Matrix products: default
#
# locale:
# [1] LC_COLLATE=English_United States.1252 
# [2] LC_CTYPE=English_United States.1252   
# [3] LC_MONETARY=English_United States.1252
# [4] LC_NUMERIC=C                          
# [5] LC_TIME=English_United States.1252    
#
# attached base packages:
# [1] stats     graphics  grDevices utils     datasets  methods   base     
#
# other attached packages:
# [1] ggplot2_2.2.1        microbenchmark_1.4-4 readr_1.1.1         
# [4] bindrcpp_0.2         dplyr_0.7.4          tidyr_0.7.2         
# [7] testthat_2.0.0       plumber_0.4.4        r2_0.6.0