How to write a "reader-friendly" sessionInfo() to text file

As previously mentioned, you can use devtools::session_info() to get an object that is easier to deal with. I then use yaml::write_yaml() (alternatively jsonlite::write_json()) to write the object into a machine readable and human readable version. For example:

library(magrittr)
library(devtools)
library(yaml)

session_info() %>%
    write_yaml("./path/to/file.yaml")

You might want to use session_info() from the devtools package. In that case you use sink() as explained in one of the answers here or you can use the following to have the headers and make it more self-explanatory (since print(session_info()) use message() for displaying the header):

library("devtools")
library("knitr")

my_session_info <- devtools::session_info()

writeLines(text = {
    paste(sep = "\n", collapse = "",
          paste0(rep("-", 80), collapse = ""),
          paste(paste0(rep("-", 32), collapse = ""),
                "R environment",
                paste0(rep("-", 33), collapse = "")),
          paste0(rep("-", 80), collapse = ""),
          paste(knitr::kable(data.frame(setting = names(my_session_info$platform),
                                  value = as.character(my_session_info$platform))), collapse = "\n"),
          paste0(rep("-", 80), collapse = ""),
          paste(paste0(rep("-", 35), collapse = ""),
                "packages",
                paste0(rep("-", 35), collapse = "")),
          paste0(rep("-", 80), collapse = ""),
          paste(knitr::kable(my_session_info$packages), collapse = "\n")
          )
}, con = "session_info.txt")

[ p.s Remember to library(devtools) ]


Capture the screen output into a character vector and use writeLines.

writeLines(capture.output(sessionInfo()), "sessionInfo.txt")

‘sink’ diverts R output to a connection.


sink("sessionInfo.txt")
sessionInfo()
sink()

sessionInfo.txt:

R version 3.0.2 (2013-09-25)
Platform: x86_64-pc-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

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

loaded via a namespace (and not attached):
[1] compiler_3.0.2 tools_3.0.2 

Tags:

R