如何将R会话记录到文件中?

Jam*_*ard 18 logging r stata

除了通过重定向捕获整个会话之外,有没有办法在R会话期间启动和停止记录?为了澄清,我正在寻找与log usingStata中的命令类似的东西.

Dir*_*tel 17

你知道sink()基地R吗?

CRAN上还有一些日志包:logging,log4r以及更多.

最后,Emacs用户拥有ESS及其成绩单模式.您可以将会话保存为日志,通常,"从文件工作并从文件执行"方法会在您工作时构建(部分,仅命令)日志.


usc*_*t01 7

为了完成使用水槽的答案

# copy the log to a text file
sink("./logofcode.txt")
Your R code(s) goes here
you can use a stored R code as well using source()
source("./XS_SPEC_CF.R",echo=T, max.deparse.length=1e3)
sink()
Run Code Online (Sandbox Code Playgroud)

  • 由于我经常需要这样做,因此我将 usct01 的方法转换为一个小函数: `source_with_log <- function(r_script, log_file) { sink(file = log_file) source(r_script, echo = TRUE) sink() }` (2认同)

小智 5

savehistory(file)会将整个历史记录以纯文本形式写入,或者,如果您尝试记录输出,请使用sink(file, split = TRUE).