use*_*854 71 console logging r file
我想将所有控制台文本重定向到一个文件.这是我尝试过的:
> 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"
Run Code Online (Sandbox Code Playgroud)
这是我在test.log中得到的:
[1] "a"
Run Code Online (Sandbox Code Playgroud)
这是我在test.log中想要的:
> a <- "a"
> a
[1] "a"
> How come I do not see this in log
Error: unexpected symbol in "How come"
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?谢谢!
Tom*_*mmy 89
你必须分别下沉"输出"和"消息"(该sink功能只查看第一个元素type)
现在,如果您还想记录输入,则将其放入脚本中:
script.R
1:5 + 1:3 # prints and gives a warning
stop("foo") # an error
Run Code Online (Sandbox Code Playgroud)
并在提示:
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")
Run Code Online (Sandbox Code Playgroud)
小智 9
如果您可以访问命令行,则可能更喜欢使用R CMD BATCH从命令行运行脚本.
==开始script.R ==的内容
a <- "a"
a
How come I do not see this in log
Run Code Online (Sandbox Code Playgroud)
==结束script.R ==的内容
在命令提示符下(许多un*x变体中的"$",Windows中的"C:>"),运行
$ R CMD BATCH script.R &
Run Code Online (Sandbox Code Playgroud)
尾部"&"是可选的,并在后台运行命令.日志文件的默认名称在扩展名后附加了"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
Run Code Online (Sandbox Code Playgroud)
==结束script.Rout ==的内容
如果您能够使用 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
Run Code Online (Sandbox Code Playgroud)
然后,当您运行脚本并将 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
Run Code Online (Sandbox Code Playgroud)
为此需要注意的其他事情是尝试简单地将 stdout 和 stderr 从 R heredoc 直接放入日志文件中;我还没有尝试过,但它可能也会起作用。