-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path_chunk-timing.qmd
More file actions
34 lines (28 loc) · 945 Bytes
/
_chunk-timing.qmd
File metadata and controls
34 lines (28 loc) · 945 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
```{r}
#| label: setup-timing
#| include: false
# Set knitr options globally for all chunks
knitr::opts_chunk$set(time_it = TRUE)
# Get chapter-specific log file name
current_file <- knitr::current_input()
log_file <- sprintf("chunk-times-%s.txt", tools::file_path_sans_ext(basename(current_file)))
# Clear previous log
if (file.exists(log_file)) file.remove(log_file)
# Create timing hook
knitr::knit_hooks$set(time_it = local({
now <- NULL
function(before, options) {
label <- if (!is.null(options$label)) options$label else "<unnamed>"
if (before) {
now <<- Sys.time()
} else {
elapsed <- difftime(Sys.time(), now, units = "secs")
msg <- sprintf("This chunk (`%s`) ran in %.2f seconds.", label, as.numeric(elapsed))
# Print to GitHub Actions log
message(msg)
# Save to chapter-specific log file
cat(msg, file = log_file, append = TRUE, sep = "\n")
}
}
}))
```