[R] Dating Objects
Rolf Turner
rolf at math.unb.ca
Fri Aug 12 17:56:14 CEST 2005
Paul Roebuck wrote:
On Fri, 12 Aug 2005, Jason Skelton wrote:
> > I'm trying to carryout the equivalent of ls -l in R
> > to give some date/time label to each of my objects
> >
> > If the case is that there is no equivalent, is it
> > possible to list all objects in an environment that
> > share a common component so that the common component
> > is also displayed?
> >
> > I'm attempting to do the following
> >
> > object$time <- Sys.time()
> >
> > for every object i've created which although tedious
> > appears to work
> >
> > However I've no idea how if it is possible to list all
> > the objects by $time or extract the object name & $time
> > from all objects simultaneously so I can compare them.
>
> Not really following why you want to do this so it's
> hard to suggest something. Were you attempting to track
> mtime or ctime?
>
> Perhaps instead you could describe the larger problem
> you're trying to solve so there's more context to work
> with.
>
> > Or am I just wasting my time?
>
> Most of us probably are...
I don't follow what you aren't following .... It seems to me
to be an eminently reasonable thing to want to do. Answers
questions such as ``Was x modified since y was modified?''
Presumably modification date is what's wanted; this is the
crucial concept. One could tack on a creation date as well ...
I think I would make the modification data an ***attribute***
of the object, rather than sticking it in as a component.
(The latter might mess up some vital aspect of the nature
of the object.)
The problem is to remember to date stamp each object each
time it is modified ....
To do the analogue of ``ls -l'' one could create a function
say ``lsl()'' along the following lines:
lsl <- function() {
xxx <- ls(envir=.GlobalEnv)
yyy <- unlist(lapply(xxx,function(x){
a <- attr(get(x),'datestamp')
if(is.null(a)) NA else a
}
))
ooo <- order(yyy,na.last=FALSE)
yyy[is.na(yyy)] <- "undated"
rrr <- rep("",length(xxx))
prmatrix(cbind(xxx[ooo],yyy[ooo]),rowlab=rrr,quote=FALSE)
invisible()
}
The date stamping could be done by a function such as
stomp <- function(x){
attr(x,'datestamp') <- paste(Sys.time())
x
}
This would work except there seems to be a bug in order()
when na.last is set to FALSE.
I tried
> lsl <- stomp(lsl)
> stomp <- stomp(stomp)
> a <- stomp(42)
> b <- stomp(runif(10))
> lsl()
and got
[,1] [,2]
lsl 2005-08-12 12:38:12
stomp 2005-08-12 12:38:27
a 2005-08-12 12:38:35
b 2005-08-12 12:38:46
Then I did
> x <- 1:10 # No stomping.
> lsl()
and got
[,1] [,2]
lsl 2005-08-12 12:38:12
stomp 2005-08-12 12:38:27
x undated
a 2005-08-12 12:38:35
b 2005-08-12 12:38:46
so the undated x got put in the middle, rather than at the beginning
as it should've been. Weird. I'm going to send a separate email
about this.
cheers,
Rolf Turner
rolf at math.unb.ca
More information about the R-help
mailing list