We want to log the commands and results of a R script into a text report file. The pipe into the text file works well with sink(), but not within a for loop.
The script is called with
source("myscript.r",echo=TRUE)
We need the loop to extract all rows of a data.frame consecutively into a vector and do some vector based analysis with each vector. Here's a short example:
#pipe output to file
sink("myfile.txt",append=TRUE,split=TRUE)
#some data
c1<-rnorm(10,mean=90,sd=10)
c2<-rnorm(10,mean=75,sd=8)
c3<-rnorm(10,mean=98,sd=12)
#data in a data.frame
cData<-data.frame(c1,c2,c3)
#print data.frame
cData
#loop over frame
for (i in 1:ncol(cData))
{
#extract vector
x<-cData[,i]
#do something with vector
n = length(x)
#... more code
#print result
print(n)
}
#close output
sink()
I tried it with sink() and txtStart() but sink() truncates the commands and puts results after the loop, txtStart() seems to repeat the commands but not the results.
I looked also at brew, but I just need a text file, nothing formatted.
Any help is appreciated.
Michael