From pdalgd at gmail.com  Sun Oct 24 23:02:52 2010
From: pdalgd at gmail.com (Peter Dalgaard)
Date: Sun, 24 Oct 2010 23:02:52 +0200
Subject: [Rd] "$<-" fails (invalid subscript type 'language')
In-Reply-To: <87fwvv7355.fsf@gmail.com>
References: <cpnwrp7zs2t.fsf@gmail.com>	<FB9D7384-795F-4EB1-8ABE-A1C1BD11977D@comcast.net>
	<87fwvv7355.fsf@gmail.com>
Message-ID: <4CC49EFC.7090801@gmail.com>

On 10/24/2010 10:21 PM, Vitalie S. wrote:
> David Winsemius <dwinsemius at comcast.net> writes:
> 
>> On Oct 24, 2010, at 5:35 AM, Vitalie S. wrote:
>>
>>>
>>> This might be just beyond of my understanding of how assignment works in R, but
>>> the documentation does not say anything about:
>>>
>>>> tv <- c(a="dsf", b="sss")
>>>> tl <- list(232)
>>>> `$<-`(tl, tv[[1]], "sdfdsfdsfsd")
>>> Error: invalid subscript type 'language'
>>
>> Are either of these what you should have done to get what it appears you were aiming for but didn't specify?
>>
> 
> I meant what I wrote there. After the assignment, the list tl  should have element 'dsf' with the
> value "sdfdsfdsfsd" (sorry for bad names).
> 
>>  `$<-`(tl, "sdfdsfdsfsd", tv[[1]])
>> # yields
>> [[1]]
>> [1] 232
>>
>> $sdfdsfdsfsd
>> [1] "dsf"
>>
>>> `[<-`(tl, tv[[1]], "sdfdsfdsfsd")
>> [[1]]
>> [1] 232
>>
>> $dsf
>> [1] "sdfdsfdsfsd"
>>
>> The "$" operator does not evaluate the index whereas the "[" function does. And the documentation is quite clear
>> about that distinction.
>>
> 
> If it is evaluated or not it is hardly an explanation for the error. It throws
> the error before the method is even dispatched. If the index (in $'s case the
> name) is unevaluated then my methods should get an expression 'tv[[1]]', which I
> can then handle.
> 
> Example:
> 
> setClass("classX", contains="list")
> setMethod("$<-", "classX",
>           function(x, name, value){
>               print("I am here!!")
>               x
>           })
> 
> x <- new("classX")
> tv <- c("aa", "bb")
> `$<-`(x, tv[[1]], 4343)
> #gives
> Error: invalid subscript type 'language'
> 
> 
>> --
>> David Winsemius.
>>
>>>
>>> This happens even before the method is dispatched. I can not handle the
>>> "name" argument in my S4 method, because it's not even entered.

There are some things you are not really supposed to mess with in R...
Computing the index to $-constructs is one of them (trying to set up a
for loop as a call to `for` is another). It can be done, it's just
rather painful, and most likely not what you wanted in the first place.

The second argument to `$` and `$<-` is passed unevaluated and expected
to be of mode "name" (i.e., a symbol). Trying to compute it inline is
going to pass an unevaluated expression, which has mode "language".

If you insist, you can do things like

eval(bquote(`$<-`(x, .(as.name(tv[[1]])), 4343)))

or similar constructs using substitute().

However, the whole situation suggests that you are really looking for
methods for "[[<-".



-- 
Peter Dalgaard
Center for Statistics, Copenhagen Business School
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



[Rd] "$<-" fails (invalid subscript type 'language')
Peter Dalgaard