将标签属性包含到 kable header
Include label attribute into kable header
可重现的例子:
我有一个数据框,其中标记了变量以使用自 v0.4.2 以来的 sjmisc package, which works nicely together with dplyr 来表示单位。
library(dplyr)
library(sjmisc)
library(ggplot2)
data("diamonds")
df= tbl_df(diamonds) %>%
select(cut, carat, color, price) %>%
set_label(c("", "Kt", "Type","EUR")) %>%
slice(1:5)
正如 str(df)
所示,它正确地包含了三列标签:
Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 5 obs. of 4 variables:
$ cut : Ord.factor w/ 5 levels "Fair"<"Good"<..: 5 4 2 4 2
$ carat: atomic 0.23 0.21 0.23 0.29 0.31
..- attr(*, "label")= Named chr "Kt"
.. ..- attr(*, "names")= chr "carat"
$ color: Ord.factor w/ 7 levels "D"<"E"<"F"<"G"<..: 2 2 2 6 7
..- attr(*, "label")= Named chr "Type"
.. ..- attr(*, "names")= chr "color"
$ price: atomic 326 326 327 334 335
..- attr(*, "label")= Named chr "EUR"
.. ..- attr(*, "names")= chr "price"
还有 R-Studio IDE 我可以看到标签 View(df)
。
现在我想通过 knitr/rmarkdown/html toolchain as html using the kable
function from knitr with following code as .Rmd
rmarkdown 文件打印这个数据框。
---
title: "Table with labels"
output: html_document
---
```{r include=FALSE}
library(dplyr)
library(sjmisc)
library(ggplot2)
library(knitr)
data("diamonds")
```
## Example
```{r echo=FALSE, results='asis'}
df= tbl_df(diamonds) %>%
select(cut, carat, color, price) %>%
set_label(c("", "Kt", "Type","EUR")) %>%
slice(1:5)
kable(df)
```
导致 html 其中包含以下内容 table:
问题:
很遗憾,单位标签未用作 header 中的第二行。
问题:
如何获得"carat"下方的"Kt"、"color"下方的"Type"和"price"下方的"EUR"第二 header 行?第二行应与第一行具有相同的水平居中。
我正在寻找一种无需手动将标签手动添加到第二行的解决方案,它应该会自动将标签应用到打印的 table。如果可能,单位标签的字体应比第一行 header 行小一点,或者没有粗体。
如何重写 kable
(在本例中为 html 输出)以满足这些要求并与标记的数据帧一起很好地发挥作用?
kable
的描述告诉我们
This is a very simple table generator. It is simple by design. It is not intended to replace any other R packages for making tables.
所以我不希望您能够直接使用 kable
执行此操作。但是还有其他选择。它们在冗长、简单和效率方面会有所不同。
这是使用 pixiedust
包的一种方法。
library(dplyr)
library(sjmisc)
library(ggplot2)
library(pixiedust)
data("diamonds")
df= tbl_df(diamonds) %>%
select(cut, carat, color, price) %>%
set_label(c("", "Kt", "Type","EUR")) %>%
slice(1:5)
Head <- rbind(names(df),
get_label(df)) %>%
as.data.frame(., stringsAsFactors = FALSE)
dust(df) %>%
redust(., Head, part = "head") %>%
sprinkle_print_method("html")
您可以使用 pixiedust
包中的其他函数来自定义 table 的外观(并且 table 的格式可以很容易地捆绑到自定义函数中以使其以后更容易应用格式)。
我相信 htmlTables
也能做到这一点,虽然我不太熟练。
可重现的例子:
我有一个数据框,其中标记了变量以使用自 v0.4.2 以来的 sjmisc package, which works nicely together with dplyr 来表示单位。
library(dplyr)
library(sjmisc)
library(ggplot2)
data("diamonds")
df= tbl_df(diamonds) %>%
select(cut, carat, color, price) %>%
set_label(c("", "Kt", "Type","EUR")) %>%
slice(1:5)
正如 str(df)
所示,它正确地包含了三列标签:
Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 5 obs. of 4 variables:
$ cut : Ord.factor w/ 5 levels "Fair"<"Good"<..: 5 4 2 4 2
$ carat: atomic 0.23 0.21 0.23 0.29 0.31
..- attr(*, "label")= Named chr "Kt"
.. ..- attr(*, "names")= chr "carat"
$ color: Ord.factor w/ 7 levels "D"<"E"<"F"<"G"<..: 2 2 2 6 7
..- attr(*, "label")= Named chr "Type"
.. ..- attr(*, "names")= chr "color"
$ price: atomic 326 326 327 334 335
..- attr(*, "label")= Named chr "EUR"
.. ..- attr(*, "names")= chr "price"
还有 R-Studio IDE 我可以看到标签 View(df)
。
现在我想通过 knitr/rmarkdown/html toolchain as html using the kable
function from knitr with following code as .Rmd
rmarkdown 文件打印这个数据框。
---
title: "Table with labels"
output: html_document
---
```{r include=FALSE}
library(dplyr)
library(sjmisc)
library(ggplot2)
library(knitr)
data("diamonds")
```
## Example
```{r echo=FALSE, results='asis'}
df= tbl_df(diamonds) %>%
select(cut, carat, color, price) %>%
set_label(c("", "Kt", "Type","EUR")) %>%
slice(1:5)
kable(df)
```
导致 html 其中包含以下内容 table:
问题:
很遗憾,单位标签未用作 header 中的第二行。
问题:
如何获得"carat"下方的"Kt"、"color"下方的"Type"和"price"下方的"EUR"第二 header 行?第二行应与第一行具有相同的水平居中。
我正在寻找一种无需手动将标签手动添加到第二行的解决方案,它应该会自动将标签应用到打印的 table。如果可能,单位标签的字体应比第一行 header 行小一点,或者没有粗体。
如何重写 kable
(在本例中为 html 输出)以满足这些要求并与标记的数据帧一起很好地发挥作用?
kable
的描述告诉我们
This is a very simple table generator. It is simple by design. It is not intended to replace any other R packages for making tables.
所以我不希望您能够直接使用 kable
执行此操作。但是还有其他选择。它们在冗长、简单和效率方面会有所不同。
这是使用 pixiedust
包的一种方法。
library(dplyr)
library(sjmisc)
library(ggplot2)
library(pixiedust)
data("diamonds")
df= tbl_df(diamonds) %>%
select(cut, carat, color, price) %>%
set_label(c("", "Kt", "Type","EUR")) %>%
slice(1:5)
Head <- rbind(names(df),
get_label(df)) %>%
as.data.frame(., stringsAsFactors = FALSE)
dust(df) %>%
redust(., Head, part = "head") %>%
sprinkle_print_method("html")
您可以使用 pixiedust
包中的其他函数来自定义 table 的外观(并且 table 的格式可以很容易地捆绑到自定义函数中以使其以后更容易应用格式)。
我相信 htmlTables
也能做到这一点,虽然我不太熟练。