如何用不同的颜色给不同的Rmarkdown电缆表上色 [英] how to colour different Rmarkdown kable tables in different colour

查看:135
本文介绍了如何用不同的颜色给不同的Rmarkdown电缆表上色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我正在使用的代码.唯一的问题是我的两个表都具有相同的颜色,这是我的第二个颜色代码(#ffff99).如何使两个表的背景保持不同.

Below is the code I am using. Only problem is that both my tables are of same colour which is my second colour code (#ffff99). How can I keep the background of both tables different.

<style>
table {
background-color:#eff8e5;
}
</style>
``` {r}
kable(df1)
```

<style>
table {
background-color:#ffff99;
}
</style>
``` {r}
kable(df2)
```

推荐答案

HTML/CSS的呈现方式与您隐式假定的方式不同.如果渲染遵循以下过程,则您的代码将按预期工作:

HTML/CSS is not rendered the way you implicitly assume. Your code would work as expected if the rendering followed a procedure like:

  • 设置:桌子是绿色的"
  • 输出绿表
  • 设置:桌子是黄色的"
  • 输出黄色表格
  • set: "tables are green"
  • output green table
  • set: "tables are yellow"
  • output yellow table

但事实并非如此.实际发生的是:

But this is not the case. What actually happens is:

  • 设置:所有桌子都是绿色的"
  • 输出表1
  • (表1为绿色)
  • 设置:所有表格均为黄色"
  • 输出表2
  • (表1 表2为黄色)
  • 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)

作为解决方案,您可以使用两个不同的类:

As a solution, you could use two different classes:

<style>
.lightgreen {
  background-color:#eff8e5;
}

.yellow {
  background-color:#ffff99;
}
</style>

(最佳情况下,类名应描述原因,为什么使用该类,而不是该类当前的样子,但最好不要带任何其他信息,浅绿色"和黄色"是最好的我想出的名字).

(Optimally, class names should describe the reason why the class is used and not how the class currently looks like, but without further information "lightgreen" and "yellow" are the best names I came up with).

现在您需要告诉kable这些表应该分配这些类.

Now you need to tell kable that the tables should get these classes assigned.

选项1:

kable(df1, format = "html", table.attr = "class=\"lightgreen\"")
kable(df2, format = "html", table.attr = "class=\"yellow\"")

但是,这会删除大多数(可能是所需的)默认表布局.

However, this strips most of the (probably desired) default table layout.

选项2:在表周围添加一个容器,并在该容器中放置样式表.

Option 2: Add a container around the tables and style tables in that container.

<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>

这篇关于如何用不同的颜色给不同的Rmarkdown电缆表上色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆