【问题标题】:R shiny RenderDT format specific cell in table outputR闪亮的RenderDT格式表输出中的特定单元格
【发布时间】:2020-05-29 21:18:11
【问题描述】:

我正在尝试格式化 R 闪亮仪表板数据表中的特定单元格(使用 renderDT)。

在 UI 中,我使用以下代码行:

DTOutput('dt_vols')

我也在 UI 中包含这一行,因为我不想显示列名(不确定这是否与问题相关)

tags$head(tags$style(type = "text/css", "#dt_vols th {display:none;}")),

在服务器代码中,我首先创建了以下反应式 2x2 矩阵(称为 dt_vols) - 我在示例中简化了矩阵

dt_vols <- reactive({

    mtx_vols <- matrix(1:4, nrow = 2, ncol = 2)

    return(mtx_vols)

  })

然后我将DT表渲染如下:

  output$dt_vols = renderDT(

    dt_vols(), options = list(pageLength = 4, dom = 't', autoWidth = FALSE), rownames= FALSE,   
    formatStyle(dt_vols(), columns = 1, border = '1px solid #ddd')

  )

在我添加 formatstyle 行之前它一直有效。我不知道如何让这条线正确并让它指向一个特定的单元格(例如 row1、column2)。 column 参数似乎有问题。如果我运行以下命令,则会出现以下错误:

Warning: Error in : $ operator is invalid for atomic vectors

【问题讨论】:

    标签: r matrix shiny


    【解决方案1】:

    formatStyle 需要一个从 datatable() 创建的表对象作为输入 - 您传递了一个矩阵,这会导致错误。

    请检查以下内容:

    library(shiny)
    library(DT)
    
    ui <- fluidPage(DTOutput('dt_vols'),
                    tags$head(tags$style(type = "text/css", "#dt_vols th {display:none;}")))
    
    server <- function(input, output, session) {
      dt_vols <- reactive({
        mtx_vols <- matrix(1:4, nrow = 2, ncol = 2)
    
        return(mtx_vols)
    
      })
    
      output$dt_vols = renderDT({
        myTable <- datatable(dt_vols(),
                             options = list(pageLength = 4, dom = 't', autoWidth = FALSE),
                             rownames = FALSE
        )
    
        formatStyle(myTable, columns = 1, border = '10px solid #ddd')
      })
    }
    
    shinyApp(ui, server)
    

    【讨论】:

    • 非常感谢,这已经解决了问题并在我的代码中工作。我有一个后续问题。是否可以仅将值为 3(第一行,第二列)的单元格的背景颜色格式化为绿色,然后将值为 2(第 2 行,第 1 列)的单元格的背景颜色格式化为红色?
    • 别担心 - 我已经使用样式间隔 backgroundColor = styleInterval(lst_vals, lst_colors) 解决了这个问题
    猜你喜欢
    • 2015-03-05
    • 1970-01-01
    • 2021-06-14
    • 1970-01-01
    • 2020-08-04
    • 2019-04-29
    • 1970-01-01
    • 2019-11-20
    • 2017-03-06
    相关资源
    最近更新 更多