Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

r - Is is 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? 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!


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

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...