"$<-" fails (invalid subscript type 'language')

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

"$<-" fails (invalid subscript type 'language')

Vitalie S.-2

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'

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.

Thanks,
Vitalie.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: "$<-" fails (invalid subscript type 'language')

Vitalie S.-2

[hidden email] (Vitalie S.) writes:
> 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'
>

I can partially solve this with do.call, but there is another problem. Quoted
expressions are evaluated during the assignment:

te <- new.env()
> tq <- quote(print("!!!!"))
> do.call("$<-", list(te, "aa", tq))
[1] "!!!!"

This is some weird do.call and $<- interaction. Does not happen with other
functions.

> 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.
>
> Thanks,
> Vitalie.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: "$<-" fails (invalid subscript type 'language')

David Winsemius
In reply to this post by Vitalie S.-2

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?

  `$<-`(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.

--
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.
>
> Thanks,
> Vitalie.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: "$<-" fails (invalid subscript type 'language')

Vitalie S.-2
David Winsemius <[hidden email]> 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.
>>
>> Thanks,
>> Vitalie.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: "$<-" fails (invalid subscript type 'language')

Simon Urbanek

On Oct 24, 2010, at 4:21 PM, Vitalie S. wrote:

> David Winsemius <[hidden email]> 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).
>

No, as David pointed out the documentation tells you unmistakably:
"[...] The main difference is that ‘$’ does not allow computed indices [...]"
so you want to use `[[` instead since `$` is defined exactly to not allow just what you're doing: the index argument must be a symbol or a character vector of length one - anything else is an error as you see.

Cheers,
Simon


>> `$<-`(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.
>>>
>>> Thanks,
>>> Vitalie.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: "$<-" fails (invalid subscript type 'language')

Peter Dalgaard-2
In reply to this post by Vitalie S.-2
On 10/24/2010 10:21 PM, Vitalie S. wrote:

> David Winsemius <[hidden email]> 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: [hidden email]  Priv: [hidden email]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: "$<-" fails (invalid subscript type 'language')

Vitalie S.-2

> 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.
>
Sorry, I was somewhat sloppy and in imprecise. For my class, I really need `$` to behave
as usually [[ behaves in R. So to say, to evaluate the "name" argument.  To be
short, my "$" is part of an user interface (lots of checks, very slow), [[ and
[  are left unattached and are part of "programing" interface for  my classes.

There are not to many symbols in R to use as accessors of the internals of
the objects. Wouldn't  it be natural that the programmer to decide
how to implement his "$" for himself?

> 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".
>

I would expect the following to behave identically:

> tv <- c(a="dsf", b="sss")
> tl <- list(232)

> `$<-`(tl, tv[[1]], "sdfdsfdsfsd")
Error: invalid subscript type 'language'

> tl$`tv[[1]]` <- "sdfdsfdsfsd"
> tl
[[1]]
[1] 232

$`tv[[1]]`
[1] "sdfdsfdsfsd"

So what is relay going on? In one case the name is expected, in another case any
"language" object would work?


> 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 "[[<-".

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: "$<-" fails (invalid subscript type 'language')

Vitalie S.-2
In reply to this post by Simon Urbanek
Simon Urbanek <[hidden email]> writes:

> On Oct 24, 2010, at 4:21 PM, Vitalie S. wrote:
>
>> David Winsemius <[hidden email]> 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).
>>
>
> No, as David pointed out the documentation tells you unmistakably:
> "[...] The main difference is that ‘$’ does not allow computed indices [...]"
> so you want to use `[[` instead since `$` is defined exactly to not allow just what you're doing: the index
> argument must be a symbol or a character vector of length one - anything else is an error as you see.
>

Oh,  that was really stupid from my part. I meant that functionality for my
specific class, not for lists of course.

For list I would expect,

`$<-`(tl, tv[[1]], "sdfdsfdsfsd")
 tl$`tv[[1]]` <- "sdfdsfdsfsd"

to give the same result.

I just gave this artificial example with lists to illustrate the error. Didn't
want to bring the definition of my classes here.

Vitalie.

> Cheers,
> Simon
>
>>> `$<-`(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.
>>>>
>>>> Thanks,
>>>> Vitalie.
>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel>
>>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: "$<-" fails (invalid subscript type 'language')

Vitalie S.-2
In reply to this post by Vitalie S.-2
[hidden email] (Vitalie S.) writes:

>
> I would expect the following to behave identically:
>
>> tv <- c(a="dsf", b="sss")
>> tl <- list(232)
>
>> `$<-`(tl, tv[[1]], "sdfdsfdsfsd")
> Error: invalid subscript type 'language'
>
>> tl$`tv[[1]]` <- "sdfdsfdsfsd"
>> tl
> [[1]]
> [1] 232
>
> $`tv[[1]]`
> [1] "sdfdsfdsfsd"
>
> So what is relay going on? In one case the name is expected, in another case any
> "language" object would work?


>
>> 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 "[[<-".

Yap, I see now, `tv[[1]]` is a name. So no contradiction.

To conclude, only the symbol is allowed as index and I can not do absolutely
anything to make my custom "$" "smarter":(.

Thanks everyone for helping me filter some noise in my head.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: "$<-" fails (invalid subscript type 'language')

Simon Urbanek
In reply to this post by Vitalie S.-2

On Oct 25, 2010, at 3:51 AM, Vitalie S. wrote:

> Simon Urbanek <[hidden email]> writes:
>
>> On Oct 24, 2010, at 4:21 PM, Vitalie S. wrote:
>>
>>> David Winsemius <[hidden email]> 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).
>>>
>>
>> No, as David pointed out the documentation tells you unmistakably:
>> "[...] The main difference is that ‘$’ does not allow computed indices [...]"
>> so you want to use `[[` instead since `$` is defined exactly to not allow just what you're doing: the index
>> argument must be a symbol or a character vector of length one - anything else is an error as you see.
>>
>
> Oh,  that was really stupid from my part. I meant that functionality for my
> specific class, not for lists of course.
>
> For list I would expect,
>
> `$<-`(tl, tv[[1]], "sdfdsfdsfsd")
> tl$`tv[[1]]` <- "sdfdsfdsfsd"
>
> to give the same result.
>

Nope, you're passing different arguments - to make them equivalent you have to use

`$<-`(tl, `tv[[1]]`, "sdfdsfdsfsd")

which works -- you need to quote the symbol in both cases not just one ;).

Cheers,
Simon



> I just gave this artificial example with lists to illustrate the error. Didn't
> want to bring the definition of my classes here.
>
> Vitalie.
>
>> Cheers,
>> Simon
>>
>>>> `$<-`(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.
>>>>>
>>>>> Thanks,
>>>>> Vitalie.
>>>
>>> ______________________________________________
>>> [hidden email] mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-devel>
>>>
>
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel