Can I have multiple submit buttons in R shiny?

2019-08-01 06:38发布

问题:

In my R shiny application, I would like to have one button to submit one set of inputs (which affect one portion of the output) and another one to submit the remaining inputs (which affect a different portion of the output). The code in the widgets example of the Shiny tutorial uses a submitButton but it seems like all the inputs are delivered when that single button is pressed? Thanks in advance for your help.

回答1:

Here is an example showing actionButtons controlling reactive components:

library(shiny)
runApp(list(
  ui = fluidPage(
    titlePanel("Hello Shiny!"),
    sidebarLayout(
      sidebarPanel(
        tags$form(
          numericInput('n', 'Number of obs', 100)
          , br()
          , actionButton("button1", "Action 1")
        )
        , tags$form(
          textInput("text", "enter some text", value= "some text")
          , br()
          , actionButton("button2", "Action 2")
        )
      ),
      mainPanel(
        plotOutput('plot')
        , textOutput("stext")
      )
    )
  ),
  server = function(input, output) {
    output$plot <- renderPlot({ 
      input$button1
      hist(runif(isolate(input$n))) 
    })
    output$stext <- renderText({
      input$button2
      isolate(input$text )
    })
  }
)
)