conditionalPanel显示奇怪的行为

时间:2016-07-14 13:49:12

标签: r shiny

让我们考虑一些条件面板扩展的简单教程示例,因为我知道它们可以/应该使用

library(shiny)
library(shinydashboard)

ui <- fluidPage(
  titlePanel("censusVis"),

  sidebarLayout(
    sidebarPanel(
      helpText("Create demographic maps with 
               information from the 2010 US Census."),
      selectInput("var", 
                  label = "Choose a variable to display",
                  choices = list("Percent White" = 1,
                                 "Percent Black" = 2,
                                 "Percent Hispanic" = 3),
                  selected = 1
                  ),
      conditionalPanel(condition = "input.var == 1",
                       sliderInput("range", label = "Range of interest:", min = 0, max = 10, value = c(0, 10))
      ),
      conditionalPanel(condition = "input.var == 2",
                       sliderInput("range", label = "Range of interest:", min = 0, max = 20, value = c(0, 20))
      ),
      conditionalPanel(condition = "input.var == 3",
                       sliderInput("range", label = "Range of interest:", min = 0, max = 30, value = c(0, 30))
      )
    ),
    mainPanel(
      textOutput("text1"),
      textOutput("text2")
    )
  )
)
server <- function(input, output) { 
  output$text1 <- renderText({ 
    paste("You have selected", input$var)
  })

  output$text2 <- renderText({ 
    paste("You have chosen a range that goes from",
          input$range[1], "to", input$range[2])
  })


  }

shinyApp(ui, server)

正如预期的那样,当我移动滑块时,范围会发生变化。但是,如果我做出另一个选择然后移动滑块,则输出不会改变。我是否误解了conditionalPanel是某种if else if条件,或者我的示例应该如何更改?

我目前的解决方案是为每个滑块提供唯一的inputID,并在服务器端使用if else if条件w.r.t "var"。但是对于很多可能性来说这非常讨厌。

1 个答案:

答案 0 :(得分:0)

您可以通过添加~/anaconda2语句并在/usr/share/anaconda2/中为reactive分配ids来解决此问题:

sliderInput