Tal Galili
2010-Nov-11 09:37 UTC
[R] Adding meta-data when creating objects. e.g: changing "<-" so to (for example) add "creation time" - how-to and pros/cons?
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@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]]
Michael Bedward
2010-Nov-11 10:16 UTC
[R] Adding meta-data when creating objects. e.g: changing "<-" so to (for example) add "creation time" - how-to and pros/cons?
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.
>
Barry Rowlingson
2010-Nov-11 10:30 UTC
[R] Adding meta-data when creating objects. e.g: changing "<-" so to (for example) add "creation time" - how-to and pros/cons?
On Thu, Nov 11, 2010 at 9:37 AM, Tal Galili <tal.galili at gmail.com> wrote:> 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)I had a go at doing (4) a few years back. The major problem I had was that if you do: y <- 1:10 x <- y with a <- operator that sets a timestamp then: identical(x,y) is FALSE. I implemented timestamping by adding an attribute to objects during assigment by modifying the C source, and then lots and lots of R's tests failed during build because identical things were no longer identical. Might be better to store your metadata in a separate object, .metadata in the global env perhaps? Then just do: .metadata[[name_of_thing]] = list(modified=Sys.time()) in your modified assign. Performance will only be a problem if your program is doing nothing else except fiddle with metadata, I reckon. Your program does do something useful, doesn't it? Barry
Seemingly Similar Threads
- Is there a way to edit a specific line in a function (e.g: doing function->text->edit->function) ?
- Adding a "description" meta-tag to the R homepage
- Importing tRNA data into R ?
- How to: highlight R syntax on webpages ?
- Capturing R console output into a file (sink+savehistory ??)