From ggrothendieck at gmail.com  Mon Jan  8 20:15:29 2007
From: ggrothendieck at gmail.com (Gabor Grothendieck)
Date: Mon, 8 Jan 2007 14:15:29 -0500
Subject: [Rd] Scoping issues
In-Reply-To: <45A29287.20600@cepheus.net>
References: <20070107135726.QSPQ11361.tomts43-srv.bellnexxia.net@JohnDesktop8300>
	<Pine.LNX.4.64.0701071435220.12570@gannet.stats.ox.ac.uk>
	<45A29287.20600@cepheus.net>
Message-ID: <971536df0701081115k37177a37t7d8e05e1a0034b7a@mail.gmail.com>

On 1/8/07, Chris Eisley <chris at cepheus.net> wrote:
> Hello Prof Ripley,
>
> Prof Brian Ripley wrote:
> > I don't undersand what the issues are, but he actually said
> >
> >>> These files make up a package,
> >
> > and all the source files on an R packages are concatenated and loaded
> > into a single environment.  So my understanding of the answer to
> >
> >>> I assume there's a good way to deal with this kind of issue.
> >
> > is 'yes, build an R package'.
> >
>
> My code is built as a package, but having a package doesn't seem to
> prevent the kind of problem I'm running into.  My code is currently
> structured as such:
>
> ui.main <- function() {
>        ui.func1 <- function {
>                ...
>                do.something(ui.widget1)
>                do.something(ui.widget2)
>                do.something(ui.widget3)
>                ...
>        }
>        ui.widget1 <- tkframe(...)
>        ui.widget2 <- tkframe(...)
>        ui.widget3 <- tkframe(...)
>        ...
>        ui.func1()
> }

Assuming ui.main can find ui.func1 we can create a local
copy of ui.func1 that is the same as the external ui.func
but has its environment set to the environment within ui.main
so that it can find ui.widget1, ui.widget2 and ui.widget3:

ui.main <- function() {
       environment(ui.func1) <- environment()
       ui.widget1 <- tkframe(...)
       ui.widget2 <- tkframe(...)
       ui.widget3 <- tkframe(...)
       ...
       ui.func1()
}

>
> I'd like to move the definition of ui.func1 and those of other functions
> like it to seperate source files, since my main "ui" source file is
> getting quite large.  The variables need to be available to several
> other functions that I'd like to move to separate source files.  Since
> the tkframe variables can't be created in the environment of ui.main,
> I'm not sure how to get around this problem without a lot of argument
> passing.
>
>
> > On Sun, 7 Jan 2007, John Fox wrote:
> >
>
> Hello John,
>
> >> Dear Chris,
> >>
> >> If I correctly understand what you want to do, the Rcmdr package uses the
> >> following functions (slightly modified from original version
> >> contributed by
> >> Philippe Grosjean) to solve a similar problem:
> >>
> >> RcmdrEnv <- function() {
> >>    pos <-  match("RcmdrEnv", search())
> >>    if (is.na(pos)) { # Must create it
> >>        RcmdrEnv <- list()
> >>        attach(RcmdrEnv, pos = length(search()) - 1)
> >>        rm(RcmdrEnv)
> >>        pos <- match("RcmdrEnv", search())
> >>        }
> >>    return(pos.to.env(pos))
> >>    }
> >>
> >> putRcmdr <- function(x, value)
> >>    assign(x, value, envir = RcmdrEnv())
> >>
> >> getRcmdr <- function(x, mode="any")
> >>    get(x, envir = RcmdrEnv(), mode = mode, inherits = FALSE)
> >>
>
> I think I understand what's going on here, but it seems like a rather
> arcane solution to such a basic problem.  It seems as if I'm trying to
> code in a way that's incompatible with R's scoping.
>
> -Chris



[Rd] Scoping issues
Gabor Grothendieck