[R] Managing global/local variables when creating R packages

Gabor Grothendieck ggrothendieck at gmail.com
Fri Aug 3 06:46:06 CEST 2007


1. This probably translates best from what you have.  It places
the object in the package space itself.  The lattice package illustrates
that.  Download the lattice package code from CRAN and look at
.LatticeEnv defined in zzz.R there.  Also try this:

library(lattice)
lattice:::.LatticeEnv
ls(lattice:::.LatticeEnv)

2. Another approach is to output an object with the data from your
constructor function and pass it through the argument list to
each consumer function.  The object could be implemented as
a list, environment, S3 object, S4 object, proto package object or with the
R.oo package or implicitly as demo(scoping) shows.

On 8/2/07, Bethany Kok <bethanyk at email.unc.edu> wrote:
> I'm working on a package for R to create graphs for modeling
> interactions between latent variables in SEM.  At the moment, I have one
> function which performs the necessary steps to prepare the data for
> graphing, and then separate graphing functions depending on what graph
> type (or types) the user wants.  To get the prepped data from the setup
> function to the graphing functions, I currently put the data into a
> dataframe and declare it as a global variable (<<-).  Is there a way to
> make a data frame or a set of variables available to all the functions
> in a package but NOT to users?  As it is, if users were to use the var
> name of the data frame for something else, the data will be lost and
> they'll have to rerun setup before creating their graphs.
>
> Thanks,
>
> Bethany
>
> --
> Bethany Kok
> Graduate Student
> Department of Psychology, Davie Hall, CB 3270
> University of North Carolina at Chapel Hill
> Chapel Hill, NC 27599
>
> TEL:  919-667-4752
>
> ______________________________________________
> R-help at 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
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list