如何在R中向闪亮的表添加条件格式?

时间:2022-10-18 19:15:21

Code details mentioned below.

下面提到的代码细节。

**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")
      )
    )
  )
)

I am able to display the output in the required format, however I am not able to add formatting to it.

我能够以所需的格式显示输出,但是我不能向它添加格式。

I want the table cell values to be highlighted in red when the time difference(absolute value of difference between System Time and the value displayed in the table cell) is more than 2 min. The table is updated every two minutes. The cell value should be highlighted whenever the difference is more than 2 min.

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

If it is possible with Datatable, then please provide the code.

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

1 个解决方案

#1


0  

There is two task :

有两个任务:

Format DT cells

it can be done using formatStyle(

可以使用formatStyle(

Update every 2 mins

it can be done using reactiveTimer

它可以使用reactiveTimer来完成

Try something like :

尝试:

UI

用户界面

library(DT)
library(shiny)

  shinyUI(

      fluidRow(      

        DT::dataTableOutput("tableUI")

      )
    )
  )

Server

服务器

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

I update every 2 seconds and compare value in cells with current time ( for test simplisity)

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

#1


0  

There is two task :

有两个任务:

Format DT cells

it can be done using formatStyle(

可以使用formatStyle(

Update every 2 mins

it can be done using reactiveTimer

它可以使用reactiveTimer来完成

Try something like :

尝试:

UI

用户界面

library(DT)
library(shiny)

  shinyUI(

      fluidRow(      

        DT::dataTableOutput("tableUI")

      )
    )
  )

Server

服务器

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

I update every 2 seconds and compare value in cells with current time ( for test simplisity)

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