我正在尝试运行具有动态输入数量的闪亮应用程序。我基于之前的堆栈溢出讨论实现了这一点,但现在我想设置它以使每个输入的起始值不同。
选择UI.R语法:
sidebarPanel(
selectInput("n", "Number of Test Scores Available", choices = c(1,2,3,4), selected = 1),
uiOutput("dyn_input")
选择server.R语法:
output$dyn_input <- renderUI({
inputs <- lapply(1:input$n, function(i) {
input_name <- paste("Test", i, sep="")
input_score <- paste("Score", i, sep="")
wellPanel(
selectInput(
input= input_name,
label= "Test Name", choices=c("Fall NWF"="FallNWF",
"Fall ORF"="FallORF",
"Spring NWF"="SpringNWF",
"Spring ORF"="SpringORF"
)),
sliderInput(input_score,"Score:",
min=0, max=250,value=0))
})
do.call(tagList, inputs)
})
当用户选择多个测试时,我希望Test2的默认值为FallORF,Test3为SpringNWF,Test4为SpringORF,现在它们都默认为FallNWF。谢谢你的帮助。如果您需要更多信息,请随时提出。
答案 0 :(得分:3)
看起来不错,你几乎就在那里,你只需要在selected
电话中设置selectInput
属性。由于您已经为每个动态i
选择了默认索引selectInput
,因此您可以实现此目的,例如choices
移动到变量choices[i]
用于selected
属性):
output$dyn_input <- renderUI({
inputs <- lapply(1:input$n, function(i) {
input_name <- paste("Test", i, sep="")
input_score <- paste("Score", i, sep="")
choices <- c("Fall NWF"="FallNWF",
"Fall ORF"="FallORF",
"Spring NWF"="SpringNWF",
"Spring ORF"="SpringORF")
wellPanel(
selectInput(
input= input_name,
label= "Test Name", choices=choices, selected = choices[i]),
sliderInput(input_score,"Score:",
min=0, max=250,value=0))
})
do.call(tagList, inputs)
})