Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Language - Adding timestamp to console output

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...

like image 747
Hash tag Kitty Avatar asked Mar 01 '26 06:03

Hash tag Kitty


1 Answers

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.

like image 193
BrodieG Avatar answered Mar 03 '26 20:03

BrodieG



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!