Re: [Rd] what is the preferred method to create a package local variable?

From: Philippe Grosjean <phgrosjean_at_sciviews.org>
Date: Tue, 31 Mar 2009 21:51:20 +0200

The best way is to have those variable hidden in the package's workspace, as explained by Roger Peng.

However, if you like to use a mechanism managing an environment specifically dedicated to temporary variables very easily, look at assignTemp() and getTemp() from svMisc package. The advantage is an easier sharing of such variables between different packages (plus the bonus of easy management of default values, overwriting or not of current content if the variable already exists, ...). The temporary environment (TempEnv) is always located in the forelast position just before 'base'.

In any cases, avoid using .GlobalEnv and the ugly <<- for that purpose. Best,

Philippe Grosjean

Roger Peng wrote:
> 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
>>

>
>

>

R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Tue 31 Mar 2009 - 18:55:18 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 02 Apr 2009 - 13:31:21 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive