knit_ex
is a utility function for running small knitr examples,
e.g., to illustrate functionalities or issues.
hook_backspace
is a chunk hook that enables the use of backspace
characters in the output (e.g., as used in progress bars), and still
obtain a final output as in the console.
knit_ex(x, ..., quiet = TRUE, open = FALSE) hook_try(before, options, envir) hook_backspace() hook_toggle()
x | text to knit as a character vector |
---|---|
... | |
quiet | logical that indicates if knitting should be quiet (no progress bars etc..). |
open | logical, only used when |
before | logical that indicates when the hook is being called: before or after the chunk is processed. |
options | list of current knitr chunk options |
envir | environment where the chunk is evaluated |
knit_ex
returns the generated code, although invisibly when open=TRUE
.
hook_try
: is a knitr hook to enable showing error
messages thrown by try
.
The function is not meant to be called directly, but only registered
using knitr::knit_hooks (see details on this dedicated man page).
This simply defines a function try
in envir
that prints
the error message if any, and is called instead of base try
.
hook_toggle
: is a chunk hook that adds clickable elements to toggle indvidual
code chunks in HTML documents generated from .Rmd files.
#> <pre><code class="r">1 + 1 #> </code></pre> #> #> <pre><code>## [1] 2 #> </code></pre>#> <pre><code class="r">stop('ah ah') #> </code></pre> #> #> <pre><code>## Error in eval(expr, envir, enclos): ah ah #> </code></pre># with try the error is output on stderr but not caughted by knitr knit_ex("try( stop('ah ah') )")#> <pre><code class="r">try( stop('ah ah') ) #> </code></pre> #> #> <pre><code>## Error in try(stop("ah ah")) : ah ah #> </code></pre># no message caught knit_ex(" ^^^{r, include = FALSE} knit_hooks$set(try = pkgmaker::hook_try) ^^^ ^^^{r, try=TRUE} try( stop('ah ah') ) ^^^")#> <pre><code class="r">try( stop('ah ah') ) #> </code></pre> #> #> <pre><code>## Error: ah ah #> </code></pre># Correctly formatting backspaces in chunk outputs tmp <- tempfile(fileext = '.Rmd') cat(file = tmp, " ^^^{r, include = FALSE} library(knitr) knit_hooks$set(backspace = pkgmaker::hook_backspace()) ^^^ Default knitr does not handle backspace and adds a special character: ^^^{r} cat('abc\bd') ^^^ Using the hook backspace solves the issue: ^^^{r, backspace=TRUE} cat('abc\bd') ^^^ ") # knit out <- knitr::knit2html(tmp, fragment.only = TRUE)#> #> #>#> | | | 0% | |......................................................................| 100% #> ordinary text without R code #> #>#># look at output if (FALSE) { browseURL(out) edit( file = out) } # cleanup out_files <- list.files(dirname(out), full.names = TRUE, pattern = paste0("^", tools::file_path_sans_ext(out))) unlink(c(tmp, out_files)) knit_ex(" Declare chunk hook: ^^^{r, setup} library(knitr) knit_hooks$set(toggle = hook_toggle()) ^^^ The R code of this chunk can be toggled on/off, and starts visible: ^^^{r, toggle=TRUE} print(1:10) ^^^ The R code of this chunk can be toggled on/off, and starts hidden: ^^^{r, toggle=FALSE} print(1:10) ^^^ This is a plain chunk that cannot be toggled on/off: ^^^{r} print(1:10) ^^^ Now all chunks can be toggled and start visible: ^^^{r, toggle_all} opts_chunk$set(toggle = TRUE) ^^^ ^^^{r} sample(5) ^^^ To diable the toggle link, one can pass anything except TRUE/FALSE: ^^^{r, toggle = NA} sample(5) ^^^ ", open = TRUE)