带有“kable”但带有“%”符号的条件格式
Conditional formatting with `kable` but with "%" signs
我喜欢 kable
和 kableExtra
包,用于表格的条件格式设置并在 Reports 中使用它们。但是,我看到如果您还想在其中包含“%”符号,则无法有条件地格式化表格。有办法解决这个问题吗?
您可以在创建 table 时使用 scales::percent()
,如下所示:
---
title: "test"
output: word_document
---
```{r setup, include=FALSE}
library(tidyverse)
library(knitr)
library(scales)
df <- diamonds
tabl1 <- df %>% group_by( cut) %>% summarise(n = n())
names(tabl1) <- c("Count of Cut", "n")
tabl1$perc <- scales::percent(tabl1$n / sum(tabl1$n))
```
```{r , message= FALSE, echo=FALSE,warning=FALSE}
kable(tabl1)
```
结果:
@heck1 有一个很好的答案,我不知道那个包。将来,如果您包含示例数据、您尝试过的内容以及您想要的结果,将会很有帮助。根据您的评论,我认为您正在寻找类似这样的东西(如下)。当然,您可以更改列名并进行其他您认为合适的修改。
---
title: "test"
output: word_document
---
```{r setup, include=FALSE}
library(tidyverse)
library(knitr)
library(kableExtra)
df <- diamonds
tabl1 <- df %>%
group_by(cut) %>%
summarise(n = n()) %>%
mutate(perc = round(n / sum(n), 3)*100,
cut = cell_spec(cut, color = ifelse(perc < 10, "red", "black")),
perc = paste0(perc, "%"))
```
```{r , message= FALSE, echo=FALSE,warning=FALSE}
kable(tabl1, escape = F) %>%
kable_styling(full_width = F)
不知道您是否仍然坚持这个问题,但我在处理同一问题时遇到了这个问题,并决定 post 我的解决方法。关键是将值的数字版本传递给调色板函数(在本例中为 spec_color),同时使用带有“%”字符的字符值作为 cell_spec 的输入,以便“ %" 包含在 cell_spec returns
的标签中
---
title: "R Notebook"
output:
html_document: default
pdf_document: default
---
```{r setup, include = F}
library(tidyverse)
library(knitr)
library(kableExtra)
options(knitr.table.format = "html")
```
```{r}
df = tibble(
x = c(1, 2, 3, 4, 5),
percents = c("12.7%", "14.0%", "19.2%", "20.4%", "13.2%")
)
```
```{r}
df = df %>%
mutate(percents = cell_spec(percents, format = "html",
#I first remove the "%" character,
#then coerce the column to a numerical value so that
#the color palette function can handle it
color = spec_color(as.numeric(str_sub(percents, end = -2L))))
)
df %>% kable(format = "html", escape = F) %>% kable_styling()
```
我喜欢 kable
和 kableExtra
包,用于表格的条件格式设置并在 Reports 中使用它们。但是,我看到如果您还想在其中包含“%”符号,则无法有条件地格式化表格。有办法解决这个问题吗?
您可以在创建 table 时使用 scales::percent()
,如下所示:
---
title: "test"
output: word_document
---
```{r setup, include=FALSE}
library(tidyverse)
library(knitr)
library(scales)
df <- diamonds
tabl1 <- df %>% group_by( cut) %>% summarise(n = n())
names(tabl1) <- c("Count of Cut", "n")
tabl1$perc <- scales::percent(tabl1$n / sum(tabl1$n))
```
```{r , message= FALSE, echo=FALSE,warning=FALSE}
kable(tabl1)
```
结果:
@heck1 有一个很好的答案,我不知道那个包。将来,如果您包含示例数据、您尝试过的内容以及您想要的结果,将会很有帮助。根据您的评论,我认为您正在寻找类似这样的东西(如下)。当然,您可以更改列名并进行其他您认为合适的修改。
---
title: "test"
output: word_document
---
```{r setup, include=FALSE}
library(tidyverse)
library(knitr)
library(kableExtra)
df <- diamonds
tabl1 <- df %>%
group_by(cut) %>%
summarise(n = n()) %>%
mutate(perc = round(n / sum(n), 3)*100,
cut = cell_spec(cut, color = ifelse(perc < 10, "red", "black")),
perc = paste0(perc, "%"))
```
```{r , message= FALSE, echo=FALSE,warning=FALSE}
kable(tabl1, escape = F) %>%
kable_styling(full_width = F)
不知道您是否仍然坚持这个问题,但我在处理同一问题时遇到了这个问题,并决定 post 我的解决方法。关键是将值的数字版本传递给调色板函数(在本例中为 spec_color),同时使用带有“%”字符的字符值作为 cell_spec 的输入,以便“ %" 包含在 cell_spec returns
的标签中---
title: "R Notebook"
output:
html_document: default
pdf_document: default
---
```{r setup, include = F}
library(tidyverse)
library(knitr)
library(kableExtra)
options(knitr.table.format = "html")
```
```{r}
df = tibble(
x = c(1, 2, 3, 4, 5),
percents = c("12.7%", "14.0%", "19.2%", "20.4%", "13.2%")
)
```
```{r}
df = df %>%
mutate(percents = cell_spec(percents, format = "html",
#I first remove the "%" character,
#then coerce the column to a numerical value so that
#the color palette function can handle it
color = spec_color(as.numeric(str_sub(percents, end = -2L))))
)
df %>% kable(format = "html", escape = F) %>% kable_styling()
```