You can use addTaskCallback as follows to create a log of each top-level execution.
.log <- data.frame(time=character(0), expr=character(0)) .logger <- function(expr, value, ok, visible) {
Of course, instead of committing a cardinal sin with the growth of the data.frame line, as I am here, you can simply leave the connection open and write it directly to the file, closing the connection using on.exit .
And if you want to be careful in this matter, you can quite successfully package the logging settings in a function.
.log <- function() { .logger <<- local({ log <- data.frame(time=character(0), expr=character(0)) function(expr, value, ok, visible) { time <- as.character(Sys.time()) expr <- deparse(expr) log <<- rbind(log, data.frame(time, expr)) return(TRUE) } }) invisible(addTaskCallback(.logger)) } .save.log <- function() { if (exists('.logger')) write.csv(environment(.logger)$log, 'log.csv') } .log() x <- 1:10 y <- mean(x) .save.log()
source share