[R] how to get a primitive function object
Duncan Murdoch
murdoch at stats.uwo.ca
Thu Jan 22 23:38:48 CET 2009
On 22/01/2009 4:50 PM, Yi Zhang wrote:
>> i was sort-of joking, though it's a real option if you want it.
>>
>> but seriously, there's no reason for the &%#* lamenting:
>>
>> x <- 1
>> '<-' = function(x,y) 0
>> x <- 2
>> # 0
>>
>> .Primitive('<-')(x,2)
>> x
>> # 2
>>
>> base::'<-'(x, 3)
>> x
>> # 3
>>
>> base::'<-'('<-', base::'<-')
>> x <- 4
>> x
>> # 4
>>
>> vQ
>>
>
> I'm still not sure if this can help solve my problem. If I want to
> overwrite the `<-` operator for a particular type of objects, but
> preserve its effect for other regular objects (in other words, if I do
> x<-5 with my new <-, x will be 5 and nothing strange happens), then
> what should I put in the [ ] below?
> old <- `<-`
> old(`<-`, function(x, value){
> if (value is my type) do something
> [ ] # to bind the object value to symbol x: no matter what you do
> here, the binding/assignment is local within this function?!
> })
>
> Hope I have made myself clear. Thanks,
You can use parent.frame() as the pos or envir argument to assign(), and
then the assignment happens in the caller's frame. And assign() is also
another way out if you overwrite <- with something that doesn't work;
just call it to reassign base::`<-` to it. Or just rm(`<-`) to unhide
the original one.
Duncan Murdoch
More information about the R-help
mailing list