2014-02-23 62 views

回答

21

將屏幕輸出捕獲到字符向量中並使用writeLines

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

是的!謝謝 - 我現在沒有capture.output()函數。 – roschu

9
‘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

謝謝!工作得很好。 – roschu

3

您可能需要使用session_info()devtools包。在這種情況下,你使用sink()作爲一個答案解釋在這裏也可以使用下面的有頭,使之更加自明的(因爲print(session_info())使用message()用於顯示標題):

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") 

[ ps記得library(devtools)]

+0

用於'devtools :: session_info'的+1。由於它是按字母順序排列的,因此閱讀起來要容易得多。它還具有很好的附加功能,例如從github或bitbucket安裝的軟件包的提交哈希。 –