Я использую встроенный код в RMarkdown, и я хотел бы, чтобы весь текст, являющийся результатом встроенного кода, имел другой цвет в документе. В этот пример я хочу, чтобы heat.colors был красным по всему документу. Есть ли способ сделать это?





Вы можете сделать что-то вроде:
---
title: ''
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{css echo=FALSE}
.custom-inline {
color: red;
font-weight: 700
}
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
This is inline code: `r sprintf("<span class='custom-inline'>%s</span>", colnames(mtcars)[1])`.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r cars}
summary(cars)
```
## Including Plots
You can also embed plots, for example:
### This is more inline code `r sprintf("<span class='custom-inline'>%s</span>", colnames(mtcars)[2])`.
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
получить:
Шаблоны по умолчанию не заключают встроенные фрагменты в классифицированный тег <span>, поэтому вам придется делать это вручную. Вы также можете создать функцию для этого.
Или вы можете использовать text_spec в kableExtra. Он буквально делает то же самое, но чуть более буквально. См. Еще здесь
---
title: ''
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(kableExtra)
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
This is inline code: `r text_spec(colnames(mtcars)[1], color = "red")`.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r cars}
summary(cars)
```
## Including Plots
You can also embed plots, for example:
### This is more inline code `r text_spec(colnames(mtcars)[2], color = "red")`.
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
Это работает, но я использую много встроенных фрагментов кода. Не могли бы вы помочь мне понять, как будет выглядеть функция, заключающая их в теги span?