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?

1
  • 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.
    – mavericks
    Jan 8, 2021 at 14:27

5 Answers 5

48

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

writeLines(capture.output(sessionInfo()), "sessionInfo.txt")
1
  • 1
    Yes! Thanks -- I did not now the capture.output() function.
    – roschu
    Feb 23, 2014 at 10:57
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 
0
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) ]

1
  • 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. Mar 4, 2018 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")
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")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.