2011-08-17 62 views
50

我想将全部控制台文本重定向到文件。这里是我的尝试:如何将所有控制台输出保存到R中的文件?

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

我是什么做错了?谢谢!

回答

65

你,如果你想得太记录的输入下沉“输出”和“消息”单独(该sink功能只着眼于type第一元素)

现在,然后放它在脚本:

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

这只打印输出,但不打印输入。我想查看输入行,例如'1:5 + 1:3',然后是输出,然后是下一个等。我想生成这种类型的日志的原因是因为我有一个需要30+ GB的RAM运行的程序。我在亚马逊云中运行它,并将回归的输出保存到单个文件中。我希望能够通过查看日志来快速找到产生每个文件的代码。注意:如果我只是剪切 - 粘贴控制台输出,就是这样。 – user443854

+5

@ user443854如果是这样,放弃交互式工作并使用脚本是一个更好的主意。 – mbq

+4

@ user443854:是的,你可以把代码放在脚本中吗?在这种情况下,source(“script.R”,echo = TRUE)会做诡计 - 如果你重定向输出,如上所述。 – Tommy

3

你不能。最多可以分别保存sinksavehistory的输出。或者使用外部工具,如script,screentmux

9

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

==开始script.R ==内容

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

在许多script.R ==

的==结束内容在命令提示符( “$” UN * X的变体, “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 

==端内容的内容。 Rout ==

+1

我正在使用zsh,由于某些原因'R CMD BATCH script.R&'不起作用。 – Gilbert

0

如果您能够使用bash shell,则可以考虑从bash脚本中简单运行R代码,并将stdout和stderr流传输到文件。下面是一个使用定界符的例子:

文件: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 

然后,当你运行带有两个标准错误和标准输出脚本管道输送到一个日志文件:

$ 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 

其他东西看为此,只需简单地将stredout和stderr从R heredoc中修改到日志文件中即可;我还没有尝试过,但它可能也会起作用。

1

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

0

要从控制台保存文本:运行分析,然后选择(Windows)“文件>保存到文件”。

+1

如果您有新问题,请点击[问问题](https://stackoverflow.com/questions/ask)按钮。如果有助于提供上下文,请包含此问题的链接。 - [来自评论](/ review/low-quality-posts/18957447) – Dmitry

相关问题