Unable to drop / select multiple columns in Shinyapp

I'm using modified mtcars data for building shinyApp. I have put checkboxgroupinput for selecting columns like cyl, vs, disp.
But its currently not working.
I have also put column visibility of DT library for the same purpose, but when i drop columns and download data, it shows the full output in excel.
I'm pasting my codes as well. Please have a look. Many thanks :slight_smile:

data_table<-mtcars[,c(2,8,3,1,4,5,9,6,7, 10,11)]

ncol(data_table)


names(data_table)[4:11]<- rep(x =                                 
                                
c('OTS_lhr_Wave_1','OTS_isb_Wave_2','OTS_lhr_Wave_2','OTS_isb_Wave_1',                                                                            
                                                                 
'NTS_lhr_Wave_1','NTS_isb_Wave_2','NTS_lhr_Wave_2','NTS_isb_Wave_1'), 
                              times=1, each=1) 


library(readr)  
library(shiny)   
library(DT)     
library(dplyr) 
library(shinythemes) 
library(htmlwidgets) 
library(shinyWidgets) 



ui = fluidPage( 
  sidebarLayout(
    sidebarPanel (
      
      
      downloadButton(outputId = 
                       "downLoadFilter",
                     label = "Download data"),
      
      
      
      
      selectInput(inputId = "cyl",
                  label = "cyl:",
                  choices = c("All",
                              
                              unique(as.character(data_table$cyl))),
                  selected = "All",
                  multiple = TRUE),
      
      
      selectInput(inputId = "vs",
                  label = "vs:",
                  choices = c("All",
                              
                              unique(as.character(data_table$vs))),
                  selected = "All",
                  multiple = TRUE),
      
      
      
      selectInput(inputId = "disp",
                  label = "disp:",
                  choices = c("All",  
unique(as.character(data_table$disp))),
                  selected = "All",
                  multiple = TRUE),
      
      
      checkboxGroupInput(inputId = "columns", 
                         label = "Select Columns to display:",
                         choices =c('cyl', 'vs', 
                                    'disp'),
                         selected = c('cyl', 'vs', 
                                      'disp'), inline  = TRUE),
      
      
      radioButtons(inputId = "variables", 
                   label = "Choose Variable(s):",
                   choices =c("All","OTS", 
                              "NTS"), inline = FALSE,
                   selected = c("OTS")),
      
      
      
      selectInput(inputId = "regions", label = "choose region",
                  choices =c("lhr", 
                             "isb"), 
                  multiple = TRUE,   
                  selected = c("lhr")),
      
      
      
      
      selectInput(inputId = "waves", label =  "choose wave",
                  choices =c("Wave_1", 
                             "Wave_2"), multiple  = TRUE,
                  selected = c("Wave_1"))
      
    ),
    
    
    mainPanel(
      tags$h5('Download only current page using following 
              buttons:'),
      DT::dataTableOutput('mytable') )))



server = function(input, output, session) {
  
  #tab 1
  thedata <- reactive({
    
    
    # #customise columns by selecting variables of your choice
    
    # data_table<-data_table[,c(input$columns),drop=FALSE]
    # 
    # 
    if(input$cyl != 'All'){
      data_table<-data_table[data_table$cyl %in% input$cyl,]
    }
    
    if(input$vs != 'All'){
      data_table<-data_table[data_table$vs %in% input$vs,]
    }
    
    
    if(input$disp != 'All'){
      data_table<-data_table[data_table$disp %in% input$disp,]
    }
    
    
    #starting OTS NTS
    
    
    if  (input$variables== 'All'){
      data_table<-  data_table[,c("cyl", "vs", "disp" ,
                                  names(data_table[grep(pattern = "TS", x = 
                                                          names(data_table), 
fixed = TRUE)])),drop=FALSE]    }
    
    
    if  (input$variables== 'OTS'){
      data_table<-  data_table[,c("cyl", "vs", "disp" ,
                                  names(data_table[grep(pattern = "OTS", x = 
                                                          names(data_table), 
fixed = TRUE)])),drop=FALSE]    }
    
    
    
    if  (input$variables== 'NTS'){
      data_table<-  data_table[,c("cyl", "vs", "disp" ,
                                  names(data_table[grep(pattern = "NTS", x = 
                                                          names(data_table), 
fixed = TRUE)])),drop=FALSE]    }
    
    
    #Region
    all_cols <- names(data_table)
    region_cols <- c("cyl", "vs", "disp" )
    
    
    if  ('lhr' %in% input$regions){
      region_cols <- c(region_cols, all_cols[grep('lhr', all_cols, fixed = 
                                                    TRUE)])
      
    }  
    if  ('isb' %in% input$regions){
      region_cols <- c(region_cols, all_cols[grep('isb', all_cols, fixed = 
                                                    TRUE)])
      
    }
    
    #Waves
    waves_cols <- c("cyl", "vs", "disp" )
    
    
    if  ('Wave_1' %in% input$waves){
      waves_cols <- c(waves_cols, all_cols[grep('Wave_1', all_cols, fixed = 
                                                  TRUE)])
    }  
    
    if  ('Wave_2'  %in%  input$waves){
      waves_cols <- c(waves_cols, all_cols[grep('Wave_2', all_cols, fixed = 
                                                  TRUE)])
    }
    
    
    data_table <- data_table[,intersect(region_cols, waves_cols), 
drop=FALSE]
    
    
  })
  
  
  output$mytable = DT::renderDataTable({
    
    DT::datatable( filter = "top",  rownames = FALSE, escape = FALSE,
                   class = 'cell-border stripe',
                   extensions = c('FixedHeader', 'Buttons'),
                   options = list(pageLength = 50, autowidth=FALSE, 
                                  fixedHeader = TRUE, 
                                  dom = 'Brtip', 
                                  
                                  
                                  buttons = list('copy', 'print', 
                                                 list(extend = 'collection', 
                                                      buttons = c('csv', 
                                                                  'excel', 
'pdf'), 
                                                      text = 'Download'), 
                                                 list(extend = 'colvis', 
                                                      columns = c(0,1,2)))
                                  
                                  
                   ),
                   {     
                     
                     thedata()   # Call reactive thedata()
                     
                     
                   }) 
    
  })
  
  
  output$downLoadFilter <- downloadHandler(
    filename = function() {
      paste('Filtered Data ', Sys.time(), '.csv', sep = '')
    },
    content = function(path){
      write_csv(thedata(), path)  # Call reactive thedata()
    }
  )
  
}      

shinyApp(ui = ui, server = server)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.