简体   繁体   English

R Markdown / knitr报道的蜡笔

[英]crayon in R Markdown / knitr reports

How do I tell R Markdown / knitr to respect crayon color codes ? 如何告诉R Markdown / knitr尊重蜡笔颜色代码 I have the following R Markdown report. 我有以下R Markdown报告。

---
title: "MWE"
author: "Will Landau"
date: "11/20/2017"
output: html_document
---

```{r color}
message(crayon::make_style("green")("My green message."))
```

When I knit and render it, I see the output 当我编织并渲染它时,我看到了输出

## My green message.

but the text color is not green. 但文字颜色不是绿色。

EDIT 编辑

Use case: https://github.com/wlandau-lilly/drake/issues/164 使用案例: https//github.com/wlandau-lilly/drake/issues/164

Since fansi is now on CRAN, I will add a solution that uses it: 由于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."))
```

This seems to work: 这似乎有效:

---
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 output: 降价输出:

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

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

One caveat: ansistrings is not released yet. 一个警告: ansistrings尚未发布。

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

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