From ripley at stats.ox.ac.uk  Wed Jun 27 08:00:34 2007
From: ripley at stats.ox.ac.uk (ripley at stats.ox.ac.uk)
Date: Wed, 27 Jun 2007 08:00:34 +0200 (CEST)
Subject: [Rd] slight anomaly in formals<- ? (PR#9758)
Message-ID: <20070627060034.4570C6597D@slim.kubism.ku.dk>

On Tue, 26 Jun 2007, georgi.boshnakov at manchester.ac.uk wrote:

> Hi,
>
> The R input/output after the following paragraph is from a session with
> version.string R version 2.4.0 Patched (2006-11-23 r39958).

Which is not recent, but this is unchanged since.

The problem is the definition:

> `formals<-`
function (fun, envir = environment(fun), value)
as.function(c(value, body(fun)), envir)
<environment: namespace:base>

and

> c(x, NULL)
$a


$b
[1] 4

$c

I've fixed this for 2.6.0 (it is not critical enough for 2.5.1 due 
tomorrow). If you need the defn for your version, it is

`formals<-` <- function (fun, envir = environment(fun), value)
{
     bd <- body(fun)
     as.function(c(value, if(is.null(bd)) list(bd) else bd), envir)
}


>
> The last element, x$c, of x has no value and after the assignment of x 
> to the formals of f, x$c does not become a formal argument of f. The 
> second assignment does the job. It seems that x$c actually becomes the 
> body of f after the first assignment. There is no problem if the body of 
> f is not NULL (this probably explains why the second time the assignment 
> to the formals of f works as expected.
>
>> x <- alist(a=,b=,c=)
>> x$b <- 4

Or just x <- alist(a=,b=4,c=)

>> f <- function(x) NULL
>> f
> function(x) NULL
>> formals(f) <- x
>> f
> function (a, b = 4)
>
>> formals(f) <- x
>> f
> function (a, b = 4, c)
>
>> body(f)
>
>
>

-- 
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] slight anomaly in formals<- ? (PR#9758)
ripley at stats.ox.ac.uk