如何用不同的颜色为不同的 Rmarkdown kable 表着色
how to colour different Rmarkdown kable tables in different colour
下面是我正在使用的代码。唯一的问题是我的两个表的颜色相同,这是我的第二个颜色代码 (#ffff99)。如何使两个表的背景保持不同。
<style>
table {
background-color:#eff8e5;
}
</style>
``` {r}
kable(df1)
```
<style>
table {
background-color:#ffff99;
}
</style>
``` {r}
kable(df2)
```
HTML/CSS 没有按照您隐含假设的方式呈现。如果渲染遵循如下过程,您的代码将按预期工作:
- set: "tables are green"
- output green table
- set: "tables are yellow"
- output yellow table
但事实并非如此。实际发生的是:
- set: "all tables are green"
- output table 1
- (table 1 is green)
- set: "all tables are yellow"
- output table 2
- (table 1 and table 2 are yellow)
作为解决方案,您可以使用两种不同的 classes:
<style>
.lightgreen {
background-color:#eff8e5;
}
.yellow {
background-color:#ffff99;
}
</style>
(最好,class 名称应该描述 原因 为什么使用 class 而不是 class 目前看起来像,但没有更多信息 "lightgreen" 和 "yellow" 是我想出的最好的名字)。
现在您需要告诉 kable
table 应该分配这些 class。
选项 1:
kable(df1, format = "html", table.attr = "class=\"lightgreen\"")
kable(df2, format = "html", table.attr = "class=\"yellow\"")
但是,这会去除大部分(可能需要的)默认 table 布局。
选项 2: 在 table 周围添加一个容器,并在该容器中添加 table 样式。
<style>
.lightgreen table {
background-color:#eff8e5;
}
.yellow table {
background-color:#ffff99;
}
</style>
<div class = "lightgreen">
```{r}
library(knitr)
df1 <- data.frame(a=1:10, b=2:11)
kable(df1)
```
</div>
<div class = "yellow">
```{r}
df2 <- data.frame(a=1:10, b=2:11)
kable(df2)
```
</div>
下面是我正在使用的代码。唯一的问题是我的两个表的颜色相同,这是我的第二个颜色代码 (#ffff99)。如何使两个表的背景保持不同。
<style>
table {
background-color:#eff8e5;
}
</style>
``` {r}
kable(df1)
```
<style>
table {
background-color:#ffff99;
}
</style>
``` {r}
kable(df2)
```
HTML/CSS 没有按照您隐含假设的方式呈现。如果渲染遵循如下过程,您的代码将按预期工作:
- set: "tables are green"
- output green table
- set: "tables are yellow"
- output yellow table
但事实并非如此。实际发生的是:
- set: "all tables are green"
- output table 1
- (table 1 is green)
- set: "all tables are yellow"
- output table 2
- (table 1 and table 2 are yellow)
作为解决方案,您可以使用两种不同的 classes:
<style>
.lightgreen {
background-color:#eff8e5;
}
.yellow {
background-color:#ffff99;
}
</style>
(最好,class 名称应该描述 原因 为什么使用 class 而不是 class 目前看起来像,但没有更多信息 "lightgreen" 和 "yellow" 是我想出的最好的名字)。
现在您需要告诉 kable
table 应该分配这些 class。
选项 1:
kable(df1, format = "html", table.attr = "class=\"lightgreen\"")
kable(df2, format = "html", table.attr = "class=\"yellow\"")
但是,这会去除大部分(可能需要的)默认 table 布局。
选项 2: 在 table 周围添加一个容器,并在该容器中添加 table 样式。
<style>
.lightgreen table {
background-color:#eff8e5;
}
.yellow table {
background-color:#ffff99;
}
</style>
<div class = "lightgreen">
```{r}
library(knitr)
df1 <- data.frame(a=1:10, b=2:11)
kable(df1)
```
</div>
<div class = "yellow">
```{r}
df2 <- data.frame(a=1:10, b=2:11)
kable(df2)
```
</div>