我正在构建一款使用R shiny、bslb和dt的应用程序.

在以前的Bootstrap版本中,使用formatStyle函数可以很容易地更改DT的样式(包括某些行的 colored颜色 ). 但是,对于新的应用程序,我们使用了bslb和bootstrap 5,该功能似乎崩溃了,即使GitHub上的用户说应该解决(https://github.com/rstudio/DT/issues/1102). 在对dt和bslb的不同版本(发布和开发)进行了一些测试后,我仍然无法使其工作.

有没有其他方法可以用bslb和bootstrap 5动态更改DT的某些成分的 colored颜色 ?

library(bslib)
library(DT)

ui <- function(id) {
 card(
   DT::dataTableOutpu("repeatsTable")
}

server <- function(id) ({      
  output$repeatsTable <- DT::renderDataTable({
  repeatsDT <- DT::datatable(repeatsData(),
        extensions = c("Buttons", "ColReorder"),
        selection = "single",
        options = list(
          dom = "Bfrtip",
          buttons = list(I("colvis")),
          scrollX = TRUE,
          colReorder = TRUE
        )
      ) %>%
        formatStyle("isPathogenic", target = "row", color = styleEqual(c(0, 1, 2), c("black", "orange", "red")))
    })
})

编辑:添加可在其自身上运行的虚拟工作代码(来自链接上部)

library(shiny)
library(dplyr)
library(DT)
library(bslib)

ui <- bslib::page_sidebar(
  DT::DTOutput("tab1")
)

server <- function(input, output) {
  output$tab1 <- DT::renderDT({
    mtcars %>% 
      head() %>% 
      # this line works
      #DT::datatable(style = "default") %>% 
      DT::datatable() %>% 
      DT::formatStyle(
        columns = "mpg", 
        target = "row", 
        color = DT::styleEqual(c(21), "white"),
        backgroundColor = DT::styleEqual(c(21), "#4d4d4d"),
      )
  })
}

shinyApp(ui = ui, server = server)

推荐答案

问题是单元格也有一些css属性,它们的优先级高于行的css属性.解决方法是取消设置单元格的css属性:

  output$tab1 <- DT::renderDT({
    mtcars %>% 
      head() %>% 
      DT::datatable() %>% 
      DT::formatStyle(
        columns = 1:ncol(mtcars), 
        target = "cell", 
        color = JS("\"unset\""),
        backgroundColor = JS("\"unset\"")
      ) %>%
      DT::formatStyle(
        columns = "mpg", 
        target = "row", 
        color = DT::styleEqual(c(21), "white"),
        backgroundColor = DT::styleEqual(c(21), "orange")
      )
  })

R相关问答推荐

使用预定值列表将模拟数量(n)替换为rnorm()

根据shiny 应用程序中的数字输入更改图标 colored颜色

使用R的序列覆盖

ggplot geom_smooth()用于线性回归虚拟变量-没有回归线

判断字符串中数字的连续性

在GGPLATE中将突出的点放在前面

在R中,如何将变量(A,B和C)拟合在同一列中,如A和B,以及A和C在同一面板中?

如何使用列表中多个列表中的第一条记录创建数据框

如何使用ggplot对堆叠条形图进行嵌套排序?

在rpart. plot或fancyRpartPlot中使用带有下标的希腊字母作为标签?

如何写商,水平线,在一个单元格的表在R

计算数据帧中指定值之前的行数,仅基于每行之后的future 行,单位为r

具有重复元素的维恩图

来自程序包AFEX和amp;的类/函数和NICE_TABLE&冲突

在点图上绘制置信度或预测区间ggplot2

将统计检验添加到GGPUBR中的盒图,在R

根据排名的顶点属性调整曲线图布局(&Q)

Package emMeans:如果emmip模型中包含的变量较少,emMeans模型中的其他变量设置为什么?

以列名的字符向量作为参数按行应用自定义函数

残差与拟合图上标记点的故障排除