我有一个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)
第一部分询问一个阵容有多少个首发QB,RB等。从那里开始,我希望 "draft_pick_X "的selectInput选项显示的数量和上面所有输入的总和一样多。例如,如果你有6个总的名册位置,那么6个 "draft_pick_X "输入就会出现,以此类推。我所包含的是我第一次尝试为 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)