Closed MichaelChirico closed 4 years ago
From: Peter Dalgaard <p.dalgaard@<::CENSORED -- SEE ORIGINAL ON BUGZILLA::>> fabian.scheipl@<::CENSORED -- SEE ORIGINAL ON BUGZILLA::>-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 <CENSORING FROM DETECTED PHONE NUMBER ONWARDS; SEE BUGZILLA>
From: Prof Brian Ripley <ripley@<::CENSORED -- SEE ORIGINAL ON BUGZILLA::>> 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@<::CENSORED -- SEE ORIGINAL ON BUGZILLA::>-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
<CENSORING FROM DETECTED PHONE NUMBER ONWARDS; SEE BUGZILLA>
-----------
#### METADATA
- Comment author - Jitterbug compatibility account
- Timestamp - 2008-12-13 17:14:38 UTC
NOTES: fixed for 2.9.0 (docs and semantics)
Audit (from Jitterbug): Wed May 27 07:21:16 2009 ripley changed notes Wed May 27 07:21:16 2009 ripley moved from incoming to Documentation-fixed
From: fabian.scheipl@<::CENSORED -- SEE ORIGINAL ON BUGZILLA::>-muenchen.de 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:
################################
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.
METADATA