DT表中的活性色

问题描述 投票:0回答:1

需要帮助。下面是应用程序。 ColB将根据ColA中的数字进行着色。下面是条件表

enter image description here

简而言之:如果ColB为2,则它小于6(12/2),应该为红色,其他颜色也应类似。我试图自己构建代码,并在下面提出。但是看起来代码中存在一些问题。我还在下面附加了输出,逻辑无法正常工作。

---
title: "Untitled"
runtime: shiny
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
---

```{r setup, include=FALSE}
library(flexdashboard)
library(DT)
```

```{r}
tab1 <- data.frame(ColA = c(12,34,45,56), ColB = c(2,32,30,56))
```

Column {data-width=650}
-----------------------------------------------------------------------

### Chart A

```{r}
DT::DTOutput("table1")
output$table1 <- DT::renderDT(
    datatable(tab1))
```

下面是我得到的输出enter image description here

因此,按照代码,突出显示的箭头显示颜色。

第一个箭头(假定为红色,但显示为黄色)第二个箭头(假定为黄色,但显示为绿色)注意:ColB是随机生成的,运行时可能看不到这些数字。但是您会随机观察,当然也可以在运行时发现此问题。不知道代码中有什么问题。以下是供您参考的代码

shiny dt
1个回答
0
投票

您可以使用formatStyleDT)中的link

---
title: "Untitled"
runtime: shiny
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
---

```{r setup, include=FALSE}
library(flexdashboard)
library(tidyverse)
library(DT)
```

```{r}
tab1 <- data.frame(ColA = c(12,34,45,56), ColB = c(2,32,30,56)) %>%
  dplyr::mutate(backgroundColB = case_when(
    ColA==ColB ~ 1,
    ColA/2>ColB ~ -1,
    ColA/2<ColB ~ 0
  ))
```

Column {data-width=650}
-----------------------------------------------------------------------

### Chart A

```{r}
DT::DTOutput("table1")
output$table1 <- DT::renderDT(
    datatable(tab1, options=list(columnDefs = list(list(visible=FALSE, targets=2)))) %>%
  formatStyle('ColB', "backgroundColB",
              backgroundColor = styleInterval(c(-.5,.5), c("red","yellow","green") ))
)
```
© www.soinside.com 2019 - 2024. All rights reserved.