该pillar软件包提供了多种打印格式选项tibble。
https://pillar.r-lib.org/reference/pillar-package.html#package-options
例如,这是我在我的Windows机器上看到的,它支持以下选项:
但是,当我为文档设置相同的选项时rmarkdown,我没有看到打印输出有任何差异。
有没有办法成功地让它工作或者它本身不受支持rmarkdown?
在tibble 包的小插图中,有一个可能的解决方案。在 .Rmd 文件的设置块中,输入:
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
library(tibble)
set.seed(1014)
options(crayon.enabled = TRUE)
options(pillar.bold = TRUE, pillar.subtle_num = TRUE)
knitr::opts_chunk$set(collapse = TRUE, comment = pillar::style_subtle("#>"))
colourise_chunk <- function(type) {
function(x, options) {
lines <- x
if (type != "output") {
lines <- crayon::red(lines)
}
paste0(
'<div class="sourceCode"><pre class="sourceCode"><code class="sourceCode">',
paste0(
fansi::sgr_to_html(htmltools::htmlEscape(lines)),
collapse = "\n"
),
"</code></pre></div>"
)
}
}
knitr::knit_hooks$set(
output = colourise_chunk("output"),
message = colourise_chunk("message"),
warning = colourise_chunk("warning"),
error = colourise_chunk("error")
)
Run Code Online (Sandbox Code Playgroud)
在一个新块中:
broom::tidy(stats::chisq.test(table(ggplot2::msleep$vore)))
Run Code Online (Sandbox Code Playgroud)
我的 HTML 输出: