如何将所有控制台输出保存到R中的文件?

我想将所有控制台文本redirect到一个文件。 这是我试过的:

> sink("test.log", type=c("output", "message")) > a <- "a" > a > How come I do not see this in log Error: unexpected symbol in "How come" 

这是我在test.log中得到的:

 [1] "a" 

这是我想要在test.log中:

 > a <- "a" > a [1] "a" > How come I do not see this in log Error: unexpected symbol in "How come" 

我究竟做错了什么? 谢谢!

你必须分别下沉“输出”和“消息”( sinkfunction只看第一个 type元素)

现在,如果你想input也被logging下来,然后把它放在脚本中:

script.R

 1:5 + 1:3 # prints and gives a warning stop("foo") # an error 

在提示符下:

 con <- file("test.log") sink(con, append=TRUE) sink(con, append=TRUE, type="message") # This will echo all input and not truncate 150+ character lines... source("script.R", echo=TRUE, max.deparse.length=10000) # Restore output to console sink() sink(type="message") # And look at the log... cat(readLines("test.log"), sep="\n") 

如果您有权访问命令行,则可能希望使用R CMD BATCH从命令行运行脚本。

==开始script.R ==的内容

 a <- "a" a How come I do not see this in log 

==结束script.R ==的内容

在命令提示符下(多个un * x变体中的“$”,Windows中的“C:>”)运行

 $ R CMD BATCH script.R & 

尾随“&”是可选的,并在后台运行该命令。 日志文件的默认名称在扩展名后附加了“out”,即script.Rout

==开始script.Rout ==的内容

 R version 3.1.0 (2014-04-10) -- "Spring Dance" Copyright (C) 2014 The R Foundation for Statistical Computing Platform: i686-pc-linux-gnu (32-bit) R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. Natural language support but running in an English locale R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. [Previously saved workspace restored] > a <- "a" > a [1] "a" > How come I do not see this in log Error: unexpected symbol in "How come" Execution halted 

==结束script.Rout ==的内容

你不能。 最多可以保存带sink输出,并分别input保存savehistory 。 或者使用scriptscreentmux等外部工具。

使用ESS(Emacs Speaks Statistics)r模式在emacs中运行R. 我有一个窗口打开我的脚本和R代码。 另一个有R运行。 代码从语法窗口发送并进行评估。 命令,输出,错误和警告都显示在正在运行的R窗口会话中。 在某个工作阶段结束时,我将所有输出保存到一个文件中。 我自己的命名系统是用于脚本的* .R和用于保存输出文件的* .Rout。 这里有一个例子的截图。 使用Emacs / ESS编写和评估R。

如果你能够使用bash shell,你可以考虑简单地从bash脚本中运行R代码,并将stdout和stderrstream传输到一个文件。 这里是一个使用heredoc的例子:

文件: test.sh

 #!/bin/bash # this is a bash script echo "Hello World, this is bash" test1=$(echo "This is a test") echo "Here is some R code:" Rscript --slave --no-save --no-restore - "$test1" <<EOF ## R code cat("\nHello World, this is R\n") args <- commandArgs(TRUE) bash_message<-args[1] cat("\nThis is a message from bash:\n") cat("\n",paste0(bash_message),"\n") EOF # end of script 

然后,当您将脚本与stderr和stdout传送到日志文件时:

 $ chmod +x test.sh $ ./test.sh $ ./test.sh &>test.log $ cat test.log Hello World, this is bash Here is some R code: Hello World, this is R This is a message from bash: This is a test 

其他要看的东西是试图简单地将R heredoc中的stdout和stderr转换为日志文件; 我还没有尝试过,但它也可能工作。