[R] Can you turn a string into a (working) symbol?

R. Michael Weylandt michael.weylandt at gmail.com
Mon Nov 5 11:30:44 CET 2012


On Sun, Nov 4, 2012 at 3:10 AM, andrewH <ahoerner at rprogress.org> wrote:
> Yes, the assign command goes a little way toward what what I was hoping for.
> But it requires a different syntax, and it does not in general let you use
> quoted expressions that you could  use with other assignment operators. For
> instance,
>
>> DD <- 1:3
>> assign("DD[2]", 5)
>> DD
> [1] 1 2 3
>
> So I am still looking for a function that produces an output that is fully
> equivalent to the string without quotation marks.  Or for a definite
> statement that no such function can exist.
>

I'm not sure what you mean without quotation marks, but here's a germ
of a solution along your original doppl:

doppl <- function(x) get(x, 1)
`doppl<-` <- function(x, value) assign(x, value, 1)

## Note that I don't have the search path right and it really should
## have a parent.frame / sys.call things in there, but I'm not at my
## own machine right now to test.

However, I don't think you can use this idiom as easily:

doppl(x)[3] <- 5

because complex sub-assignment does some funny things. Happy to be
proven wrong.

Cheers,
Michael




More information about the R-help mailing list