Unable to clear the displayed output in ShinyApp using actionButton

I'm building a shinyApp on mtcars data. I got 2 actionButtons (Go & Clear).
The Go button is for displaying the output on mainPanel whereas the Clear button is for clearing that output.
My Clear button isn't working due to some unforeseen reason. Can somebody please have a look at my codes. I shall be extremely grateful.

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

data_table<-mtcars

#ui
ui = fluidPage( 
  sidebarLayout(
    sidebarPanel (
      
      uiOutput("cyl_selector"),
      uiOutput("disp_selector"),
      
      actionButton(inputId = "go", label = "Go"),
      actionButton(inputId = "reset", label = "Clear")),
    
    
    mainPanel(
           DT::dataTableOutput('mytable') )))



#server
server = function(input, output, session) {
  
  output$cyl_selector <- renderUI({
    
    selectInput(inputId = "cyl",
                label = "cyl:", multiple = TRUE,
                choices = c( unique(as.character(data_table$cyl))),
                selected = c('4')) })
  

  output$disp_selector <- renderUI({
    
    available <- data_table[c(data_table$cyl %in% input$cyl ), "disp"]  
    
    selectInput(
      inputId = "disp", 
      label = "disp:",
      multiple = TRUE,
      choices = c('All',as.character(unique(available))),
      selected = 'All') })
  
  
  thedata <- eventReactive(input$go,{
 
    data_table<-data_table[data_table$cyl %in% input$cyl,]
 
    
    if(input$disp != 'All'){
      data_table<-data_table[data_table$disp %in% input$disp,]
    }

    data_table
 })


 # thedata <- eventReactive(input$reset,{
 #   data_table<-NULL
 # })

  
  output$mytable = DT::renderDataTable({
    
    DT::datatable( filter = "top",  rownames = FALSE, escape = FALSE,
                   options = list(pageLength = 50, autowidth=FALSE,
                                  dom = 'Brtip'  ),
                   {     
                     thedata()   # Call reactive thedata()
                   })
 })}  
shinyApp(ui = ui, server = server)

If you put something like this, it will work but won't be dynamic (It will clear the table but won't upload again). This is because of the way you have designed your app. I can write more once I get back from dinner but this will fix things in the short term

observeEvent(input$reset,{
    removeUI(selector = "#mytable")
  })
1 Like

Thanks a lot mate :slight_smile:

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