闪亮的条件面板更新问题

shiny conditionalPanel update problems

我有一个 Shiny 应用程序,它没有给出任何错误,但显然我的 conditionalPanel 无法正常工作。当我 select 输入时,一些图表会更新,而有些则不会。例如,如果我 select 周并更改 0 或 1 的条件,则图表会更新,但如果我 select rel 图表会更新 1 而不是 4(如果我在 Shiny 之外执行此操作适用于所有情况的应用程序)。这是代码的样子: UI.R

shinyUI(pageWithSidebar(
headerPanel(' '),
sidebarPanel(
selectInput('zcol', 'Variable to be fixed',  names(taxi[,-c(1,4,5,7,8,9,10,11)])),
conditionalPanel(condition = "input.zcol == 'week'",
                 selectInput("levels", "Levels",c(0,1)
                 )),
conditionalPanel(condition = "input.zcol == 'tollfree'",
                 selectInput("levels", "Levels",c(0,1)
                 )),
conditionalPanel(condition = "input.zcol == 'rel'",
                 selectInput("levels", "Levels",c(1,4)
                 )),
conditionalPanel(condition = "input.zcol == 'source'",
                 selectInput("levels", "Levels",c(1,2)
                 )),
conditionalPanel(condition = "input.zcol == 'hour'",
                 selectInput("levels", "Levels",c(seq(0,23))
                 ))
),
mainPanel(
plotOutput('plot1'),
plotOutput('plot2')
)
))

Server.R

shinyServer(function(input, output, session) {

simiData <- reactive({ 
 eval(substitute(taxi %>%  group_by(simi.mean,col) %>% summarise(mean = mean(prop.conv)) %>% 
                                  filter(col==input$levels) %>% select(simi.mean,mean), 
                                list(col=as.symbol(input$zcol))))
  })

 distData <- reactive({ 
eval(substitute(taxi %>%  group_by(dist.mean,col) %>% summarise(mean = mean(prop.conv)) %>% 
                  filter(col==input$levels) %>% select(dist.mean,mean), 
                list(col=as.symbol(input$zcol))))
})

output$plot1 <- renderPlot({
 plot(simiData(),xlim=c(0,max(simiData()$simi.mean)),ylim=c(0,max(simiData()$mean)))
})

output$plot2 <- renderPlot({
plot(distData())
})

 })

有什么建议吗? 谢谢!

不能有多个具有相同 inputId ("levels") 的 selectInputs。每个都需要一个唯一的 inputId。

您可以只拥有一个并使用 updateSelectInput(session,"levels",choices = newValue)) 更新选择,而不是使用多个选择输入 您可以使用观察者在每次 input$zcol 更改时更改 input$levels 的选择.

这是如何使用观察者更新选择输入的基本示例

taxi <- data.frame(week=sample(1:10),hour=sample(1:10))
runApp(list(
  ui = shinyUI(
        fluidPage(
            sidebarLayout(
                sidebarPanel(
                    selectInput("zcol", 'Variable to be fixed',  names(taxi)),
                    selectInput("levels", "Levels",1:5)
                ),
                mainPanel(
                    plotOutput('plot1')
                )
            )
        )
  ),

    server = function(input, output, session) {

        output$plot1 <- renderPlot({
            plot(taxi[1:input$levels,])
        })

        observe({
            if ( input$zcol == 'week') {
                updateSelectInput(session, "levels", choices = 1:5)
            } else if(input$zcol == 'hour') {
                updateSelectInput(session, "levels", choices = 6:10)
            }
        })

    }
))