rstudio/rmarkdown-cookbook

Unclear units in chunk timing (section 13.3)

plger opened this issue · 1 comments

plger commented

Thanks for the great resource.

Section 13.3 Report how much time each chunk takes to run provides a snippet to automatically print the executing time of code chunks. The current snippet only prints the number (with tons of decimals), without any unit (it's stripped out in the conversion to character of the paste), and the unit will be different between chunks (e.g. minutes when it's longer, seconds otherwise), leading to inconsistencies and confusion.

To avoid this, one options is to specify the unit when calculating the difference:

res <- difftime(Sys.time(), now, units="secs")

and then report it as such:

paste('Time for this code chunk to run:', round(res,2), 'seconds')