set_knit_hooks | R Documentation |
This is a convenience function designed for use within an rmarkdown
document. It overrides the knitr
output hooks by using
knitr::knit_hooks$set
. It replaces the hooks with ones that convert
Control Sequences into HTML. In addition to replacing the hook functions,
this will output a <STYLE> HTML block to stdout. These two actions are
side effects as a result of which R chunks in the rmarkdown
document that
contain CSI SGR are shown in their HTML equivalent form.
set_knit_hooks(
hooks,
which = "output",
proc.fun = function(x, class) html_code_block(to_html(html_esc(x)), class = class),
class = sprintf("fansi fansi-%s", which),
style = getOption("fansi.css", dflt_css()),
split.nl = FALSE,
.test = FALSE
)
hooks |
list, this should the be |
which |
character vector with the names of the hooks that should be replaced, defaults to 'output', but can also contain values 'message', 'warning', and 'error'. |
proc.fun |
function that will be applied to output that contains
CSI SGR sequences. Should accept parameters |
class |
character the CSS class to give the output chunks. Each type of
output chunk specified in |
style |
character a vector of CSS styles; these will be output inside
HTML >STYLE< tags as a side effect. The default value is designed to
ensure that there is no visible gap in background color with lines with
height 1.5 (as is the default setting in |
split.nl |
TRUE or FALSE (default), set to TRUE to split input strings
by any newlines they may contain to avoid any newlines inside SPAN tags
created by |
.test |
TRUE or FALSE, for internal testing use only. |
The replacement hook function tests for the presence of CSI SGR
sequences in chunk output with has_ctl
, and if it is detected then
processes it with the user provided proc.fun
. Chunks that do not contain
CSI SGR are passed off to the previously set hook function. The default
proc.fun
will run the output through html_esc
, to_html
, and
finally html_code_block
.
If you require more control than this function provides you can set the
knitr
hooks manually with knitr::knit_hooks$set
. If you are seeing your
output gaining extra line breaks, look at the split.nl
option.
named list with the prior output hooks for each of which
.
Since we do not formally import the knitr
functions we do not
guarantee that this function will always work properly with knitr
/
rmarkdown
.
has_ctl
, to_html
, html_esc
, html_code_block
,
knitr
output hooks,
embedding CSS in Rmd,
and the vignette vignette(package='fansi', 'sgr-in-rmd')
.
## Not run:
## The following should be done within an `rmarkdown` document chunk with
## chunk option `results` set to 'asis' and the chunk option `comment` set
## to ''.
```{r comment="", results='asis', echo=FALSE}
## Change the "output" hook to handle ANSI CSI SGR
old.hooks <- set_knit_hooks(knitr::knit_hooks)
## Do the same with the warning, error, and message, and add styles for
## them (alternatively we could have done output as part of this call too)
styles <- c(
getOption('fansi.style', dflt_css()), # default style
"PRE.fansi CODE {background-color: transparent;}",
"PRE.fansi-error {background-color: #DD5555;}",
"PRE.fansi-warning {background-color: #DDDD55;}",
"PRE.fansi-message {background-color: #EEEEEE;}"
)
old.hooks <- c(
old.hooks,
fansi::set_knit_hooks(
knitr::knit_hooks,
which=c('warning', 'error', 'message'),
style=styles
) )
```
## You may restore old hooks with the following chunk
## Restore Hooks
```{r}
do.call(knitr::knit_hooks$set, old.hooks)
```
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.