From ripley at stats.ox.ac.uk  Tue May 31 11:47:02 2011
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Tue, 31 May 2011 10:47:02 +0100
Subject: [Rd] assignInNamespace and new bindings
In-Reply-To: <4DE4A82E.7060803@r-enthusiasts.com>
References: <4DE4A82E.7060803@r-enthusiasts.com>
Message-ID: <alpine.LFD.2.02.1105311044280.28857@toucan.stats.ox.ac.uk>

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.

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


[Rd] assignInNamespace and new bindings
Prof Brian Ripley