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: abielr@gmail.com
cell: 541-514-1115
[[alternative HTML version deleted]]
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.
See the Object class in the R.oo package. /Henrik On Thu, Aug 14, 2008 at 7:35 PM, Abiel Reinhart <abielr 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: abielr at gmail.com > cell: 541-514-1115 > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >