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

Vitalie S. spinuvit.list at gmail.com
Mon Oct 25 09:45:40 CEST 2010


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



More information about the R-devel mailing list