rstudio / rmarkdown-cookbook

R Markdown Cookbook. A range of tips and tricks to make better use of R Markdown.
https://bookdown.org/yihui/rmarkdown-cookbook/
584 stars 225 forks source link

Unclear units in chunk timing (section 13.3) #389

Closed plger closed 1 year ago

plger commented 1 year ago

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')
cderv commented 1 year ago

Thanks for the suggestion !