【问题标题】:Reactive coloring in DT tableDT 表中的反应性着色
【发布时间】:2020-04-24 16:51:06
【问题描述】:

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

简要说明:如果 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))
```

下面是我得到的输出

所以根据代码,突出显示的箭头显示颜色。

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

【问题讨论】:

    标签: shiny dt


    【解决方案1】:

    您可以使用formatStyle 中的DT (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") ))
    )
    ```
    

    【讨论】:

    • 非常感谢。但我也试过了。我已经添加到我的问题中。但是代码中存在一些问题,虽然逻辑是正确的
    • 您好,我已经重新运行脚本,添加了您指出的行,并且它们按照您想要的方式着色。我认为您更改了脚本的某些内容,如果没有您的确切代码,我无法进一步帮助您。
    猜你喜欢
    • 2019-04-12
    • 2021-11-09
    • 2021-05-05
    • 2020-11-20
    • 2015-06-29
    • 2018-05-23
    • 2018-11-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多