R Shiny Dynamic Input

前端 未结 1 1441
甜味超标
甜味超标 2020-12-03 19:51

I want to build an R shiny app that has a dynamic input that asks the user for a numeric input and then based on that input generates 4 more input fields. Here is what I ha

相关标签:
1条回答
  • 2020-12-03 20:33

    See a working example below

    library(shiny)
    
    ui <- shinyUI(fluidPage(
      titlePanel("Old Faithful Geyser Data"),
    
      sidebarLayout(
        sidebarPanel(
          numericInput("numInputs", "How many inputs do you want", 4),
          # place to hold dynamic inputs
          uiOutput("inputGroup")
        ),
        # this is just a demo to show the input values
        mainPanel(textOutput("inputValues"))
      )
    ))
    
    # Define server logic required to draw a histogram
    server <- shinyServer(function(input, output) {
      # observe changes in "numInputs", and create corresponding number of inputs
      observeEvent(input$numInputs, {
        output$inputGroup = renderUI({
          input_list <- lapply(1:input$numInputs, function(i) {
            # for each dynamically generated input, give a different name
            inputName <- paste("input", i, sep = "")
            numericInput(inputName, inputName, 1)
          })
          do.call(tagList, input_list)
        })
      })
    
      # this is just a demo to display all the input values
      output$inputValues <- renderText({
        paste(lapply(1:input$numInputs, function(i) {
          inputName <- paste("input", i, sep = "")
          input[[inputName]]
        }))
      })
    
    })
    
    # Run the application
    shinyApp(ui = ui, server = server)
    
    0 讨论(0)
提交回复
热议问题