[Rd] assignInNamespace and new bindings

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue May 31 12:01:22 CEST 2011


On Tue, 31 May 2011, Prof Brian Ripley wrote:

> On Tue, 31 May 2011, Romain Francois wrote:
>
>> Hello,
>> 
>> assignInNamespace refuses to assign an object to a name that is not already 
>> used in the namespace.
>
> That's intentional, and as documented:
>
>     ‘assignInNamespace’ and ‘fixInNamespace’ are invoked for their
>     side effect of changing the object in the name space.
>                    ^^^^^^^^
>
> I very much doubt we want to allow adding objects.

Even clearer, the Note says

      They can only be used to change the values of objects in the name
      space, not to create new objects.


>
>> 
>> Something like this would make it possible:
>> 
>> --- src/library/utils/R/objects.R	(revision 56024)
>> +++ src/library/utils/R/objects.R	(working copy)
>> @@ -252,8 +252,9 @@
>>             stop("environment specified is not a package")
>>         ns <- asNamespace(substring(nm, 9L))
>>     } else ns <- asNamespace(ns)
>> -    if(bindingIsLocked(x, ns)) {
>> -        unlockBinding(x, ns)
>> +    new_binding <- !exists(x,ns)
>> +    if( new_binding || bindingIsLocked(x, ns)) {
>> +        if(!new_binding) unlockBinding(x, ns)
>>         assign(x, value, envir = ns, inherits = FALSE)
>>         w <- options("warn")
>>         on.exit(options(w))
>> 
>> Romain
>> 
>> -- 
>> Romain Francois
>> Professional R Enthusiast
>> +33(0) 6 28 91 30 30
>> http://romainfrancois.blog.free.fr
>> http://romain-francois.com
>> |- http://bit.ly/hdKhCy : Rcpp article in JSS
>> |- http://bit.ly/elZJRJ : Montpellier Comedie Club - Avril 2011
>> `- http://bit.ly/fhqbRC : Rcpp workshop in Chicago on April 28th
>> 
>> ______________________________________________
>> R-devel at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>> 
>
> -- 
> Brian D. Ripley,                  ripley at stats.ox.ac.uk
> Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
> University of Oxford,             Tel:  +44 1865 272861 (self)
> 1 South Parks Road,                     +44 1865 272866 (PA)
> Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595


More information about the R-devel mailing list