How do you sink input and output to a text file in R?
Asked Answered
P

2

19

How do you sink both the console input and the console output to a text file? Take the following code:

sink("temp.txt")
1:10
sink()

It will write a text file that looks like this:

[1]  1  2  3  4  5  6  7  8  9 10

But how do I create a text file that looks like this:

>   1:10
 [1]  1  2  3  4  5  6  7  8  9 10

I've looked at ?sink and searched R-help. I've also read: maintaining an input / output log in R

If it makes a difference, I'm using StatET and Eclipse.

Program answered 6/6, 2010 at 7:19 Comment(0)
T
19
library(TeachingDemos)

txtStart("temp.txt")
1:10
txtStop()

The text file now looks like

> 1:10
 [1]  1  2  3  4  5  6  7  8  9 10
Trichomonad answered 6/6, 2010 at 9:1 Comment(2)
Thanks. It seems to work well. I'm surprised that a similar function is not a part of Base R.Program
@George Dontas, is this still necessary, 5 years on ? Can it sink help( "xx" ) to a file ?Tralee
R
6

If you save an R script file instead of inputing your commands into an interactive session, you can also run the following from the command line (on *nix systems):

R --no-save --quiet < /path/to/script.R > /path/to/output.txt

This will create the file "output.txt" and save all input (including comments) and output from the R script into it.

Rocketry answered 27/6, 2013 at 18:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.