如何告诉R Markdown / knitr尊重crayon color codes?我有以下R Markdown报告。
---
title: "MWE"
author: "Will Landau"
date: "11/20/2017"
output: html_document
---
```{r color}
message(crayon::make_style("green")("My green message."))
```
当我编织并渲染它时,我看到了输出
## My green message.
但文字颜色不是绿色。
答案 0 :(得分:2)
这似乎有效:
---
title: "MWE"
output: html_document
---
```{r color, echo = FALSE}
options(crayon.enabled = TRUE)
knitr::knit_hooks$set(message = function(x, options){
paste0(
"<pre class=\"r-output\"><code>",
ansistrings::ansi_to_html(text = x, fullpage = FALSE),
"</code></pre>"
)
})
message(crayon::make_style("green")("My green message."))
```
Markdown输出:
---
title: "MWE"
output: html_document
---
<pre class="r-output"><code>
## <span style="color:#4e9a06">My green message.</span>
</code></pre>
有一点需要注意:ansistrings尚未发布。
答案 1 :(得分:2)
由于fansi现在在CRAN上,我将添加一个使用它的解决方案:
---
title: "fansi Rmd"
output: html_document
---
```{r color, echo = FALSE}
options(crayon.enabled = TRUE)
knitr::knit_hooks$set(message = function(x, options){
paste0(
"<pre class=\"r-output\"><code>",
fansi::sgr_to_html(x = x, warn = FALSE),
"</code></pre>"
)
})
message(crayon::make_style("green")("My green message."))
```