0
votes

I have a Shiny app that doesn't give any error but clearly my conditionalPanel are not working properly. When I select the inputs some graphs get updated and some don't. For example if I select week and change the condition for 0 or 1 the graphs get updated, but if I select rel the graph gets update for 1 but not for 4 (If I do this outside the Shiny app it works for all cases). This is how the code looks like: 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())
})

 })

Any suggestions? Thanks!

2

2 Answers

1
votes

Instead of having multiple selectinputs, you can just have one and update the choices using updateSelectInput(session,"levels",choices = newValue)) You can use an observer to change the choices of input$levels each time a input$zcol changed.

Here is basic example of how to update a selectinput using an observer

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)
            }
        })

    }
))
1
votes

You can't have multiple selectInputs with the same inputId ("levels"). Each one needs a unique inputId.