Can I list objects sorted by date?

Something you might try is automating the logging of modifications to variables of interest using the makeActiveBinding function. You could then use the log to sort the output of ls() by modification time.

One caveat is that, using this method, tracking has to be set up before the variable is first used.

.change.log <- list()
track <- function(variable) {
    makeActiveBinding(variable,
        function(v) 
            if (! missing(v)) 
            .change.log[[variable]] <<- c(.change.log[[variable]], 
                                           as.list(Sys.time())),
        .GlobalEnv)
}

track('x')
x <- 1
.change.log
x <- 2
.change.log

Each time x is modified, the anonymous function supplied to makeActiveBinding gets called with v equal to the new value. This function also gets called when x is referenced, but with nothing supplied to v in this case, hence the conditional with missing(v)---we only want to update the log when the value changes.


EDIT

After further consideration, a better alternative to makeActiveBinding would be to install a modification logger via the function addTaskCallback. The code below creates an automated logger that records timestamps by variable name every time the <- operator is used at the top level.

# define a log maker function. This returns a new logger function
# bound with a fresh log.
mk.log <- function() {
    log <- list()
    # handler functions have to have these four args, but we only use the first.
    function(expr, value, ok, visible) {
        if (class(expr) == '<-') {
            # convert the assignment call to a list, so the 
            # variable name can be extracted
            call.list <- as.list(expr)
            # extract the name of the variable being affected, and 
            # convert it to character, so we can use it as a list name
            variable <- as.character(call.list[[2]])
            # append a timestamp to the log for this variable
            log[[variable]] <<- c(log[[variable]], list(Sys.time()))
        }
        # callback handlers need to return a bool
        return(TRUE)
    }
}

# assign the handler to .log, and install it.
addTaskCallback(.log <- mk.log(), name='log')

x <- 5
x <- 10
y <- 4

# read the log
environment(.log)$log

# $x
# $x[[1]]
# [1] "2013-01-25 10:24:26.581 EST"
# 
# $x[[2]]
# [1] "2013-01-25 10:24:26.585 EST"
# 
# 
# $y
# $y[[1]]
# [1] "2013-01-25 10:24:26.589 EST"

Well, with a little creative hacking, you could write your own methods for your variables. E.g.:

datedmatrix<-function(data,nrow,ncol,...) {
    output <- matrix(data, nrow=nrow,ncol=ncol,...)
    attr(output,'create') <- date()
    return(output)
}

No, but you should use the historyfunction to find its name through the latest commands you have run.

By default, history will show the last 25 lines of code, but you can request more by doing:

history(max.show = 100)

There is also a hard limit on the number of lines you can show. It is equal to the value of the environment variable R_HISTSIZE whose default is 512. But as the documentation says:

There is no limit on the number of lines of history retained during a session [...]

so you can do:

Sys.setenv("R_HISTSIZE" = 10000)
history(max.show = 10000)

and you should be able to see all your history since you started your session (assuming you ran less than 10000 lines of code.)

Tags:

R