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

File 如何将所有控制台输出保存到R中的文件?,file,r,console,logging,File,R,Console,Logging,我想将所有控制台文本重定向到一个文件。以下是我尝试过的: > 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" > a <- "a" > a [1] "a" >

我想将所有控制台文本重定向到一个文件。以下是我尝试过的:

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

我做错了什么?谢谢

必须分别接收输出和消息。接收函数只查看类型的第一个元素

现在,如果希望也记录输入,请将其放入脚本中:

script.R

在提示下:

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

你不能。最多可以分别使用sink和savehistory保存输出和输入。或者使用脚本、屏幕或tmux等外部工具

如果您有权访问命令行,您可能更喜欢使用R CMD BATCH从命令行运行脚本

==开始脚本的内容。R==

a <- "a"
a
How come I do not see this in log
trailing&是可选的,在后台运行命令。 日志文件的默认名称已附加到扩展名,即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

=脚本的结束内容。RUT==

< P>如果您能够使用BASH shell,可以考虑简单地从BASH脚本中运行R代码,并将STDUT和STDRR流管到文件。以下是使用heredoc的示例:

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
文件:test.sh


对于这一点,需要注意的其他事项是尝试将stdout和stderr直接从redoc导入日志文件;我还没有尝试过这个,但它可能也会起作用。

在emacs中使用ESS emacs Speaks Statistics R模式运行R。我打开了一个窗口,其中有我的脚本和R代码。另一个正在运行。代码从语法窗口发送并计算。命令、输出、错误和警告都出现在正在运行的R窗口会话中。在某个工作周期结束时,我将所有输出保存到一个文件中。我自己的命名系统是*.R(用于脚本)和*.Rout(用于保存输出文件)。
下面是一个带有示例的屏幕截图。

要从控制台保存文本,请运行分析,然后选择Windows文件>保存到文件。

为大量行设置Rgui首选项,然后按适当的间隔设置时间戳和另存为文件

如果要获取保存在文件中的错误消息

此输出将保存在名为Errors.txt的文件中

在这种情况下,您希望将console的打印值打印到一个文件中,您可以使用“split”参数:

zz <- file("console.txt", open="wt")
sink(zz,  split=TRUE)
print("cool")
print(errr)
在console.txt文件中。因此,所有控制台输出都将打印在名为console.txt的文件中


在运行R脚本时,您可以打印到文件,同时可以看到有无屏幕的进度

不使用screen时,使用R CMD BATCH yourscript.R&和步骤4

使用屏幕时,在终端中,启动屏幕

 screen
运行你的R脚本

 R CMD BATCH yourscript.R
按CtrlA转到另一个屏幕,然后按c

实时查看您的输出:

 tail -f yourscript.Rout
使用CtrlA然后在屏幕之间切换n


这只打印输出,不打印输入。我想看到输入行,例如1:5+1:3,然后是输出,然后是下一行,等等。我想生成这种类型的日志的原因是因为我有一个需要30+GB RAM才能运行的程序。我在amazon云中运行它,并将回归的输出保存到单个文件中。我希望能够通过查看日志快速找到生成每个文件的代码。注意:如果我只是剪切n粘贴控制台输出,就可以了。@user443854如果是这样,最好放弃交互式工作并使用脚本。@user443854:是的,你能把代码放在脚本中吗?在这种情况下,sourcescript.R,echo=TRUE就可以了——如果你像上面解释的那样重定向输出。@Tommy你就是那个人。谢谢我确实有一个.R脚本,但我正在将它粘贴到remote box上的交互式会话中。寻源就是这样。@user443854:是的,使用max.deparse.length参数。我更新了答案。出于某些原因,我正在使用zsh。R CMD批处理脚本。R&不起作用。请详细说明您可以看看本教程:
Error in print(errr) : object 'errr' not found
Execution halted
zz <- file("console.txt", open="wt")
sink(zz,  split=TRUE)
print("cool")
print(errr)
[1] "cool"
 screen
 R CMD BATCH yourscript.R
 tail -f yourscript.Rout