英文:
How to use conditionPanel to update display different sliderInput in Shiny?
问题
我想使用ConditionalPanel根据用户的selectInput显示不同的sliderInput在Shiny中。但输出总是来自第一个sliderInput。
我如何实现它?
这是我的示例代码:
library(shiny)
ui <- fluidPage(
sidebarPanel(
selectInput("Type", "Type", c(A = "A", B = "B")),
conditionalPanel(
condition = "input.Type == 'A'",
sliderInput(inputId = "hi", label = "", min = 0, max = 10, value = 5, step = 1)
),
conditionalPanel(
condition = "input.Type == 'B'",
sliderInput(inputId = "hi", label = "", min = 10, max = 100, value = 50, step = 1)
),
),
mainPanel(
verbatimTextOutput(outputId = "text")
)
)
server <- function(input, output) {
output$text <- renderPrint({
input$hi
})
}
shinyApp(ui, server)
英文:
I want to use ConditionalPanel to display different sliderInput in Shiny according to user's selectInput.
But the output is always from the first sliderInput.
How can I achieve it?
Here is my example code:
library(shiny)
ui <- fluidPage(
sidebarPanel(
selectInput("Type", "Type", c(A = "A", B = "B")),
conditionalPanel(
condition = "input.Type == 'A'",
sliderInput(inputId = "hi", label = "", min = 0, max = 10, value = 5, step = 1)
),
conditionalPanel(
condition = "input.Type == 'B'",
sliderInput(inputId = "hi", label = "", min = 10, max = 100, value = 50, step = 1)
),
),
mainPanel(
verbatimTextOutput(outputId = "text")
)
)
server <- function(input, output) {
output$text <- renderPrint({
input$hi
})
}
shinyApp(ui, server)
答案1
得分: 1
问题在于inputId
必须是唯一的,即使你将其包装在conditionalPanel
中,你也创建了两个输入,这两个输入根据条件是可见还是不可见。
因此,为了使你的代码工作,使用唯一的ID,并在服务器端使用例如switch
来有条件地显示可见滑块的值:
library(shiny)
ui <- fluidPage(
sidebarPanel(
selectInput("Type", "Type", c(A = "A", B = "B")),
conditionalPanel(
condition = "input.Type == 'A'",
sliderInput(inputId = "hiA", label = "", min = 0, max = 10, value = 5, step = 1)
),
conditionalPanel(
condition = "input.Type == 'B'",
sliderInput(inputId = "hiB", label = "", min = 10, max = 100, value = 50, step = 1)
),
),
mainPanel(
verbatimTextOutput(outputId = "text")
)
)
server <- function(input, output) {
output$text <- renderPrint({
switch(input$Type,
A = input$hiA,
B = input$hiB
)
})
}
shinyApp(ui, server)
或者作为第二个选项,使用一个sliderInput
,您可以使用observe
和updateSelectInput
有条件地更新用户的选择:
library(shiny)
ui <- fluidPage(
sidebarPanel(
selectInput("Type", "Type", c(A = "A", B = "B")),
sliderInput(inputId = "hi", label = "", min = 0, max = 10, value = 5, step = 1)
),
mainPanel(
verbatimTextOutput(outputId = "text")
)
)
server <- function(input, output) {
observe({
switch(input$Type,
A = updateSliderInput(inputId = "hi", min = 0, max = 10, value = 5),
B = updateSliderInput(inputId = "hi", min = 10, max = 100, value = 50)
)
})
output$text <- renderPrint({
input$hi
})
}
shinyApp(ui, server)
英文:
The issue is that inputId
s have to be unique, i.e. even if you wrap inside conditionalPanel
you are creating two inputs, which depending on the condition are visible or not.
Hence, to make your code work use unique IDs and take care of that in the server using e.g. a switch
to conditionally display the value of the visible slider:
library(shiny)
ui <- fluidPage(
sidebarPanel(
selectInput("Type", "Type", c(A = "A", B = "B")),
conditionalPanel(
condition = "input.Type == 'A'",
sliderInput(inputId = "hiA", label = "", min = 0, max = 10, value = 5, step = 1)
),
conditionalPanel(
condition = "input.Type == 'B'",
sliderInput(inputId = "hiB", label = "", min = 10, max = 100, value = 50, step = 1)
),
),
mainPanel(
verbatimTextOutput(outputId = "text")
)
)
server <- function(input, output) {
output$text <- renderPrint({
switch(input$Type,
A = input$hiA,
B = input$hiB
)
})
}
shinyApp(ui, server)
Or as a second option use one sliderInput
which you could update conditionally on the user's choice using an observe
er and updateSelectInput
.
library(shiny)
ui <- fluidPage(
sidebarPanel(
selectInput("Type", "Type", c(A = "A", B = "B")),
sliderInput(inputId = "hi", label = "", min = 0, max = 10, value = 5, step = 1)
),
mainPanel(
verbatimTextOutput(outputId = "text")
)
)
server <- function(input, output) {
observe({
switch(input$Type,
A = updateSliderInput(inputId = "hi", min = 0, max = 10, value = 5),
B = updateSliderInput(inputId = "hi", min = 10, max = 100, value = 50)
)
})
output$text <- renderPrint({
input$hi
})
}
shinyApp(ui, server)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论