重置 (actionButton) 和 submitButton 是否可以在 Shiny 应用程序中独立工作?

问题描述

我的 Shiny 应用中有一个重置 (actionButton) 和更新按钮 (submitButton)。问题是要重置应用程序,我必须点击 reset 按钮,然后点击 update 按钮。是否可以在无需点击更新的情况下重置应用?

编辑:我确实希望应用程序仅在用户明确单击更新后更新。这是因为在我的应用程序中,他们可以选择多个选择器来过滤数据。很高兴使用除 submitbutton 以外的其他功能,但到目前为止,这是唯一可以实现此目的的功能

在下面的示例中,我必须点击 update 两次 才能重置整个应用程序:

library(shiny)
shinyApp(
  ui = basicPage(
    numericInput("num",label = "Make changes",value = 1),submitButton("Update",icon("refresh")),shinyjs::useShinyjs(),actionButton("reset","Reset"),helpText(
      "When you click the button above,you should see","the output below update to reflect the value you","entered at the top:"
    ),verbatimtextoutput("value")
  ),server = function(input,output) {
    # submit buttons do not have a value of their own,# they control when the app accesses values of other widgets.
    # input$num is the value of the number widget.
    output$value <- renderPrint({
      input$num
    })
    
    observeEvent(input$reset,{
      shinyjs::reset("num")
    })
    
  }
)

希望有人能赐教!

解决方法

也许 actionButtonupdateNumericInput() 组合可以满足您的需求。试试这个

library(shiny)
shinyApp(
  ui = basicPage(
    numericInput("num",label = "Make changes",value = 1),actionButton("Update","refresh"),shinyjs::useShinyjs(),actionButton("reset","Reset"),helpText(
      "When you click the button above,you should see","the output below update to reflect the value you","entered at the top:"
    ),verbatimTextOutput("value")
  ),server = function(input,output,session) {
    
    # submit buttons do not have a value of their own,# they control when the app accesses values of other widgets.
    # input$num is the value of the number widget. 
    
    
    observeEvent(input$Update,{
      output$value <- renderPrint({
        isolate(input$num)
      })
    })
    
    
    observeEvent(input$reset,{
      #shinyjs::reset("num")
      updateNumericInput(session,"num",value=1)
      
    })
    
  }
)