[Rd] Scoping issues

Chris Eisley chris at cepheus.net
Mon Jan 8 19:50:47 CET 2007

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 {
	ui.widget1 <- tkframe(...)
	ui.widget2 <- tkframe(...)
	ui.widget3 <- tkframe(...)

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 

> 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.


More information about the R-devel mailing list