Re: [Rd] Scoping issues

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Mon 08 Jan 2007 - 19:15:29 GMT

On 1/8/07, Chris Eisley <chris@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



R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Tue Jan 09 16:51:29 2007

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Tue 09 Jan 2007 - 07:31:12 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.