From h.wickham at gmail.com  Tue Mar 31 18:53:16 2009
From: h.wickham at gmail.com (hadley wickham)
Date: Tue, 31 Mar 2009 11:53:16 -0500
Subject: [Rd] what is the preferred method to create a package local
	variable?
In-Reply-To: <8ec76080903310845x3b0a0c2cy22d30cd0799a206e@mail.gmail.com>
References: <8ec76080903310845x3b0a0c2cy22d30cd0799a206e@mail.gmail.com>
Message-ID: <f8e6ff050903310953ye9e188eob864616ec00da5ac@mail.gmail.com>

For saving the previous plot in ggplot2, I use the following:

.plot_store <- function() {
  .last_plot <- NULL

  list(
    get = function() .last_plot,
    set = function(value) .last_plot <<- value
  )
}
.store <- .plot_store()

set_last_plot <- function(value) .store$set(value)
last_plot <- function() .store$get()

which is basically equivalent to Martin's first suggestion, with some
wrapper functions.

Hadley

On Tue, Mar 31, 2009 at 10:45 AM, Whit Armstrong
<armstrong.whit at gmail.com> wrote:
> for the moment, I'm using:
>
> .onAttach <- function(libname, pkgname) {
> ? ?.bbg.db.conn <<- dbConnect(dbDriver("PostgreSQL"), user="blah","blah")
> }
>
> .onUnload <- function(libpath) {
> ? ?dbDisconnect(.bbg.db.conn)
> }
>
>
> which results in a hidden global variable in the global environment.
>
> I would prefer to make the assignment only in the package namespace.
> I've looked at assignInNamespace, but I can't seem to make it work.
>
> Is there a preferred method for doing this?
>
> When I try adding an assignment directly in the source file, I get the
> "cannot change value of locked binding" error.
>
> What am I missing?
>
> Thanks,
> Whit
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>



-- 
http://had.co.nz/



[Rd] what is the preferred method to create a package local variable?
hadley wickham