Skip to content

Commit 820ad5e

Browse files
authored
Merge pull request #227 from thomasp85/issue222-knitr-hook
Add knitr hook
2 parents f29cdea + 32eacef commit 820ad5e

File tree

6 files changed

+80
-0
lines changed

6 files changed

+80
-0
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export(layout_logging)
5353
export(layout_simple)
5454
export(layout_syslognet)
5555
export(log_appender)
56+
export(log_chunk_time)
5657
export(log_debug)
5758
export(log_elapsed)
5859
export(log_elapsed_start)

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* `log_errors()` gains a `traceback` argument that toggles whether the error traceback should be logged along with the message (fix #86 via #223, @thomasp85)
66
* File and line location of the log call is now available to the layouts (fix #110 via #224, @thomasp85)
77
* New `formatter_cli()` allows you to use the syntax from the cli package to create log messages (fix #210 via #225, @thomasp85)
8+
* New `log_chunk_time()` helper function to automatically log the execution time of knitr code chunks (fix #222 via ##227, @thomasp85)
89

910
# logger 0.4.0 (2024-10-19)
1011

R/helpers.R

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,3 +300,40 @@ log_failure <- function(expression) {
300300
}
301301
)
302302
}
303+
304+
#' Automatically log execution time of knitr chunks
305+
#'
306+
#' Calling this function in the first chunk of a document will instruct knitr
307+
#' to automatically log the execution time of each chunk. If using
308+
#' [formatter_glue()] or [formatter_cli()] then the `options` variable will be
309+
#' available, providing the chunk options such as chunk label etc.
310+
#'
311+
#' @inheritParams log_elapsed
312+
#'
313+
#' @export
314+
#'
315+
#' @examples
316+
#' # To be put in the first chunk of a document
317+
#' log_chunk_time("chunk {options$label}")
318+
#'
319+
log_chunk_time <- function(..., level = INFO, namespace = NA_character_) {
320+
if (!requireNamespace("knitr", quietly = TRUE)) {
321+
stop("knitr is required to use this functionality", call. = FALSE)
322+
}
323+
if (!isTRUE(getOption("knitr.in.progress"))) {
324+
return(invisible())
325+
}
326+
args <- list(...)
327+
args$level <- level
328+
args$namespace <- namespace
329+
knitr::knit_hooks$set(logger_timer = function(before, options) {
330+
if (before) {
331+
log_elapsed_start(namespace = namespace, quiet = TRUE)
332+
} else {
333+
do.call(log_elapsed, args)
334+
}
335+
})
336+
knitr::opts_chunk$set(logger_timer = TRUE)
337+
338+
invisible()
339+
}

_pkgdown.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ reference:
2727
- log_separator
2828
- log_with_separator
2929
- with_log_threshold
30+
- log_chunk_time
3031

3132
- title: Appenders
3233
desc: >

man/log_chunk_time.Rd

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-helpers.R

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,17 @@ test_that("lower log level", {
9696
local_test_logger(TRACE, layout = layout_glue_generator("{level} {msg}"))
9797
expect_output(log_eval(4), sprintf("TRACE %s => %s", shQuote(4), shQuote(4)))
9898
})
99+
100+
test_that("knitr hook gets applied", {
101+
local_test_logger()
102+
mock_doc <- c(
103+
"```{r, include = FALSE}",
104+
"library(logger)",
105+
"log_chunk_time()",
106+
"```",
107+
"```{r}",
108+
"Sys.sleep(0.2)",
109+
"```"
110+
)
111+
expect_output(knitr::knit(text = mock_doc, quiet = TRUE), "global timer 0.2")
112+
})

0 commit comments

Comments
 (0)