[Rd] formals assignment now strips attributres
Patrick Burns
pburns at pburns.seanet.com
Sat Apr 30 22:28:10 CEST 2005
Peter Dalgaard wrote:
>Patrick Burns <pburns at pburns.seanet.com> writes:
>
>
>
>>The assignment form of 'formals' strips attributes (or something close
>>to that) from the values in the list. This wasn't intentional, was it?
>>
>>The current behavior (2.0.0 through 2.1.0 on Windows at least):
>>
>> > fjj <- function() x
>> > formals(fjj) <- list(x=c(a=2, b=4))
>> > fjj
>>function (x = c(2, 4))
>>x
>>
>>
>>Previous behavior:
>>
>> > fjj <- function() x
>> > formals(fjj) <- list(x=c(a=2, b=4))
>> > fjj
>>function (x = structure(c(2, 4), .Names = c("a", "b")))
>>x
>>
>>
>
>It is only a buglet in deparsing:
>
>
>
>> formals(fjj)
>>
>>
>$x
>a b
>2 4
>
>
>>fjj()
>>
>>
>a b
>2 4
>
But the buglet gets more aggressive if you edit the function:
> fjj <- function() x
> formals(fjj) <- list(x=c(a=2, b=4))
> fjj
function (x = c(2, 4))
x
> fjj()
a b
2 4
> fix(fjj) # do nothing but save
> fjj()
[1] 2 4
I'm quite sure that I wouldn't have noticed if my real function were
not broken.
Now I know that my functions will work if I assign the formals after I
edit the
function -- even though they look like they shouldn't work.
>
>
>>as.list(fjj)
>>
>>
>$x
>a b
>2 4
>
>[[2]]
>x
>
>BTW, why is it that we cannot deparse named vectors nicely?
>
>
>>deparse(c(a=1,b=2))
>>
>>
>[1] "structure(c(1, 2), .Names = c(\"a\", \"b\"))"
>
>
>>deparse(as.list(c(a=1,b=2)))
>>
>>
>[1] "structure(list(a = 1, b = 2), .Names = c(\"a\", \"b\"))"
>
>Notice also that fjj constructed as above is not identical to
>
>function (x = c(a = 1, b = 2))
>x
>
>since the default expression is a vector in one case and a call to "c"
>in the other. This is part of the problem; you're trying to deparse
>something that cannot be the result of parsing. (The existence of such
>objects is a generic problem in the R (and S) language).
>
>
>
More information about the R-devel
mailing list