Producing dynamic/multiple input boxes to collect data depending on user selection in Shiny R

后端 未结 1 566
抹茶落季
抹茶落季 2021-01-28 23:50

I am trying to build an app in Shiny that (1) asks user for number of Assets in his/her portfolio. Depending on the numeric entry, (2) the user is presented with one numeric bo

1条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-29 00:14

    ui.R

    library(shiny)
    
    shinyUI(pageWithSidebar (
    
      headerPanel( "Portfolio Returns"),
    
      sidebarPanel(
        numericInput("assets", label = "Enter Number of Assets in Portfolio", value="1"),
        uiOutput("tickers")
      ),
      mainPanel()
    )) 
    

    server.R
    -Note that to pass multiple items in the renderUI() function you have to group them into a list, and here lapply() is creating a list of lists.

    library(shiny)
    
    shinyServer( function(input, output, session) {
    
      output$tickers <- renderUI({
        numAssets <- as.integer(input$assets)
    
        lapply(1:numAssets, function(i) {
          list(tags$p(tags$u(h4(paste0("Asset ", i)))),
               textInput(paste0("ticker", i), label = "Ticker Name", value = "Enter ticker..."),
               numericInput(paste0("weight", i), label = "Weight of Asset", value=0))  
        })
      })
    })  
    

    0 讨论(0)
提交回复
热议问题