Unknown column error while switching the reports in my shiny dashboard

It appears for second and then disappears.But i am able to see the report . I can see that warning error in my console as well .Please help me on this. Thank you

library(shiny) 
library(DT)    
library(leaflet)
library(dplyr)

ui <- fluidPage(
  selectInput(
    "FILE", 
    "Select the Report:",
    choices = c("tbl1","tbl2","tbl3")),
  
  checkboxGroupInput(
    "col_n",
    "Columns to display:",
    choices = c()
  ),
  
  DT::dataTableOutput("table_data")  
)

server <- function(input, output,session) {
  
  df <-  reactive ({ 
    switch(input$FILE, 
           "tbl1" = iris, 
           "tbl2" = mtcars,
           "tbl3" = faithful
    )
  })
  
  observeEvent(input$FILE,{
    
    req(df()) 
    updateCheckboxGroupInput(
      session,
      "col_n",
      choices = colnames(df()),
      selected = colnames(df())
    )
  })
  
  output$table_data <-   DT::renderDataTable({
    req(input$col_n) 
    DT::datatable(
      df() %>% select_at(input$col_n),
      rownames = FALSE)
  })
}

shinyApp(ui, server)

This is an unfortunate limitation of using updateCheckboxGroupInput. This is because update...send messages to shiny on the client side, so in your example, shiny is unaware at first that input$col_n has been updated and tries to subset your newly selected df() with the previously selected columns (from the previously selected dataframe). See here for example, for a discussion of this limitation.

A simple (albeit not particularly elegant) workaround is to simply not render the datatable until col_n and df match up - i.e.:

output$table_data <- DT::renderDataTable({
    
    req(all(input$col_n %in% names(df()))) # only render if all selected columns are present in df
    
    DT::datatable(
      df() %>% select_at(input$col_n),
      rownames = FALSE)
  })

Thank you . It worked

1 Like

This topic was automatically closed 54 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.