尝试根据其他输入的总和在 R Shiny 应用程序中创建动态 UI

Trying to create a dynamic UI in an R Shiny app based on the sum of other inputs

我有一个 R Shiny 应用程序,我试图用它创建一个基于其他输入总和的动态 UI。请参阅下面我的最小代表:

    library(shiny)
    library(tidyverse)

    ui <- fluidPage(
        sidebarLayout(
            sidebarPanel(
                numericInput(inputId = "starting_qbs",
                             label = "Number of starting QBs",
                             value = 1,
                             min = 0,
                             max = 6),
                numericInput(inputId = "starting_rbs",
                             label = "Number of starting RBs",
                             value = 2,
                             min = 0,
                             max = 6),
                numericInput(inputId = "starting_wrs",
                             label = "Number of starting WRs",
                             value = 3,
                             min = 0,
                             max = 6),
                numericInput(inputId = "starting_tes",
                             label = "Number of starting TEs",
                             value = 1,
                             min = 0,
                             max = 6),
                numericInput(inputId = "starting_flex",
                             label = "Number of starting flex spots",
                             value = 1,
                             min = 0,
                             max = 6),
                conditionalPanel(
                    condition = "sum(input.starting_qbs, 
                                     input.starting_rbs, 
                                     input.starting_wrs,
                                     input.starting_tes,
                                     input.starting_flex) >= 1", 
                    selectInput("draft_pick_1",
                                "Round 1 Selection",
                                c("Select player" ="", projections$player))),

                selectInput("draft_pick_2",
                            "Round 2 Selection",
                            c("Select player" ="", projections$player)),
                selectInput("draft_pick_3",
                            "Round 3 Selection",
                            c("Select player" ="", projections$player)),
                selectInput("draft_pick_4",
                            "Round 4 Selection",
                            c("Select player" ="", projections$player)),
                selectInput("draft_pick_5",
                            "Round 5 Selection",
                            c("Select player" ="", projections$player)),
                selectInput("draft_pick_6",
                            "Round 6 Selection",
                            c("Select player" ="", projections$player)),
                selectInput("draft_pick_7",
                            "Round 7 Selection",
                            c("Select player" ="", projections$player))
            ),
            mainPanel(
                plotOutput("draftPlot"))
        )
    )

server <- function(input, output) {
    renderPrint("This is text")
}

shinyApp(ui = ui, server = server)

第一部分询问一个阵容有多少首发四分卫、边卫等。从那里,我希望显示的 "draft_pick_X" selectInput 选项与上面所有输入的总和一样多。例如,如果您总共有 6 个花名册名额,那么 "draft_pick_X" 输入中的 6 个将出现,依此类推。我包含的内容是我第一次尝试为 draft_pick_1 执行此操作,但没有成功。知道如何让它工作吗?

uiOutput 与服务器中的渲染输入一起使用是可行的方法。

library(shiny)
library(tidyverse)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      numericInput(inputId = "starting_qbs",
                   label = "Number of starting QBs",
                   value = 1,
                   min = 0,
                   max = 6),
      numericInput(inputId = "starting_rbs",
                   label = "Number of starting RBs",
                   value = 2,
                   min = 0,
                   max = 6),
      numericInput(inputId = "starting_wrs",
                   label = "Number of starting WRs",
                   value = 3,
                   min = 0,
                   max = 6),
      numericInput(inputId = "starting_tes",
                   label = "Number of starting TEs",
                   value = 1,
                   min = 0,
                   max = 6),
      numericInput(inputId = "starting_flex",
                   label = "Number of starting flex spots",
                   value = 1,
                   min = 0,
                   max = 6),
      uiOutput('draftUI'),
    ),
    mainPanel(
      textOutput("players")
      )
  )
)

server <- function(input, output) {
  total <- reactive(input$starting_qbs + input$starting_rbs + input$starting_wrs + input$starting_tes + input$starting_flex)
  output$draftUI <- renderUI({
    count <- total()
    list <- seq(count)
    list <- lapply(list, function(x) selectInput(paste0("draft_pick_",x),
                                                 paste0("Round ", x, " Selection"),
                                                 c("Select player" ="", projections$player)))


    tagList(list)
  })

  drafted_players <- reactive({

    # If you did not make the total() reactive for some reason you can use this method to get any arbitrary number of inputs that match a pattern
    # names(input)[grepl('draft_pick_',names(input), fixed = T)] %>% sort() %>% sapply(function(x) input[[x]])

   paste0('draft_pick_',1:total()) %>% sapply(function(x) input[[x]])

  })

  output$players <- renderText({
    drafted_players()
  })

}

shinyApp(ui = ui, server = server)