【问题标题】:Is it possible for reset (actionButton) and submitButton to work independently in Shiny app?重置 (actionButton) 和 submitButton 是否可以在 Shiny 应用程序中独立工作?
【发布时间】:2021-04-27 11:56:42
【问题描述】:

我的 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")
    })
    
  }
)

希望有人能赐教!

【问题讨论】:

  • 包含提交按钮的应用程序不会在输入更改时自动更新其输出,而是等到用户明确单击提交按钮。通常不鼓励使用submitButton,而是使用更通用的actionButton()
  • 谢谢I read this on the Shiny app site,但这正是我想要的;我想在点击更新之前选择几个过滤器,因为应用程序已经很慢了,所以我不希望它在每次选择过滤器时都做出反应。
  • 同意@YBS,尽可能避免submitButton

标签: r shiny shinydashboard shinyjs


【解决方案1】:

也许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)
      
    })
    
  }
)

【讨论】:

  • 感谢您的回答,但不幸的是,它不适用于我的应用程序...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-29
  • 2015-05-26
  • 2021-02-19
  • 2019-02-03
  • 2021-11-30
相关资源
最近更新 更多