在闪亮的应用程序中单击actionButton()后,将 Dataframe 重置为之前的状态

m4pnthwp  于 2022-12-25  发布在  其他
关注(0)|答案(1)|浏览(124)

我有下面的shiny应用程序,当用户开始在搜索textInput()中键入一个词时。然后用户按下搜索, Dataframe 将根据此搜索进行子集化。然后我希望在单击ResetactionButton()后重新设置表。

## 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)
a7qyws3x

a7qyws3x1#

我们可以使用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))
                
               })
  }

相关问题