使用 Reactable 和 Blogdown 进行条件格式化
Conditional Formatting with Reactable and Blogdown
使用 R 的 Reactable 包和 RMarkdown 我想创建一个 table 其中
一列的状态 <= 2 为绿色方块,否则为红色方块。
如果我尝试根据其值设置状态列的格式,它会创建一个非彩色方块。见图。
在下面的 .Rmd 文件中,只有当该列没有值时,我才能使用 css 在该列中创建一个彩色方块。
总而言之,在 Status 列下,我想要没有可见数字的彩色方块(Status <= 2 为绿色方块,否则为红色方块),与列的 Status 名称左对齐。
Flag 列只是为了表明 css 正在工作。
RMarkdown 文件
title: Conditional Colored Square in Reactable Table
author: IX
date: 'September 5, 2020'
slug: test
categories: []
tags: []
output:
html_document
---
```{css, echo=FALSE}
.row {
display : flex;
align-items : center;
margin-bottom: 15px;
}
.box {
height: 20px;
width: 20px;
border: 1px solid black;
margin-right : 5px;
}
.red {
background-color: red;
}
.green {
background-color: green;
}
.blue {
background-color: blue;
}
```
```{r, echo=FALSE, message=FALSE, warning=FALSE}
library("reactable")
orders <- data.frame(
Order = 2300:2304,
Created = seq(as.Date("2019-04-01"), by = "day", length.out = 5),
Customer = sample(rownames(MASS::painters), 5),
Status = c(1, 2, 3, 4, 5),
Flag = c("", "", "", "", "")
)
reactable(orders, columns = list(
Status = colDef(cell = function(value) {
if (value <= 2) {
class <- paste0("tag box green", tolower(value))
htmltools::div(class = class, value)
}
}),
Flag = colDef(cell = function(value) {
class <- paste0("tag box green", tolower(value))
htmltools::div(class = class, value)
})
))
使用 css
是正确的方法。
由于paste
:
,您提供的示例中没有显示颜色
class <- paste0("tag box green", tolower(value))
导致 green1
、green2
、... 是未定义的选择器 类。
尝试:
---
title: Conditional Colored Square in Reactable Table
author: IX
date: 'September 5, 2020'
slug: test
categories: []
tags: []
output:
html_document
---
```{css, echo=FALSE}
.row {
display : flex;
align-items : center;
margin-bottom: 15px;
}
.box {
height: 20px;
width: 20px;
border: 1px solid black;
margin-right : 5px;
}
.green {
background: green;
color: green;
}
.red {
background-color: red;
color: red;
}
```
```{r, echo=FALSE, message=FALSE, warning=FALSE}
library("reactable")
orders <- data.frame(
Order = 2300:2304,
Created = seq(as.Date("2019-04-01"), by = "day", length.out = 5),
Customer = sample(rownames(MASS::painters), 5),
Status = c(1, 2, 3, 4, 5),
Flag = c("", "", "","","")
)
reactable(orders, columns = list(
Status = colDef(cell = function(value) {
if (value <= 2) {
class <- "box green"
} else {
class <- "box red"
}
htmltools::div(class = class, "")
}),
Flag = colDef(cell = function(value) {
class <- paste0("tag box")
htmltools::div(class = class, value)
})
))
使用 R 的 Reactable 包和 RMarkdown 我想创建一个 table 其中 一列的状态 <= 2 为绿色方块,否则为红色方块。
如果我尝试根据其值设置状态列的格式,它会创建一个非彩色方块。见图。
在下面的 .Rmd 文件中,只有当该列没有值时,我才能使用 css 在该列中创建一个彩色方块。
总而言之,在 Status 列下,我想要没有可见数字的彩色方块(Status <= 2 为绿色方块,否则为红色方块),与列的 Status 名称左对齐。
Flag 列只是为了表明 css 正在工作。
RMarkdown 文件
title: Conditional Colored Square in Reactable Table
author: IX
date: 'September 5, 2020'
slug: test
categories: []
tags: []
output:
html_document
---
```{css, echo=FALSE}
.row {
display : flex;
align-items : center;
margin-bottom: 15px;
}
.box {
height: 20px;
width: 20px;
border: 1px solid black;
margin-right : 5px;
}
.red {
background-color: red;
}
.green {
background-color: green;
}
.blue {
background-color: blue;
}
```
```{r, echo=FALSE, message=FALSE, warning=FALSE}
library("reactable")
orders <- data.frame(
Order = 2300:2304,
Created = seq(as.Date("2019-04-01"), by = "day", length.out = 5),
Customer = sample(rownames(MASS::painters), 5),
Status = c(1, 2, 3, 4, 5),
Flag = c("", "", "", "", "")
)
reactable(orders, columns = list(
Status = colDef(cell = function(value) {
if (value <= 2) {
class <- paste0("tag box green", tolower(value))
htmltools::div(class = class, value)
}
}),
Flag = colDef(cell = function(value) {
class <- paste0("tag box green", tolower(value))
htmltools::div(class = class, value)
})
))
使用 css
是正确的方法。
由于paste
:
class <- paste0("tag box green", tolower(value))
导致 green1
、green2
、... 是未定义的选择器 类。
尝试:
---
title: Conditional Colored Square in Reactable Table
author: IX
date: 'September 5, 2020'
slug: test
categories: []
tags: []
output:
html_document
---
```{css, echo=FALSE}
.row {
display : flex;
align-items : center;
margin-bottom: 15px;
}
.box {
height: 20px;
width: 20px;
border: 1px solid black;
margin-right : 5px;
}
.green {
background: green;
color: green;
}
.red {
background-color: red;
color: red;
}
```
```{r, echo=FALSE, message=FALSE, warning=FALSE}
library("reactable")
orders <- data.frame(
Order = 2300:2304,
Created = seq(as.Date("2019-04-01"), by = "day", length.out = 5),
Customer = sample(rownames(MASS::painters), 5),
Status = c(1, 2, 3, 4, 5),
Flag = c("", "", "","","")
)
reactable(orders, columns = list(
Status = colDef(cell = function(value) {
if (value <= 2) {
class <- "box green"
} else {
class <- "box red"
}
htmltools::div(class = class, "")
}),
Flag = colDef(cell = function(value) {
class <- paste0("tag box")
htmltools::div(class = class, value)
})
))