views:

250

answers:

3

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

A: 

I'm not sure to understand exactly what you want, in particular why you want to put the source code in your text file.

First, I'd better use the *apply family of functions to browse the data frame instead of a for loop, especially lapply and sapply.

With the following code you will get three different presentations of your output. The first one should be similar to what you get with your code. The second and third ones are a bit different, they output values as a list or a data frame.

sink("/tmp/myfile.txt", append=TRUE, split=TRUE)

myfunc <- function(v) {
  variance <- var(v)
  mean <- mean(v)
  return(list(variance=variance,mean=mean))
}

myotherfunc <- function(v) {
  cat("And the mean is:\n")
  print(mean(v))
  cat("And the variance is:\n")
  print(var(v))
}

# results printed directly
invisible(lapply(cData, myotherfunc))

# results as a list
lapply(cData, myfunc)

# results as a data frame
sapply(cData, myfunc)


sink()

Anyway, I don't know if one of these outputs could be what you're looking for.

juba
What I need is a text report which can be printed and attached to a documentation. This report should contain the data frame, source code and results to allow a manual check of the results. The lapply(cData,myfunc) would work, but if myfunc contains any conditions (for, if ...) the printout of the code inside for/if is truncated, e.g.:> myfunc <- function(v) { + n<-length(v)+ if (n>5) + {+ variance <- var(v)+ mean <- mean(v)+ return(list(variance=variance,mean=mean))+ .... [TRUNCATED]> # results as a list> lapply(cData, myfunc)$c1...Thanks for your help.
Michael
A: 

I would recommend not to think in terms of

source("myscript.r",echo=TRUE)

but rather in terms of Rscript (which comes with R)

Rscript myscript.r     # on windows, linux or os x

or in terms of littler

r myscript.r           # on linux or os x

This gives you the ability to query command-line parameters (via CRAN packages getopt and optparse) and much more.

Dirk Eddelbuettel
A: 
Richie Cotton
I agree, the truncation is standard for R and saves paper, but my report needs full source code. Speaking of, the R code could be vectorised but the algorithms come from material testing standards and are easier to understand (for the report reader) when implemented in a for-loop. Finally, I tried Sweave: it works much better than I imagined and the LaTeX-Output looks great.
Michael