From pburns at pburns.seanet.com  Sat Apr 30 22:28:10 2005
From: pburns at pburns.seanet.com (Patrick Burns)
Date: Sat Apr 30 22:29:05 2005
Subject: [Rd] formals assignment now strips attributres
In-Reply-To: <x2ekcstq4f.fsf@turmalin.kubism.ku.dk>
References: <42736B4B.3080507@pburns.seanet.com>
	<x2ekcstq4f.fsf@turmalin.kubism.ku.dk>
Message-ID: <4273EA5A.2060503@pburns.seanet.com>

Peter Dalgaard wrote:

>Patrick Burns <pburns@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).
>
>  
>


[Rd] formals assignment now strips attributres
Patrick Burns