Home > Mobile >  Reset a dataframe to previous condition after clicking an actionButton() in a shiny app
Reset a dataframe to previous condition after clicking an actionButton() in a shiny app

Time:12-22

I have the shiny app below in which when the user starts typing a word in search textInput(). Then the user press Search and the dataframe is subseted according to this search. Then I would like the table to be reseted after clicking the Reset actionButton().

## app.R ##
library(shiny)
library(shinydashboard)
library(DT)
col1<-c("sd fgg","df dfg","fgh gdfg")
col2<-c("sd fgg","df dgfg","fgh gdfg")
col3<-c("sd fggg","dfg dfgg","fgghol gdfg")
df<-data.frame(col1,col2,col3)


ui <- dashboardPage(
  dashboardHeader(title = "Dataset Inventory"),
  dashboardSidebar(
    
    textInput("tt","search",""),
    actionButton("ser","Search"),
    actionButton("res","Reset")
    
  ),
  dashboardBody(
    dataTableOutput("table")
  )
)

server <- function(input, output) {
  
  output$table<-renderDataTable({
  input$res
    datatable(
      d_new <- df[apply(df, 1, function(x) any(grepl(isolate(input$tt), x))), ]
      
    )
  })
}

shinyApp(ui, server)

CodePudding user response:

We may use observeEvent

server <- function(input, output, session) {
  cntrl <- reactiveValues(n = 0)
  output$table <- renderDataTable({
      datatable(df)
    
  })
  observeEvent(input$ser,
               {
                 cntrl$n <- cntrl$n   1
                 output$table<-renderDataTable({
                   datatable(
                     d_new <- df[apply(df, 1, function(x) any(grepl(isolate(input$tt), x))), ]
                     
                   )
                 })
               }
               
               )
  observeEvent(input$res,
               {
                 cntrl$n <- 0
                 updateTextInput(session, "tt", value = "")
                  output$table <- renderDataTable(datatable(df))
                
               })
  }
  • Related