R Language - Adding timestamp to console output

2.5k views Asked by At

I'm using sink() for logging purposes for running R-Scripts, which works fine.

*R> sink(file = paste(Log_Path, FileName), append = TRUE, type = c("output"), split = TRUE)*

I'm now doing performance tests and needing to find out how long certain parts of the R-Script runs, without adding tons of print statements.

This solution works, via in RGui Interface:

R> updatePrompt <- function(...) {options(prompt=paste(Sys.time(),"> ")); return(TRUE)}

R> addTaskCallback(updatePrompt)

However, The time prompts doesn't propagate back into the Console stream of sink() when running in the R-Server.

Suggestions?

I've explored txtStart , but not sure if that's what I need. Is there a different package or a option to set to set the timestamp in the prompt in the sink() console output?

Thanks for any help...

2

There are 2 answers

0
BrodieG On

The prompt is not part of stdout, which is why it doesn't make it to the sink. Why don't you just print from your callback? For example:

make_timing_fun <- function() {
  time.start <- proc.time()
  function(...) {
    new.time <- proc.time()
    print(new.time - time.start)
    time.start <<- new.time
    TRUE
  }
}
addTaskCallback(make_timing_fun()) # note parens used to generate actual function

Note this times the time between statements completing, so if you're just waiting around the console doing nothing that will be part of the time as well.

5
Hash tag Kitty On

I did try that originally, but I tried it again. and received same results:

Snippet of saved console output from log file:

> startdate <- as.vector(input_data2)

> input_data3 <- stop_date

> stopdate <- as.vector(input_data3)

.

Was hoping for this:

2014-01-03 09:07:57 > startdate <- as.vector(input_data2)

2014-01-03 09:07:57 > input_data3 <- stop_date

2014-01-03 09:07:57 > stopdate <- as.vector(input_data3)

.