R - capture output from system() to a txt file.

454 Views Asked by At

I want to capture all output from R console. I tried to use sink() function and txtStart() of library 'TeachingDemos'. However, none of them can capture the output from system() command.

For example If I run the below codes:

zz <- file("log.txt")
sink(zz)
sink(zz, type = "message")
print('first layer message!!!!')
system("Rscript test1.R") #test1.R is a R script that print 'hello world'
sink(type = "message")
sink()

I can see the message 'hello world' in the R console. However, I cannot write it into log.txt. Is there any way to solve this?

Thanks

1

There are 1 best solutions below

0
On
system("Rscript test1.R", intern = TRUE)