如何为DT表的给定行间隔赋予颜色?

Pro*_*Man 4 r shiny

我正在使用DT库来可视化表,但假设我要为某些行提供颜色,例如从第1行到第4行的RED:

在此处输入图片说明

如果可能的话,更改文本的颜色也是非常好的。经过数小时的搜索,我从库DT中发现了此功能:

datatable(df, rownames = FALSE) %>%
  formatStyle(columns = "inputval", 
              background = styleInterval(c(0.7, 0.8, 0.9)-1e-6, c("white", "lightblue", "magenta", "white"))) 
Run Code Online (Sandbox Code Playgroud)

但是我需要为所有列赋予颜色,而不仅仅是inputval代码中的选定列,我是否可以给columnsnames(df)这样的值赋值,以便它可以为所有列赋予颜色?而且styleInterval选择表中的值而不是行的间隔,我该怎么做,以便选择行并给它们指定颜色?

Por*_*hop 6

像这样的事情应该做的。请注意,我为行2:4而不是1:4着色是为了获得更多功能:

library(shiny)
library(DT)

ui <- basicPage(
  mainPanel(DT::dataTableOutput('mytable'))
)

server <- function(input, output,session) {

  output$mytable = DT::renderDataTable(    
    DT::datatable(mtcars,  options = list(
      pageLength = 25,
      rowCallback = JS('function(row, data, index, rowId) {',
                       'console.log(rowId)','if(rowId >= 1 && rowId < 4) {',
                       'row.style.backgroundColor = "pink";','}','}')
    )
    )
  ) 




}
runApp(list(ui = ui, server = server))
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

编辑:动态为行着色:在这里,我只是用来sub替换为行着色的范围

library(shiny)
library(DT)

fnc <- JS('function(row, data, index, rowId) {',
                    'console.log(rowId)','if(rowId >= ONE && rowId < TWO) {',
                    'row.style.backgroundColor = "pink";','}','}')

ui <- basicPage(
  sliderInput("colorrows", "Which to color:",min = 0, max = 10, value = c(1,3)),
  mainPanel(DT::dataTableOutput('mytable'))
)

server <- function(input, output,session) {

  Coloring <- eventReactive(input$colorrows,{
    fnc <- sub("ONE",input$colorrows[1],fnc)
    fnc <- sub("TWO",input$colorrows[2],fnc)
    fnc
  })

  output$mytable = DT::renderDataTable(
    DT::datatable(mtcars,  options = list(pageLength = 25,rowCallback = Coloring())
    )
  )
}
runApp(list(ui = ui, server = server))
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明