R Markdown / knitr报道的蜡笔

时间:2017-11-20 13:10:10

标签: r knitr r-markdown

如何告诉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.

但文字颜色不是绿色。

修改

用例:https://github.com/wlandau-lilly/drake/issues/164

2 个答案:

答案 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."))
```