将动态输入的默认值设置为不同的。

时间:2021-01-06 14:26:37

I am trying to run a shiny application that has a dynamic number of inputs. I achieved that based on previous stack overflow discussions, but now I would like to set it so that the starting value for each of the inputs is different.

我正在尝试运行一个具有动态数量输入的闪亮应用程序。我基于以前的栈溢出讨论实现了这一点,但是现在我想设置它,以便每个输入的起始值是不同的。

Selection of UI.R syntax:

选择的UI。R语法:

sidebarPanel(
selectInput("n", "Number of Test Scores Available", choices = c(1,2,3,4), selected = 1),
uiOutput("dyn_input")

Selection of server.R syntax:

选择服务器。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)
}) 

When the user selects multiple tests I would like the default value for Test2 to be FallORF, Test3 to be SpringNWF and Test4 to be SpringORF, right now they all default to FallNWF. Thanks for your help. If you need more information to help me feel free to ask.

当用户选择多个测试时,我希望Test2的默认值为FallORF, Test3为SpringNWF, Test4为SpringORF,现在它们都默认为FallNWF。谢谢你的帮助。如果你需要更多的信息来帮助我,请尽管问。

1 个解决方案

#1


3  

Looks good, you're almost there, you just need to set the selected attribute in the selectInput calls. Since you already have the index i of the default you want to select for each dynamic selectInput, you could achieve this for example like this (choices moved to a variable and choices[i] used for the selected attribute):

看起来不错,差不多了,只需在selectInput调用中设置所选属性。由于您已经有了要为每个动态selectInput选择的默认索引i,因此您可以实现如下目标(选项移动到一个变量,选项[i]用于选择的属性):

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)
}) 

#1


3  

Looks good, you're almost there, you just need to set the selected attribute in the selectInput calls. Since you already have the index i of the default you want to select for each dynamic selectInput, you could achieve this for example like this (choices moved to a variable and choices[i] used for the selected attribute):

看起来不错,差不多了,只需在selectInput调用中设置所选属性。由于您已经有了要为每个动态selectInput选择的默认索引i,因此您可以实现如下目标(选项移动到一个变量,选项[i]用于选择的属性):

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)
})