Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
584 views
in Technique[技术] by (71.8m points)

r - Is it possible for reset (actionButton) and submitButton to work independently in Shiny app?

I have a reset (actionButton) and update button (submitButton) in my Shiny app. The problem is that to reset the app, I have to click on the reset button followed by the update button. Is it possible to reset the app without having to click on update?

EDIT: I do want the app to update only after the user explicitly clicks update. This is because in my app they will have the option to select several selectors to filter the data. Happy to use something else other than submitbutton, but so far this has been the only function that worked for the purpose.

In the example below, I have to click on update twice to get the whole app to reset :

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

I hope someone can enlighten me!

question from:https://stackoverflow.com/questions/65854940/is-it-possible-for-reset-actionbutton-and-submitbutton-to-work-independently-i

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Perhaps actionButton in combination with updateNumericInput() will meet your needs. Try this

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...