From luke-tierney at uiowa.edu  Tue May 31 15:16:16 2011
From: luke-tierney at uiowa.edu (luke-tierney at uiowa.edu)
Date: Tue, 31 May 2011 08:16:16 -0500
Subject: [Rd] assignInNamespace and new bindings
In-Reply-To: <4DE4DA8D.9050000@r-enthusiasts.com>
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>
	<4DE4DA8D.9050000@r-enthusiasts.com>
Message-ID: <alpine.DEB.2.00.1105310813190.2045@luke-inspiron>

Also note at the beginning of of th help file:

      Utility functions to access and replace the non-exported functions
      in a name space, for use in developing packages with name spaces.
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This is intended only as a developer convenience, not as something to
be used in production code. It is quite deliberate the name spaces are
intended to be read-only once created. This allows the compiler to
make assumptions about functions/constants defined in a name space.
For now the compiler only uses this infomation for the base packages,
but that could change in the future.

Best,

luke

On Tue, 31 May 2011, Romain Francois wrote:

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

-- 
Luke Tierney
Statistics and Actuarial Science
Ralph E. Wareham Professor of Mathematical Sciences
University of Iowa                  Phone:             319-335-3386
Department of Statistics and        Fax:               319-335-3017
    Actuarial Science
241 Schaeffer Hall                  email:      luke at stat.uiowa.edu
Iowa City, IA 52242                 WWW:  http://www.stat.uiowa.edu


[Rd] assignInNamespace and new bindings
luke-tierney at uiowa.edu