I want to redirect all console text to a file. Here is what I tried:
我想将所有的控制台文本重定向到一个文件。以下是我的尝试:
> 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"
Here is what I got in test.log:
下面是我在test.log中得到的结果。
[1] "a"
Here is what I want in test.log:
下面是我在test.log中需要的内容。
> a <- "a"
> a
[1] "a"
> How come I do not see this in log
Error: unexpected symbol in "How come"
What am I doing wrong? Thanks!
我做错了什么?谢谢!
6 个解决方案
#1
70
You have to sink "output" and "message" separately (the sink
function only looks at the first element of type
)
您必须分别接收“输出”和“消息”(sink函数只查看类型的第一个元素)
Now if you want the input to be logged too, then put it in a script:
现在,如果您希望输入也被记录,那么将它放入一个脚本中:
script.R
script.R
1:5 + 1:3 # prints and gives a warning
stop("foo") # an error
And at the prompt:
和提示:
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")
#2
9
If you have access to a command line, you might prefer running your script from the command line with R CMD BATCH.
如果您可以访问命令行,您可能更喜欢使用rcmd批处理的命令行运行您的脚本。
== begin contents of script.R ==
==开始脚本内容。R = =
a <- "a"
a
How come I do not see this in log
== end contents of script.R ==
==脚本的结束内容。R = =
At the command prompt ("$" in many un*x variants, "C:>" in windows), run
在命令提示符下(在windows中,“$”在许多un*x变体中,“C:>”),运行。
$ R CMD BATCH script.R &
The trailing "&" is optional and runs the command in the background. The default name of the log file has "out" appended to the extension, i.e., script.Rout
后面的“&”是可选的,并在后台运行该命令。日志文件的默认名称“out”附加到扩展名,即。,script.Rout
== begin contents of 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
== end contents of script.Rout ==
==脚本的结束内容。溃败= =
#3
3
You can't. At most you can save output with sink
and input with savehistory
separately. Or use external tool like script
, screen
or tmux
.
你不能。在大多数情况下,您可以独立地将存储和输入保存到存储库中。或者使用外部工具,如脚本、屏幕或tmux。
#4
1
Run R in emacs with ESS (Emacs Speaks Statistics) r-mode. I have one window open with my script and R code. Another has R running. Code is sent from the syntax window and evaluated. Commands, output, errors, and warnings all appear in the running R window session. At the end of some work period, I save all the output to a file. My own naming system is *.R for scripts and *.Rout for save output files. Here's a screenshot with an example.
在emacs中运行R (emacs说统计)R模式。我有一个打开脚本和R代码的窗口。另一个运行R。从语法窗口发送代码并进行评估。命令、输出、错误和警告都出现在运行的R窗口会话中。在某个工作期间结束时,我将所有输出保存到一个文件中。我自己的命名系统是*。R用于脚本和*。查找保存输出文件。这是一个例子的截图。
#5
0
If you are able to use the bash shell, you can consider simply running the R code from within a bash script and piping the stdout and stderr streams to a file. Here is an example using a heredoc:
如果您能够使用bash shell,您可以考虑简单地从bash脚本中运行R代码,并将stdout和stderr流传输到一个文件。这里有一个例子:
File: test.sh
文件: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
Then when you run the script with both stderr and stdout piped to a log file:
然后,当您使用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
Other things to look at for this would be to try simply pipping the stdout and stderr right from the R heredoc into a log file; I haven't tried this yet but it will probably work too.
其他需要注意的事情是简单地将stdout和stderr从R文档中移到一个日志文件中;我还没试过,但它可能也会起作用。
#6
0
To save text from the console: run the analysis and then choose (Windows) "File>Save to File".
要从控制台保存文本:运行分析,然后选择(Windows)“文件>保存到文件”。
#1
70
You have to sink "output" and "message" separately (the sink
function only looks at the first element of type
)
您必须分别接收“输出”和“消息”(sink函数只查看类型的第一个元素)
Now if you want the input to be logged too, then put it in a script:
现在,如果您希望输入也被记录,那么将它放入一个脚本中:
script.R
script.R
1:5 + 1:3 # prints and gives a warning
stop("foo") # an error
And at the prompt:
和提示:
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")
#2
9
If you have access to a command line, you might prefer running your script from the command line with R CMD BATCH.
如果您可以访问命令行,您可能更喜欢使用rcmd批处理的命令行运行您的脚本。
== begin contents of script.R ==
==开始脚本内容。R = =
a <- "a"
a
How come I do not see this in log
== end contents of script.R ==
==脚本的结束内容。R = =
At the command prompt ("$" in many un*x variants, "C:>" in windows), run
在命令提示符下(在windows中,“$”在许多un*x变体中,“C:>”),运行。
$ R CMD BATCH script.R &
The trailing "&" is optional and runs the command in the background. The default name of the log file has "out" appended to the extension, i.e., script.Rout
后面的“&”是可选的,并在后台运行该命令。日志文件的默认名称“out”附加到扩展名,即。,script.Rout
== begin contents of 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
== end contents of script.Rout ==
==脚本的结束内容。溃败= =
#3
3
You can't. At most you can save output with sink
and input with savehistory
separately. Or use external tool like script
, screen
or tmux
.
你不能。在大多数情况下,您可以独立地将存储和输入保存到存储库中。或者使用外部工具,如脚本、屏幕或tmux。
#4
1
Run R in emacs with ESS (Emacs Speaks Statistics) r-mode. I have one window open with my script and R code. Another has R running. Code is sent from the syntax window and evaluated. Commands, output, errors, and warnings all appear in the running R window session. At the end of some work period, I save all the output to a file. My own naming system is *.R for scripts and *.Rout for save output files. Here's a screenshot with an example.
在emacs中运行R (emacs说统计)R模式。我有一个打开脚本和R代码的窗口。另一个运行R。从语法窗口发送代码并进行评估。命令、输出、错误和警告都出现在运行的R窗口会话中。在某个工作期间结束时,我将所有输出保存到一个文件中。我自己的命名系统是*。R用于脚本和*。查找保存输出文件。这是一个例子的截图。
#5
0
If you are able to use the bash shell, you can consider simply running the R code from within a bash script and piping the stdout and stderr streams to a file. Here is an example using a heredoc:
如果您能够使用bash shell,您可以考虑简单地从bash脚本中运行R代码,并将stdout和stderr流传输到一个文件。这里有一个例子:
File: test.sh
文件: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
Then when you run the script with both stderr and stdout piped to a log file:
然后,当您使用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
Other things to look at for this would be to try simply pipping the stdout and stderr right from the R heredoc into a log file; I haven't tried this yet but it will probably work too.
其他需要注意的事情是简单地将stdout和stderr从R文档中移到一个日志文件中;我还没试过,但它可能也会起作用。
#6
0
To save text from the console: run the analysis and then choose (Windows) "File>Save to File".
要从控制台保存文本:运行分析,然后选择(Windows)“文件>保存到文件”。