ixo*_*did 5 r r-markdown reactable
使用 R 的Reactable包和 RMarkdown 我想创建一个表,其中列的状态 <= 2 为绿色方块,否则为红色方块。
如果我尝试根据状态列的值设置其格式,它会创建一个非彩色方块。参见图片。
在下面的 .Rmd 文件中,仅当列没有值时,我才可以使用 css 在列中制作彩色方块。
总而言之,在“状态”列下,我想要没有可见数字的彩色方块(状态 <= 2 为绿色方块,否则为红色方块),与列的状态名称左对齐。
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)
})
))
Run Code Online (Sandbox Code Playgroud)
使用css才是正确的方法。
由于以下原因,颜色没有出现在您提供的示例中paste:
class <- paste0("tag box green", tolower(value))
Run Code Online (Sandbox Code Playgroud)
这会导致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)
})
))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
951 次 |
| 最近记录: |