如何使用`conditionPanel`在Shiny中更新显示不同的`sliderInput`?

huangapple go评论75阅读模式
英文:

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 &lt;- fluidPage(
  sidebarPanel(
    selectInput(&quot;Type&quot;, &quot;Type&quot;, c(A = &quot;A&quot;, B = &quot;B&quot;)),
    conditionalPanel(
      condition = &quot;input.Type == &#39;A&#39;&quot;,
      sliderInput(inputId = &quot;hi&quot;, label = &quot;&quot;, min = 0, max = 10, value = 5, step = 1)
    ),
    conditionalPanel(
      condition = &quot;input.Type == &#39;B&#39;&quot;,
      sliderInput(inputId = &quot;hi&quot;, label = &quot;&quot;, min = 10, max = 100, value = 50, step = 1)
    ),
  ),
  mainPanel(
    verbatimTextOutput(outputId = &quot;text&quot;)
  )
)

server &lt;- function(input, output) {
  output$text &lt;- 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,您可以使用observeupdateSelectInput有条件地更新用户的选择:

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 inputIds 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 &lt;- fluidPage(
  sidebarPanel(
    selectInput(&quot;Type&quot;, &quot;Type&quot;, c(A = &quot;A&quot;, B = &quot;B&quot;)),
    conditionalPanel(
      condition = &quot;input.Type == &#39;A&#39;&quot;,
      sliderInput(inputId = &quot;hiA&quot;, label = &quot;&quot;, min = 0, max = 10, value = 5, step = 1)
    ),
    conditionalPanel(
      condition = &quot;input.Type == &#39;B&#39;&quot;,
      sliderInput(inputId = &quot;hiB&quot;, label = &quot;&quot;, min = 10, max = 100, value = 50, step = 1)
    ),
  ),
  mainPanel(
    verbatimTextOutput(outputId = &quot;text&quot;)
  )
)

server &lt;- function(input, output) {
  output$text &lt;- 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 observeer and updateSelectInput.

library(shiny)
ui &lt;- fluidPage(
  sidebarPanel(
    selectInput(&quot;Type&quot;, &quot;Type&quot;, c(A = &quot;A&quot;, B = &quot;B&quot;)),
    sliderInput(inputId = &quot;hi&quot;, label = &quot;&quot;, min = 0, max = 10, value = 5, step = 1)
  ),
  mainPanel(
    verbatimTextOutput(outputId = &quot;text&quot;)
  )
)

server &lt;- function(input, output) {
  observe({
    switch(input$Type,
      A = updateSliderInput(inputId = &quot;hi&quot;, min = 0, max = 10, value = 5),
      B = updateSliderInput(inputId = &quot;hi&quot;, min = 10, max = 100, value = 50)
    )
  })
  output$text &lt;- renderPrint({
    input$hi
  })
}

shinyApp(ui, server)

huangapple
  • 本文由 发表于 2023年5月28日 18:21:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76351010.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定