[R] Adding meta-data when creating objects. e.g: changing "<-" so to (for example) add "creation time" - how-to and pros/cons?

Michael Bedward michael.bedward at gmail.com
Fri Nov 12 11:17:13 CET 2010


Hi Ivan,

> I had already seen your solution (that does work).
> If you're right about the issue in "my" function, then the error message is
> confusing ('could not find the function "get<-" '). Moreover, I assign()ed
> and get() "x" from the .GlobalEnv, so there shouldn't be a problem with
> scoping, right?

Ah, my apologies. I didn't notice that you were passing in a character
string as the "x" arg rather than a name as in my version.

Consider this...

# from prompt, create object and set attribute
foo <- 123
attr(foo, "bar") <- 42

# now this will work without error
attr(get("foo"), "bar")

# but this won't
attr(get("foo"), "bar") <- 99

It provokes the error: "target of assignment expands to non-language
object". This is a bit different to the error you get trying the same
thing from within your function, but I'm guessing that both are caused
by the same thing. Perhaps the get function call is not fully
evaluated before the assignment is attempted in the attr function ? As
I say, I'm just guessing here.

> It's just that R doesn't behave as I expect, so I'd like to understand where
> I'm wrong.

Me too but I suspect that we'd probably have to delve into the sources
to work it out - or better, hope that someone else here can tell us
the answer :)

Michael



More information about the R-help mailing list