[R-pkg-devel] environment scoping

William Dunlap wdunlap at tibco.com
Thu Oct 27 18:29:42 CEST 2016


If I were writing a package for factoring integers I might store a vector
of known primes in an environment in my package and have my factoring
functions append to the list when they find some more primes.  This works
because there is only one set of of primes (given we stick with ordinary
integers).

However the term structure (of interest rates, I assume) depends on the
date, the county, the type of bond, the method used to estimate it, etc.  I
think it would be better to store it in some sort of object (function,
list, or environment) that the user would be expected to know about.  The
user would be expected to pass that data into each function that need that
information (via the argument list, by using a function made in the
environment containing the term structure, etc.).


Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Thu, Oct 27, 2016 at 7:55 AM, Jenny Bryan <jenny at stat.ubc.ca> wrote:

> Hi Glenn,
>
> It sounds like you should create an environment for your package. If you
> store these objects in the global environment, they are exposed to the user
> and, more importantly, user could modify or delete them. If you use an
> environment specific to your package, you can be sure you are the only one
> messing with them.
>
> You create it like so:
>
> .package_env <- new.env(parent = emptyenv())
>
> And read from / write into it like so (very much like a list):
>
> .package_env$foo <- ...
> .package_env$foo
>
> or with assign() and get():
>
> assign(foo, bar, envir = .package_env)
> get(foo, envir = .package_env)
>
> This blog post by Jeff Allen is a nice write-up of what you're trying to
> do:
>
> http://trestletech.com/2013/04/package-wide-variablescache-in-r-package/
>
> -- Jenny
>
> > On Oct 27, 2016, at 7:05 AM, Glenn Schultz <glennmschultz at me.com> wrote:
> >
> > All,
> >
> > I would like to have some inputs available to many functions.  For
> example, I have coefficient for a term structure fit which I would like to
> make a available to total return functions.  Thereby eliminating the need
> to fit the same term structure over and over again.  However, I still
> reading and researching scoping.  My understanding is that I could fit the
> term structure and keep the coefficients in the global environment which
> would then make those coefficients available to all functions requiring a
> term structure object input.  Am I correct in my understanding.  What is
> the downside of environment global, if any?
> >
> > Best Regards,
> > Glenn
> > ______________________________________________
> > R-package-devel at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-package-devel
>
> ______________________________________________
> R-package-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-package-devel
>

	[[alternative HTML version deleted]]



More information about the R-package-devel mailing list