简体   繁体   English

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

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

In my renderDataTable, I am trying to get two things at the same time:在我的 renderDataTable 中,我试图同时得到两件事:

  • coloured rows based on one column values (here - column "a")基于一列值的彩色行(此处 - 列“a”)
  • get all rows as default默认获取所有行

The coloured rows code, depending on value of a:彩色行代码,取决于 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)

Then, I can display all the rows of the table as default:然后,我可以默认显示表的所有行:

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)

My question is: how to combine those two features?我的问题是:如何结合这两个功能? Ie I would like to display all the rows as default and have different colours of rows, depending on value of column "a".即,我想将所有行显示为默认值并具有不同颜色的行,具体取决于列“a”的值。

Thanks in advance!提前致谢!

I have found the answer.我找到了答案。 I messed the position of options vs formatStyle parts:我弄乱了选项与 formatStyle 部分的 position :

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

 })

Have a good day Greg3er祝你有美好的一天 Greg3er

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM