Re: [R] tcl/tk return problem

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Wed 31 Aug 2005 - 21:18:08 EST


On Wed, 31 Aug 2005, Philippe Grosjean wrote:

> deggle wrote:
> > Hello,
> >
> > I'm very new in working with tcl/tk in R and have a problem which will
> > probably
> > sound silly to most of you.
> > Here is the code I have problems with:
> >
> > readcelfiles <- function()
> > {
> > require(tcltk)
> > tt <- tktoplevel()
> > tkgrid(tklabel(tt,text="Choose a directory!"))
> >
> > OnOK <- function()
> > {
> > fileDir<-tclvalue(tkchooseDirectory())
> > data.raw <- ReadAffy(celfile.path=fileDir)
> > #return(data.raw)
> > }
> >
> > OK.but <- tkbutton(tt,text="OK",command=OnOK)
> > tkgrid(OK.but)
> > tkfocus(tt)
> > }
> >
> > So after clicking on my "OK" button, I choose my directory and read the
> > cel-files.
> > But now I want to return the object to my workspace ... "return" doesn't
> > work here.

>

> I suppose you mean in the User Workspace. Your OnOK function should look
> like that:
>

> OnOK <- function() {
> fileDir<-tclvalue(tkchooseDirectory())
> data.raw <<- ReadAffy(celfile.path=fileDir)
> }
>

> Note that the function overwrites any existing 'data.raw', so this could
> be dangerous. Writting directly in the User Workspace is not advised
> from inside a function, but here, it is the simplest way to return a
> result from a tk widget action.

Maybe simplest, but not a very good way. See R_SOURCES/src/library/tcltk/R/utils.R for ideas on how to write a modal dialog box that returns the value selected.

One problem with <<- is that it does not necessarily write in the workspace. You need

   assign("data.raw", ReadAffy(celfile.path=fileDir), envir=.GlobalEnv)

to be sure of that. (The example code I quote does use <<- but in a controlled way.)

-- 
Brian D. Ripley,                  ripley@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
R-help@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
Received on Wed Aug 31 21:26:57 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:40:00 EST