【问题标题】:shiny conditionalPanel update problems闪亮的条件面板更新问题
【发布时间】:2015-09-17 14:34:56
【问题描述】:

我有一个闪亮的应用程序,它没有给出任何错误,但显然我的条件面板无法正常工作。当我选择输入时,有些图形会更新,有些则不会。例如,如果我选择 week 并将条件更改为 0 或 1,则图表会更新,但如果我选择 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')
)
))

服务器.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())
})

 })

有什么建议吗? 谢谢!

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    您不能有多个具有相同 inputId(“级别”)的 selectInput。每个人都需要一个唯一的 inputId。

    【讨论】:

    • 我选择相同的 inputId 名称的原因之一是在构建数据框时允许我进行一般参考:filter(col==input$levels)。由于我不允许这样做,您是否知道任何其他功能可以做到这一点,而不是为每个 inputId 构建一个表?
    【解决方案2】:

    您可以只拥有一个并使用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)
                }
            })
    
        }
    ))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-29
      • 2016-04-10
      • 2021-04-06
      • 2017-04-23
      • 1970-01-01
      • 1970-01-01
      • 2014-03-03
      • 1970-01-01
      相关资源
      最近更新 更多