Problem with dropping columns in Table Container using Inputs

datatable

#1

I'm using Data Table library in Shiny to create a Table Container.
I want to drop columns using checkboxGroupInput or any input but currently its not working.
Can someone please guide me? I shall be extremely grateful.
Same question is posted on stackoverflow as follows, you can cross check answers:


Regards

My Codes:

    library(shiny)
    library(DT)

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

    ui =basicPage(

    selectInput(inputId = "Species", 
              label = "Species:",
              choices = c("All",
                          unique(as.character(iris$Species)))),
  
  
    checkboxGroupInput(inputId = "columns", label = "Select Variable:",
                            choices =names(iris),selected = names(iris)),

  
    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(filter = "top",  rownames = FALSE, container = sketch,
                    extensions = 'Buttons', options = list(dom = 'Bfrtip', buttons = 
                                                           
                                                           c('copy', 'csv', 'excel', 'pdf', 'print')
                                                                            ),
                   
                    
                     {
                       
                       data<-iris
                       
                       if(input$Species != 'All'){
                         data<-data[data$Species == input$Species,]
                       }
                       
                       
                       data<-data[,c(input$columns),drop=FALSE]
                       
                        
                       data
                       
                     })}) }


    shinyApp(ui = ui, server = server)

#2

It's a good idea to get your basic data frame subsetting code working outside of shiny first, before adapting it for use in your app.

Remember that you're subsetting the data frame, not the final displayed table, so you need to use variable names that appear in the data frame (e.g., there is no variable called Sepal in iris). It looks like you need to introduce an intermediate step to translate the values in your UI into values that can be used to subset your data frame. You don't seem to be selecting specific variables, but rather variables that match a pattern (e.g., contain "Sepal" or contain "Length"), so the logic to translate the UI selection into the right set of variable names or indices needs to reflect that.

Also, what do you expect to happen to the Species column when someone selects variables? Should it stay there no matter what? If so, you need to account for that in your subsetting logic, as well.


#3

Thanks a lot for the suggestion, it's very kind of you. I have updated the checkboxgroupInput (naming part) accordingly. For the rest of the part i'm not able to figure out due to my limited knowledge. If possible could you please update code? Many thanks for that.
For the Species column, i want it to change accordingly, not stay fixed.
Regards


#4

From here, I'd recommend working through your problem in stages:

  1. Get the UI for selecting rows and columns working without the custom table container. If you give it a try right now, you'll see that you've got some decisions to make.

    For example, since you're filtering the rows based on Species, if you allow your user to de-select Species then the table won't have any rows. So you need to decide how to handle that case. And from a UI perspective, having "All" as a checkbox option alongside each column name is a little confusing — what should happen if the user has selected both "All" and a single column? Maybe a different UI widget would be more appropriate to the interaction flow you are expecting?

    These are not the kind of decisions other people can make for you — only you know your app's goals and constraints well enough to decide what you need.

  2. Once you have things working the way you want without the custom container, then start figuring out what needs to change to adapt to the custom container. Trying to implement this before the main app logic is stable seems to me like a recipe for unnecessary work!

The flip side of shiny's massive acceleration of web app development is that I think sometimes people hit the point where their ideas have outrun their current skills that much more quickly. Luckily the Shiny Dev Center has a ton of resources for leveling up! :smile: If you break the problem into pieces and attack them one by one, you can build your app and your skills at the same time.


#5

Many Thanks, i have updated the codes by removing 'All' from checkboxgroupinput. The good thing is that my app is working fine without custom container, i can drop the columns as well.
But after inserting custom container i'm unable to drop the columns, i guess its the only main hurdle left now.
There's one more issue which i just found out, a copy button would appear at the top when you run the app, if you export data to Excel then top headers i.e Sepal and Petal don't appear which is shocking? If these headers don't appear then it won't be much useful to build the app. Could you please shed some light on it?

Previously i was using RpivotTables in Shiny, the only problem was that it was giving wrong repetition of Headers when data was exported to excel. So i had to drop the idea of using RpivotTables though it was the best package for showing multiple headers in shiny with minimum input. And then i moved to DT library having Custom tables.
Kind regards


#6

This post was flagged by the community and is temporarily hidden.