繁体   English   中英

R Markdown / knitr报道的蜡笔

[英]crayon in R Markdown / knitr reports

如何告诉R Markdown / knitr尊重蜡笔颜色代码 我有以下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

由于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."))
```

这似乎有效:

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

降价输出:

---
title: "MWE"
output: html_document
---

<pre class="r-output"><code>
## <span style="color:#4e9a06">My green message.</span>
</code></pre>

一个警告: ansistrings尚未发布。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM