Title: | Chunk Hooks for 'R Markdown' |
---|---|
Description: | Set chunk hooks for 'R Markdown' documents <https://rmarkdown.rstudio.com/>, and improve user experience. For example, change units of figure sizes, benchmark chunks, and number lines on code blocks. |
Authors: | Atsushi Yasumoto [aut, cph, cre] |
Maintainer: | Atsushi Yasumoto <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.0.1 |
Built: | 2024-11-10 05:45:46 UTC |
Source: | https://github.com/atusy/chunkhooks |
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
.
hook_benchmark( trigger = "benchmark", default = NULL, format = format_benchmark, .set = TRUE ) format_benchmark(result, options) benchmarks
hook_benchmark( trigger = "benchmark", default = NULL, format = format_benchmark, .set = TRUE ) format_benchmark(result, options) benchmarks
trigger |
A name of chunk option that triggers benchmark (default:
|
default |
A default value for the chunk option that |
format |
A function to format a benchmark result (default:
|
.set |
|
result |
A result of benchmark |
options |
A list of current chunk options |
An object of class environment
of length 0.
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 format
ting function.
invisible hook 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"))
# 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"))
Comment out codes based on their languages.
comment_out(x, engine, extra_syntax = character(0L))
comment_out(x, engine, extra_syntax = character(0L))
x |
A character vector |
engine |
A string that defines the engine of the |
extra_syntax |
A named character vector which defines extra syntax to comment out.
Each values should contain |
# Default behaviors comment_out("R language", "R") comment_out("Python language", "python") # A customized behavior comment_out( "Python language", "python", extra_syntax = c("python" = '"""%s"""') )
# Default behaviors comment_out("R language", "R") comment_out("Python language", "python") # A customized behavior comment_out( "Python language", "python", extra_syntax = c("python" = '"""%s"""') )
A named vector which defines comment syntax. This is internally used by
hook_title_comment()
, comment_title()
, and comment_out()
.
comment_syntax
comment_syntax
An object of class character
of length 38.
Comment title to chunk code
comment_title( options = list(engine = "R"), template = "{engine}", extra_syntax = character(0L), ... )
comment_title( options = list(engine = "R"), template = "{engine}", extra_syntax = character(0L), ... )
options |
A list of chunk options. |
template |
A template of title processed by |
extra_syntax |
A named character vector which defines extra syntax to comment out.
Each values should contain |
... |
Arguments passed to |
comment_title( options = list(engine = "R"), template = "{engine}" ) comment_title( options = list(engine = "css", label = "mystyle"), template = "{label}.{engine}" )
comment_title( options = list(engine = "R"), template = "{engine}" ) comment_title( options = list(engine = "css", label = "mystyle"), template = "{label}.{engine}" )
By default, figure size of R Markdown is specified with inches. This function changes the default unit.
hook_figure_unit(unit = "mm", .set = TRUE)
hook_figure_unit(unit = "mm", .set = TRUE)
unit |
A string of an unit (default: "mm"). Available units follow.
|
.set |
|
As a side effect, fig.retina
is set to NULL
because of
https://github.com/yihui/knitr/issues/1876.
invisible hook function
hook_figure_unit('mm')
hook_figure_unit('mm')
Number lines on code blocks created by chunks, i.e. source, output, message, warning, and/or error.
hook_numberLines(targets = "source", .set = TRUE)
hook_numberLines(targets = "source", .set = TRUE)
targets |
A character vector specifying what kind of code blocks
automatically number lines (default: |
.set |
|
invisible hook function
hook_numberLines("source")
hook_numberLines("source")
Prepend title comments to each chunks. By default, title is the name of the corresponding chunk engines.
hook_title_comment( trigger = "title", default = "{engine}", args = list(), .set = TRUE )
hook_title_comment( trigger = "title", default = "{engine}", args = list(), .set = TRUE )
trigger |
A name of chunk option triggers the hook. |
default |
A string or function as a default value of the |
args |
A list of arguments given to |
.set |
|
comment_title()
, comment_out()
, comment_syntax()
# Set hook which comments engine names of each chunks at the beginning. hook_title_comment() # Customize format # For R chunks, let title comment be something like "chunk-label.R". # Otherwise, fall back to the default behavior. hook_title_comment( default = function(options) { if (options$engine == "R") { return(sprintf("# %s.%s", options$label, options$engine)) } return(comment_title(options, template = "{engine}")) } )
# Set hook which comments engine names of each chunks at the beginning. hook_title_comment() # Customize format # For R chunks, let title comment be something like "chunk-label.R". # Otherwise, fall back to the default behavior. hook_title_comment( default = function(options) { if (options$engine == "R") { return(sprintf("# %s.%s", options$label, options$engine)) } return(comment_title(options, template = "{engine}")) } )