如果您能够使用bash shell,则可以考虑仅在bash脚本中运行R代码,并将stdout和stderr流传输到文件中。这是使用heredoc的示例:
文件: test.sh
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
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
然后,当您将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
其他需要考虑的事情是尝试简单地将Rd.doc中的stdout和stderr插入日志文件中。我还没有尝试过,但它可能也可以工作。