From romain at r-enthusiasts.com  Tue May 31 14:09:49 2011
From: romain at r-enthusiasts.com (Romain Francois)
Date: Tue, 31 May 2011 14:09:49 +0200
Subject: [Rd] assignInNamespace and new bindings
In-Reply-To: <alpine.LFD.2.02.1105311100290.32362@gannet.stats.ox.ac.uk>
References: <4DE4A82E.7060803@r-enthusiasts.com>	<alpine.LFD.2.02.1105311044280.28857@toucan.stats.ox.ac.uk>
	<alpine.LFD.2.02.1105311100290.32362@gannet.stats.ox.ac.uk>
Message-ID: <4DE4DA8D.9050000@r-enthusiasts.com>

Le 31/05/11 12:01, Prof Brian Ripley a ?crit :
> 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.

Sure.

I'll just keep using this small workaround, unexported from the next Rcpp:

# just like assignInNamespace but first checks that the binding exists 

forceAssignInNamespace <- function( x, value, env ){
     is_ns <- isNamespace( env )
     if( is_ns && exists( x, env ) && bindingIsLocked(x, env ) ){
         unlockBinding( x, env )
     }

     assign( x, value, env )

     if( is_ns ){
         lockBinding( x, env )
     }
}

I find this useful for when a package wants to assign in its own namespace.

Romain

>>> 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
>
>
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel


-- 
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



[Rd] assignInNamespace and new bindings
Romain Francois