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

Ivan Calandra ivan.calandra at uni-hamburg.de
Thu Nov 11 12:17:59 CET 2010


Hi,

I have tried this (just to practice):
assign2 <- function(x, ...){
  assign(x, ..., envir=.GlobalEnv)
  attr(get(x, envir=.GlobalEnv), "creation.time") <- Sys.time()
}

assign2("y", 1:4)
Error in attr(get(x), "creation.time") <- Sys.time() :
   could not find function "get<-"

Why doesn't it work?
If I remove the attr() part,
identical(y, get("y")) returns TRUE, so why attr() cannot work with it?

Thanks in advance for the clarification,

Ivan


Le 11/11/2010 11:16, Michael Bedward a écrit :
> Hi Tal,
>
> Here's a way of doing the first bit...
>
> assign2<- function(x, ...) {
>    xname<- deparse(substitute(x))
>    assign(xname, ...)
>    x<- get(xname)
>    attr(x, "creation.time")<- Sys.time()
>    assign(xname, x, pos=.GlobalEnv)
> }
>
> Michael
>
>
> On 11 November 2010 20:37, Tal Galili<tal.galili at gmail.com>  wrote:
>> My objective is to start having meta-data on objects that I create.
>> For example, consider the following function:
>>
>> assign2<- function(x, ...)
>> {
>>   assign("x", ...)
>> attr(x, "creation time")<- Sys.time()
>>   x<<- x
>> }
>>
>> assign2("x", 1:4)
>>
>> "assign2" assigns to x the vector 1:4, and it then also adds the creation
>> time of the object.
>>
>> (Hat tip goes to Peter Alspach for pointing me to the concept of adding meta
>> data to an object using attr)
>>
>>
>> But this function has several major limitations:
>> 1) It will not work for any assignment other then "x".  For example
>> assign2("y", 1:4)
>> Doesn't work.
>> How might this be fixed ?
>> 2) This function will probably need to also search the parent environment if
>> the variable already exists.  If it does, then there should be a "update
>> date" instead of "creation date".  But for that to work, I'll need a
>> solution for problem 1.
>> 3) How will this handle a case when we are updating only a subset of the
>> items?  (for example:  assign2("x[1:2]", 8:9) )
>> 4) My real intention is to somehow change the "<-" operator (not simply the
>> assign).  I am unsure as to how to do that.
>> 5) Are there any major pros/cons to the adding of such meta-data to objects?
>> (for example, excessive overhead on memory/performance)
>> 6) Is there already some system that knows how to do this in R (which I am
>> simply ignorant about)?
>>
>> Thanks for following through, and for any suggestions/thoughts you might
>> have.
>>
>> Best,
>> Tal
>>
>>
>>
>>
>>
>> ----------------Contact
>> Details:-------------------------------------------------------
>> Contact me: Tal.Galili at gmail.com |  972-52-7275845
>> Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) |
>> www.r-statistics.com (English)
>> ----------------------------------------------------------------------------------------------
>>
>>         [[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.
>>
> ______________________________________________
> 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.
>

-- 
Ivan CALANDRA
PhD Student
University of Hamburg
Biozentrum Grindel und Zoologisches Museum
Abt. Säugetiere
Martin-Luther-King-Platz 3
D-20146 Hamburg, GERMANY
+49(0)40 42838 6231
ivan.calandra at uni-hamburg.de

**********
http://www.for771.uni-bonn.de
http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php



More information about the R-help mailing list