How to save all console output to file in R?

匿名 (未验证) 提交于 2019-12-03 02:06:01

问题:

I want to redirect all console text to a file. Here is what I tried:

> sink("test.log", type=c("output", "message")) > 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:

[1] "a" 

Here is what I want in test.log:

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

回答1:

You have to sink "output" and "message" separately (the sink function only looks at the first element of type)

Now if you want the input to be logged too, then put it in a script:

script.R

1:5 + 1:3   # prints and gives a warning stop("foo") # an error 

And at the prompt:

con 


回答2:

If you have access to a command line, you might prefer running your script from the command line with R CMD BATCH.

== begin contents of script.R ==

a 

== end contents of script.R ==

At the command prompt ("$" in many un*x variants, "C:>" in windows), run

$ 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

== 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 [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:

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.



回答4:

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.



回答5:

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:

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

Then when you run the script with both stderr and stdout piped to a log file:

$ 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.



回答6:

To save text from the console: run the analysis and then choose (Windows) "File>Save to File".



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!