Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
464 views
in Technique[技术] by (71.8m points)

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:

  • coloured rows based on one column values (here - column "a")
  • get all rows as default

The coloured rows code, depending on value of 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? I.e. I would like to display all the rows as default and have different colours of rows, depending on value of column "a".

Thanks in advance!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I have found the answer. I messed the position of options vs formatStyle parts:

  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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...