Tracking chunk execution times in Quarto book #13040
-
DescriptionI'm trying to track chunk execution times (via the terminal/log in GitHub Actions and via a log file) for a Quarto book. Using my setup chunk (see below), I am able to get it to work for the Here is my "setup" chunk of the
There is already a feature request for tracking chunk execution times in Quarto: #5179. I also had to follow some advice in the thread on getting messages to appear in the log (via |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
Did you try the R Markdown Cookbook code on the matter? |
Beta Was this translation helpful? Give feedback.
-
Here is the example in Quarto from the Cookbook ---
format: html
---
```{r}
knitr::knit_hooks$set(time_it = local({
now <- NULL
function(before, options) {
if (before) {
# record the current time before each chunk
now <<- Sys.time()
} else {
# calculate the time difference after a chunk
res <- difftime(Sys.time(), now, units = "secs")
# return a character string to show the time
paste("Time for this code chunk to run:", round(res,
2), "seconds")
}
}
}))
```
```{r}
#| time_it: true
Sys.sleep(2)
``` So it is working, and I think it is working for you too
All chapters in a Quarto Book are rendered invidually, which means that a new R session and new knitr context will be used for each. The setup chunk you have needs to be inserted into each chapter, for example using an include {{< _setup-knitr-timing.qmd >}} Each R session will be used for each document is independant. So to share content you need to use what is available through Quarto (include shortcode for example), or through knitr (sourcing a R file in in setup chunk that you have in all your document). There are several discussions opened about sharing / loading content for each chapter. You can look at discussion there.
You are printing value here, not outputing a message. So ```{r}
knitr::knit_hooks$set(time_it = local({
now <- NULL
function(before, options) {
if (before) {
# record the current time before each chunk
now <<- Sys.time()
} else {
# calculate the time difference after a chunk
res <- difftime(Sys.time(), now, units = "secs")
# return a character string to show the time
message("Time for this code chunk to run:", round(res,
2), "seconds")
}
}
}))
```
```{r}
#| time_it: true
#| message: NA
Sys.sleep(2)
``` and now it is in the terminal log
Hope it helps. |
Beta Was this translation helpful? Give feedback.
-
FYI, If the goal is to display the running time in the log/console, another way is to use the progress bar option from |
Beta Was this translation helpful? Give feedback.
Here is the example in Quarto from the Cookbook
So it is working, and I think it is working for you too