【问题标题】:How to add conditional formatting to shiny tables in R?如何为 R 中的闪亮表添加条件格式?
【发布时间】:2016-07-16 14:24:01
【问题描述】:

下面提到的代码细节。

**server.R**
shinyServer(function(input, output) {

  getTable <- reactive({
    // Some manipulations done every 2 mins and table is updated
    // Assume tbl_op has only one row with 3 columns
    // The table values represent time in hh:mm:ss
    tbl_op
  })

  output$tableUI <- renderTable({
    getTable()
  },include.rownames=FALSE)  
})

**ui.R**
shinyUI(
  fluidPage(    
    fluidRow(      
      column(width = 5, offset = 1,             
             tableOutput("tableUI")
      )
    )
  )
)

我能够以所需的格式显示输出,但是我无法为其添加格式。

当时间差(系统时间与表格单元格中显示的值之间的绝对差值)超过 2 分钟时,我希望表格单元格值以红色突出显示。该表每两分钟更新一次。当差异超过 2 分钟时,应突出显示单元格值。

如果Datatable可以,请提供代码。

【问题讨论】:

  • 可以对 Shiny 中显示的数据表应用条件格式,但这是一项相当复杂的练习,你看过this discussion 吗?如果我可以冒昧地提出另一个建议,您为什么不将您的代码重新发布为带有一些虚拟数据的单个 app.R 文件?人们使用起来会更容易,我想看看这个但失去了兴趣,因为它看起来需要大量工作,因为必须生成所有数据和转换。

标签: r shiny


【解决方案1】:

有两个任务:

格式化 DT 单元格

可以使用formatStyle(完成

每 2 分钟更新一次

可以使用reactiveTimer来完成

试试类似的东西:

用户界面

library(DT)
library(shiny)

  shinyUI(

      fluidRow(      

        DT::dataTableOutput("tableUI")

      )
    )
  )

服务器

library(shiny)
library(DT)


tbl_op=data.frame(z=as.POSIXct(Sys.time())+1:10 )


shinyServer(function(input, output,session) {

  ttt=reactiveValues(tbl_op=tbl_op)

  autoInvalidate <- reactiveTimer(2000, session)

  observe({
  autoInvalidate()
 print(Sys.time())
 ttt$tbl_op$check=ifelse(Sys.time()> ttt$tbl_op$z,1,0)
})

    output$tableUI <- DT::renderDataTable({
        DT::datatable( ttt$tbl_op,rownames=F,
                      options=list(columnDefs = list(list(visible=FALSE,targets=1))))%>%formatStyle("check",target = 'row',backgroundColor = styleInterval(0.5,c("red","none")))
    })  
  })

PS

我每 2 秒更新一次,并将单元格中的值与当前时间进行比较(为了测试简单)

【讨论】:

    猜你喜欢
    • 2021-02-13
    • 2016-11-03
    • 2021-06-13
    • 1970-01-01
    • 1970-01-01
    • 2015-03-05
    • 2020-08-04
    • 2016-04-10
    • 1970-01-01
    相关资源
    最近更新 更多