From ripley at stats.ox.ac.uk  Sat Dec 13 12:20:05 2008
From: ripley at stats.ox.ac.uk (ripley at stats.ox.ac.uk)
Date: Sat, 13 Dec 2008 12:20:05 +0100 (CET)
Subject: [Rd] (PR#13318) Wishlist: fix error in documentation for
Message-ID: <20081213112006.00ACD282EFF0@mail.pubhealth.ku.dk>

  This message is in MIME format.  The first part should be readable text,
  while the remaining parts are likely unreadable without MIME-aware tools.

--27464147-1942928637-1229159653=:5347
Content-Type: TEXT/PLAIN; CHARSET=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 8BIT
Content-ID: <alpine.LFD.2.00.0812130938061.6595 at gannet.stats.ox.ac.uk>

There are two issues here, what it does and what it should do..

One issue is what is meant by 'expression': is it an expressions vector of 
type "expression" or is it 'expression' as documented as an element of 
such a vector in ?expression?  For body<- it is the latter.

Then there are a couple of odd exceptions.

1) If 'value' is of type expression, the first element is taken, silently.

2) If 'value' is a length-one list, its first element is taken.
If 'value' is a named list of length greater than one. the last element is 
taken as the value and the remaining elements as additional arguments.

The first is by design, the second seems to be a bug (and in the case of 
formals<- it definitely is a bug which I have just corrected).  The issue 
is in

> base::`body<-`
function (fun, envir = environment(fun), value)
{
     if (is.expression(value))
         value <- value[[1]]
     as.function(c(formals(fun), value), envir)
}

formals(fun) is a normally a pairlist so c() is concatenating 'value' to a 
list, and will do an implicit as.list().  However, in the case of no 
arguments it fails (another bug I have just corrected).

I think we want this to be

     as.function(c(as.list(formals(fun)), list(value)), envir)

but that does change the behaviour for a length-one list 'value'.  It 
seems a fair guess that no one is intentionally relying on the latter, but 
you never know ....

On Fri, 21 Nov 2008, Peter Dalgaard wrote:

> fabian.scheipl at stat.uni-muenchen.de wrote:
>> Full_Name: Fabian Scheipl
>> Version: 2.8.0
>> OS: Windows, Linux
>> Submission from: (NULL) (138.246.7.150)
>> 
>> 
>> It seems to me that the documentation for body<- is wrong.
>> The help file for 
>> body(fun, envir = environment(fun)) <- value
>> 
>> says that:
>> 
>> value can be an expression or a list of R expressions.
>> 
>> This produces errors however:
>> 
>> ################################
>> 
>>> f <- function(x){}
>>> body(f) <- list(expression(res <- x^2),expression(return(x)))
>> Fehler in as.function.default(c(formals(fun), value), envir) :   ung?ltiges 
>> formales Argument f?r "function"
>> 
>> ################################
>> 
>> 
>> The only way to assign multiple statements to a function body
>> that I could get to work is to put all the statements into a single 
>> expression,
>> (separated by ';' and in curly brackets):
>> 
>> ################################
>> 
>> body(f) <- expression({res <- x^2; return(x)})
>> 
>> ################################
>> 
>> I think the words 'or a list of R expressions.' should be removed.
>
> It's slightly trickier than that. The value _can_ be a list, but only one of 
> length 1. The same actually goes for expressions, but in that case there is 
> explicit code to take only the first element. Also, the distinction between 
> objects of mode "expression" and unevaluated expressions is unclear (here and 
> elsewhere).
>
>> body(f) <- expression(x,y,z)
>> f
> function ()
> x
>> body(f) <- list(quote(2+2))
>> f
> function ()
> 2 + 2
>> body(f) <- list(expression(2+2))
>> f
> function ()
> expression(2 + 2)
>
>
> BTW: Don't use 'wishlist' on things that are real errors.
>
> --
>   O__  ---- Peter Dalgaard             ?ster Farimagsgade 5, Entr.B
>  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
> (*) \(*) -- University of Copenhagen   Denmark      Ph:  (+45) 35327918
> ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)              FAX: (+45) 35327907
>
> ______________________________________________
> 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
--27464147-1942928637-1229159653=:5347--



[Rd] (PR#13318) Wishlist: fix error in documentation for
ripley at stats.ox.ac.uk