38

I would like to save the output of "sessionInfo()" to a text file. Using "write()" failed because "list() cannot be handled by 'cat()'". I then tried "save()" with ascii = T but the resulting file is not really helpful.

I would like to have an output like this in a text file. Any easy, straightforward way to do this?

Community
  • 1
  • 1
roschu
  • 766
  • 1
  • 8
  • 17
  • I was looking for an option that can be included in an R markdown file (Rmd) and prints nicely to PDF/HTML. With my approach for this, I ended up providing an answer to a related [SO question](https://stackoverflow.com/a/65630773/10451245). – mavericks Jan 08 '21 at 14:27

5 Answers5

48

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

writeLines(capture.output(sessionInfo()), "sessionInfo.txt")
Roland
  • 127,288
  • 10
  • 191
  • 288
15
‘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 
Jake Burkhead
  • 6,435
  • 2
  • 21
  • 32
9

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(t(data.frame(my_session_info$platform)), col.names = "value"), collapse = "\n"),
          paste0(rep(" ", 80), collapse = ""),      # some separator
          paste0(rep(" ", 80), collapse = ""),      # some separator
          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) ]

Mehrad Mahmoudian
  • 3,466
  • 32
  • 36
  • 1
    +1 for `devtools::session_info`. It's much easier to read, since it's alphabetized. It also has nice extras like the commit hash for packages installed from github or bitbucket. – eric_kernfeld Mar 04 '18 at 22:58
4

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")
SlyFox
  • 100
  • 1
  • 5
0

Seems like we don't need writeLines() as we can directly specify the output file to capture.output() function.

sessionInfo() %>% capture.output(file="session_info.txt")
dabsingh
  • 281
  • 1
  • 3
  • 10