On Tue, Mar 31, 2009 at 12:41 PM, Martin Morgan <mtmorgan@fhcrc.org> wrote:

> I don't konw about preferred, but one method is
>
> pkgVars <- local({
>    x <- NULL
>    list(getX=function() x, setX=function(value) x <<- value)
> })
>
> with use
>
> > pkgVars$getX()
> ...
> This introduces a different programming paradigm (a list of functions)
> that might not be familiar for end-users and is not readily amenable
> to documentation.


Why "not readily amenable to documentation"?  If the user doesn't need to
understand the structure, he can consider the "$" as simply part of the
function name.  And I wouldn't name it pkgVars but pkg.


> A probably better way is
>
> pkgVars <- new.env(parent=emptyenv())
> getX <- function() get("x", pkgVars, inherits=FALSE)
> setX <- function(value) assign("x", value, envir=pkgVars)
>

I think a simpler, clearer way to make getX etc. globally visible is:

  local({
   x <- NULL
   getX <<- function() x
   setX  <<- function(value) x <<- value
 })

To my taste, this is much cleaner than explicitly referencing environments.

            -s

	[[alternative HTML version deleted]]

