hook_benchmark sets a hook to benchmark chunks with the benchmark=TRUE option. The name of the trigger chunk option can be changed via the chunk_option parameter. The result is printed right after chunk outputs. See examples for the default printing format by format_benchmark.

  trigger = "benchmark",
  default = NULL,
  format = format_benchmark,
  .set = TRUE

format_benchmark(result, options)




A name of chunk option that triggers benchmark (default: "benchmark"). In order to trigger benchmark, specify TRUE as a value of this option.


A default value for the chunk option that triggers the hook (default: NULL).


A function to format a benchmark result (default: format_benchmark). It must accept two arguments, where the first is the benchmark result and the second is the list of current chunk options. NULL suppresses printing.


TRUE or FALSE to set the hook.


A result of benchmark


A list of current chunk options


An object of class environment of length 0.


invisible hook function


benchmarks records the results of benchmarks from chunks as a list named by chunk labels. If one requires complex formatting of benchmark results, then suppress automatic formatting by hook_benchmark(format = NULL). Then, retrieve benchmark results from benchmarks[["chunk-label"]]. Furthermore, format can happen conditionally by utilizing current chunk options via the second argument of the formatting function.


# Set a hook that triggers benchmarks if the `time` chunk option is not `NULL`. hook_benchmark("time") # Example of the default output format # Input is sec. Output is prettified. format_benchmark(1234, options = list(label = "example-chunk"))
#> [1] "example-chunk: 20m 34s"