From ripley at stats.ox.ac.uk  Sun Jan  7 15:45:37 2007
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Sun, 7 Jan 2007 14:45:37 +0000 (GMT)
Subject: [Rd] Scoping issues
In-Reply-To: <20070107135726.QSPQ11361.tomts43-srv.bellnexxia.net@JohnDesktop8300>
References: <20070107135726.QSPQ11361.tomts43-srv.bellnexxia.net@JohnDesktop8300>
Message-ID: <Pine.LNX.4.64.0701071435220.12570@gannet.stats.ox.ac.uk>

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

If you really don't want to do that, you cannot easily use source() with 
such an environment as RcmdrEnv(), but you can use sys.source().


On Sun, 7 Jan 2007, John Fox wrote:

> 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 hope this helps,
> 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-devel-bounces at r-project.org
>> [mailto:r-devel-bounces at r-project.org] On Behalf Of Chris Eisley
>> Sent: Sunday, January 07, 2007 12:29 AM
>> To: r-devel at r-project.org
>> Subject: [Rd] Scoping issues
>>
>> Hi everyone,
>>
>> I'm trying to split up a large R file that implements a
>> tcl/tk gui into several smaller ones.  The problem is that
>> the main GUI creation function defines many tclVars and
>> tkframes that need to be accessible to functions that are
>> currently defined in main function, but which I'd like to
>> move to different source files.  These files make up a
>> package, so it seems that using source() is a bad idea.  I
>> could try argument passing, but there are a lot of arguments
>> to pass in some cases.
>>
>> The lexical scoping is hurting me here, but I assume there's
>> a good way to deal with this kind of issue.  Any help is
>> greatly appreciated.
>>
>> -Chris

-- 
Brian D. Ripley,                  ripley at 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 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



[Rd] Scoping issues
Prof Brian Ripley