[R] Output from function to a tcltk window

John Fox jfox at mcmaster.ca
Fri Feb 4 13:40:29 CET 2005


Dear Peter and Henrik,

What the Rcmdr does may be overkill for Henrik's application, since it also
intercepts error and warning messages, and tries to take the behaviour of
the R console. The relevant functions are in the file Commander.R in the
source package; the principal one is:

doItAndPrint <- function(command, log=TRUE) {
    messages.connection <- textConnection(".messages", open="w")
    sink(messages.connection, type="message")
    output.connection <- textConnection(".Output", open="w")
    sink(output.connection, type="output")
    on.exit({
        sink(type="message")
        if (!.console.output) sink(type="output") # if .console.output,
output connection already closed
        close(messages.connection)
        close(output.connection)
        })
    if (log) logger(command)
    result <-  try(eval(parse(text=command), envir=.GlobalEnv), silent=TRUE)
    if (class(result)[1] ==  "try-error"){
        tkmessageBox(message=paste("Error:",
            strsplit(result, ":")[[1]][2]), icon="error")
        if (.console.output) sink(type="output")
        tkfocus(.commander)
        return()
        }
    if (isS4object(result)) show(result) else print(result)
    if (.Output[length(.Output)] == "NULL") .Output <-
.Output[-length(.Output)] # suppress "NULL" line at end of output
    if (length(.Output) != 0) {  # is there output to print?
        if (.console.output) {
            out <- .Output
            sink(type="output")
            for (line in out) cat(paste(line, "\n", sep=""))
            }
        else{
            for (line in .Output) tkinsert(.output, "end", paste(line, "\n",
sep=""))
            tkyview.moveto(.output, 1)
            }
        }
    else if (.console.output) sink(type="output")
    checkWarnings(.messages)  # errors already intercepted, display any
warnings
    result
    }

Regards,
 John

--------------------------------
John Fox
Department of Sociology
McMaster University
Hamilton, Ontario
Canada L8S 4M4
905-525-9140x23604
http://socserv.mcmaster.ca/jfox 
-------------------------------- 

> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch 
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Peter Dalgaard
> Sent: Friday, February 04, 2005 5:21 AM
> To: Henrik Andersson
> Cc: r-help at stat.math.ethz.ch
> Subject: Re: [R] Output from function to a tcltk window
> 
> Henrik Andersson <h.andersson at nioo.knaw.nl> writes:
> 
> > I would like to display output to a tcltk window from e.g. 
> a call to 
> > summary().
> > 
> > 
> > 
> > I tried to get something else than oneliners into a text 
> window of the 
> > kind found at:
> > 
> > 
> http://bioinf.wehi.edu.au/~wettenhall/RTclTkExamples/TextWindows.html
> > 
> > But without success.
> 
> (Rcmdr must be doing this sort of thing already?)
> 
> I'd try this:
> 
> 1) str <- paste(capture.output(summary(myfit),collapse="\n"))
> 
> 2) clone the tkfaq demo (or one of James W.'s examples), but replace
>    the line
> 
>    tkinsert(txt, "end", tkcmd("read", chn))
> 
> with 
> 
>    tkinsert(txt, "end", str)
> 
> -- 
>    O__  ---- Peter Dalgaard             Blegdamsvej 3  
>   c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
>  (*) \(*) -- University of Copenhagen   Denmark      Ph: 
> (+45) 35327918
> ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: 
> (+45) 35327907
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html




More information about the R-help mailing list