Function sink
is the standard R way of redirecting output. It has an argument split
that when set to TRUE
will still have the output of commands print to stdout
.
From the documentation, my emphasis.
split
logical: if TRUE, output will be sent to the new sink and to the current output stream, like the Unix program tee.
The following example displays this behavior.
First a data set to run a command with lots of written output.
set.seed(1234)
x <- 1:10
y <- x + rnorm(10, mean = 2, sd = 4)
These instructions are meant not to mess with my workspace. Feel free to skip them.
old_dir <- getwd()
setwd('~/tmp')
Now the split sink
example.
sink(file = 'sink.txt', split = TRUE)
summary(lm(y ~ x))
sink(NULL)
summary
output to both stdout and the file sink.txt
.
Clean up.
unlink('sink.txt')
And back to where I was.
setwd(old_dir)