[R] dribble for R?

Duncan Temple Lang duncan at research.bell-labs.com
Tue Mar 5 16:34:25 CET 2002


Johann Petrak wrote:
> Is there something like the LISP function dribble in R?
> This would log all the output to a file but still show it
> on the terminal too.
> 
> sink just diverts the output, but I dont see a way
> to do what dribble would do?
> 
> Johann

I don't know of a way of duplicating _all_ output in the current
mechanism directly from R. Perhaps using ESS (transcript mode or simply the 
R shell buffer) in Emacs is what you want.

In the longer term, having richer output streams in R is something
that may be added (perhaps with a more event/listener based
architecture as in some othe systems). This is useful when embedding R
in other systems, and passively producing output in different formats
such as XML, HTML, TeX, etc.

In the interim, the code below provides a way to duplicate the output
from the results of top-level tasks, i.e commands at the prompt,
within an R session. Basically, one can register an R function that
gets called each time a top-level task has been completed, and it is
called with a) the command/expression, b) the value, c) whether the
command completed normally or with an error, and d) whether the result
was printed or not.

One register's the function with a call to addTaskCallback().
Having defined tee() as below, you give the command

 addTaskCallback(tee("myRLogFile"))

The resulting function that gets registered is the out() function in
the definition of tee() and prints both the expression and value of
any command that completed normally and produced output that was
printed. You can adapt it to output what you want.

This won't duplicate error and warning messages, calls to cat() &
print() within functions.


Hope this helps a little. 

tee <- function(fileName="R.log")
{
  # Open the file and put an identifier at the top.
  # Could also append to an existing file if it is already there.
  con <- file(fileName,"w")
  cat("Log of R output", date(), "\n", file=con)
  
  out <-
    function(expr, value, ok, visible) {
        # only show the commands and output for
        # visible output.
      if(ok && visible) {
        # redirect output to the log file temporarily
       sink(con)
       on.exit(sink()) # make certain we revert back to usual output stream

       cat("\n\n", file=con) # put a separator between the expressions, perhaps number them

       try({print(expr) ; print(value)}) # print the expression and value
      }
       # Make certain to return true so that the callback
       # stays around for the next task. Returning FALSE
       # causes it to be removed.
      return(TRUE)
    }

  out
}

D.



> 
> 
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

-- 
_______________________________________________________________

Duncan Temple Lang                duncan at research.bell-labs.com
Bell Labs, Lucent Technologies    office: (908)582-3217
700 Mountain Avenue, Room 2C-259  fax:    (908)582-3340
Murray Hill, NJ  07974-2070       
         http://cm.bell-labs.com/stat/duncan
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list