R Shiny DT 输出 - 背景行着色并显示所有结果冲突

R Shiny DT output - background rows coloured and displayed all results clash

在我的 renderDataTable 中,我试图同时获取两件事:

彩色行代码,取决于 a 的值:

    library(shiny); library(DT)
ui <- fluidPage(mainPanel(
                sliderInput("seed",
                            "Set seed",
                            min = 1,  max = 100,  value = 1, step = 1),
                mainPanel(
                  tabsetPanel(
                    tabPanel("Table", DT::dataTableOutput("table1"))
              ))))
server <- function(input, output, session){
     df1 <- reactive({
  req(input$seed)
  a <- c(rep(1,3), rep(2,3), rep(3,3),rep(4,5))
   set.seed(input$seed)
    b <- c(runif(3),runif(3),runif(3),runif(5))
    df <- data.frame(a,b)
  })     
      output$table1 <- DT::renderDataTable({
      {df1() %>% datatable() %>%
          formatStyle(1, target = 'row', 
                      backgroundColor = styleEqual(c(seq(1, 99, 2),seq(2, 100, 2)), c(rep("#F5F5F5",50),rep("#FFFFFF",50))))}
      #options = list(lengthMenu = list(c(5, 15, -1), c('5', '15', 'All'),pageLength = -1))
    })
}
shinyApp(ui, server)

然后,我可以默认显示 table 的所有行:

library(shiny); library(DT)
    ui <- fluidPage(mainPanel(
      sliderInput("seed",
                  "Set seed",
                  min = 1,  max = 100,  value = 1, step = 1),
      mainPanel(
        tabsetPanel(
          tabPanel("Table", DT::dataTableOutput("table1"))
        ))))
    server <- function(input, output, session){
      df1 <- reactive({
        req(input$seed)
        a <- c(rep(1,3), rep(2,3), rep(3,3),rep(4,5))
        set.seed(input$seed)
        b <- c(runif(3),runif(3),runif(3),runif(5))
        df <- data.frame(a,b)
      })    
      output$table1 <- DT::renderDataTable(DT::datatable({df1()}, options = list(lengthMenu = list(c(5, 15, -1), c('5', '15', 'All')),pageLength = -1)))
    
    }
    shinyApp(ui, server)

我的问题是:如何结合这两个功能? IE。我想默认显示所有行,并根据列“a”的值显示不同颜色的行。

提前致谢!

我找到了答案。我弄乱了选项与格式样式部分的位置:

  output$table1 <- DT::renderDataTable({
    DT::datatable(df1(),
    options=list(pageLength=-1,
                 lengthMenu = (list(c(5, 15, -1), c('5', '15', 'All')))
              )) %>%  # end of options
    formatStyle(1, target = 'row',backgroundColor = styleEqual(c(seq(1, 99, 2),seq(2, 100, 2)), c(rep("#F5F5F5",50),rep("#FFFFFF",50))))
 

 })

祝你有美好的一天 Greg3er