rstudio / DT

R Interface to the jQuery Plug-in DataTables
https://rstudio.github.io/DT/
Other
596 stars 182 forks source link

Unable to filter multiple headers in Custom Container using DT::DataTable #622

Open SaadAmin5 opened 5 years ago

SaadAmin5 commented 5 years ago

I'm using custom container in R ShinyApp. Its currently having Sepal and Petal as headers that are both containing Length and Width columns. So is it possible to get a dropdown from Sepal/Petal for selecting/filtering Length or Width?
i.e. filter out the headers within headers. I'm currently using checkboxGroupInput for this purpose but its not giving required results. I have attached my codes as well. Can someone please sort it out. Thanks in advance :)

MY Codes:

library(shiny)
library(DT)
iris<-iris[,c(5,1:4)]

ui =basicPage(
tags$head(
tags$style(type = "text/css",
       HTML("th { text-align: center; }")  )),

selectInput(inputId = "Species", 
      label = "Species:",
      choices = c("All",
                  unique(as.character(iris$Species)))),

 checkboxGroupInput(inputId = "columns", label = "Select Variable:",
             choices =c("Sepal.Length", "Sepal.Width", "Petal.Length", 
 "Petal.Width"),
             selected = c("Sepal.Length", "Sepal.Width", "Petal.Length", 
 "Petal.Width")),

h2('Iris Table'),
DT::dataTableOutput('mytable') )

server = function(input, output) {
output$mytable = DT::renderDataTable({

 # a custom table container
sketch = htmltools::withTags(table(
class = 'display',
thead(
tr(
  th(rowspan = 2, 'Species'),
  th(colspan = 2, 'Sepal'),
  th(colspan = 2, 'Petal')),
tr(
  lapply(rep(c('Length', 'Width'), 2), th)
)) )) 

DT::datatable( rownames = FALSE, container = sketch,
          extensions = 'Buttons',
             options = list(dom = 'Bfrtip',
                         buttons = 
                           list('colvis', list(
                             extend = 'collection',
                             buttons = list(list(extend='csv',
                                                 filename = 'hitStats'),
                                            list(extend='excel',
                                                 filename = 'hitStats'),
                                            list(extend='pdf',
                                                 filename= 'hitStats'),
                                            list(extend='copy',
                                                 filename = 'hitStats'),
                                            list(extend='print',
                                          filename = 'hitStats')),

                             text = 'Download' ))),
           {

            data<-iris

            if(input$Species != 'All'){
              data<-data[data$Species == input$Species,]
            }    

            data<-data[,c("Species",input$columns),drop=FALSE]   

            data   
          }) })    }

shinyApp(ui = ui, server = server)
stla commented 5 years ago

I'm not sure to understand. Do you mean something like this ? :

Capture