From rdpeng at gmail.com  Tue Mar 31 21:35:46 2009
From: rdpeng at gmail.com (Roger Peng)
Date: Tue, 31 Mar 2009 15:35:46 -0400
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: <66f3bd910903311235w5421c11ap357ced8a6706f985@mail.gmail.com>

I usually use environments for this. So, in one of the R files for the
package, just do

.localstuff <- new.env()

Then, in functions you can do things like

.localstuff$bbg.db.conn <- dbConnect(...)

-roger

On Tue, Mar 31, 2009 at 11: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
>



-- 
Roger D. Peng  |  http://www.biostat.jhsph.edu/~rpeng/



[Rd] what is the preferred method to create a package local variable?
Roger Peng