[R] Convert text string to object pointer

caddr pavel.obraztsov at gmail.com
Fri Aug 15 09:38:43 CEST 2008


Try this:
x <- 1
z <- as.name ("x")
eval (call ("<-", z, call ("attr<-", z, "foo", "bar")))
x
[1] 1
attr(,"foo")
[1] "bar"

On 15 авг, 06:35, "Abiel Reinhart" <abi... at gmail.com> wrote:
> I would like to use a text string to get a reference to an object whose name
> is the text string. I have seen people using get() for this purpose, but as
> far as I can tell this returns a copy of the object, not a pointer to the
> object. For instance, if I were to write
>
> x <- get("z")
> attr(x, "age") <- "fifty"
>
> Then x would have the attribute "age" attached to it, but z would not. I
> would like z to change when I change x. Does anyone have a suggestion on how
> to do this? The context for this question is an application in which object
> attributes are listed in an XML file, with each set of attributes having a
> key which refers to the name of an object in R.
>
> Thank you,
>
> --
> Abiel Reinhart
> email: abi... at gmail.com
> cell: 541-514-1115
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-h... at r-project.org mailing listhttps://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list