1
votes

I'm trying to create a dynamic dropdown selection where the lower lists are determined by the higher lists. In the below example, I can't manage to change the middle list, it always jumps back to the first option. Can you help me figure this out?

library(shiny)
library(dplyr)

# Data input and processing
dat <- data.frame(Region=c(rep("Reg 1", 6),
                            rep("Reg 2", 6),
                            rep("Reg 3", 6)),
                   Province=paste("Prov", c(rep("A", 2), "B", rep("C",3),"D",rep("E",4),"F",rep("G",3),rep("H",3))),
                   District=paste("Dist", letters[1:18]))


# Define UI 
ui <- fluidPage(

    verticalLayout(
        selectInput("region", "Select region",
                    choices = dat %>% group_by(Region) %>% summarize()
                    ), 
        selectInput("province", "Select province or indie", choices = NULL),
        selectInput("district", "Select this", choices = NULL)
    )
)

# Define server logic 
server <- function(input, output, session) {

    observe({
        y <- input$region

        regsub <- dat %>% filter(Region == y) %>% group_by(Province) %>% summarize()

        zee <- input$province

        provsub <- dat %>% filter(Province == zee) %>% group_by(District) %>% summarize()

        updateSelectInput(session, "province", "Select province or independent city", choices = regsub)


        updateSelectInput(session, "district", "Select municipality or district", choices = provsub)


    })
}

# Run the application 
shinyApp(ui = ui, server = server)
> sessionInfo()
R version 3.6.0 (2019-04-26)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17134)

other attached packages:
[1] dplyr_0.8.1 shiny_1.3.2
1

1 Answers

1
votes

You need to split your observe code, to watch the first and the second drop down seperately. Because changing province selection will reload the province list.

library(shiny)
library(dplyr)

# Data input and processing
dat <- data.frame(Region=c(rep("Reg 1", 6),
                           rep("Reg 2", 6),
                           rep("Reg 3", 6)),
                  Province=paste("Prov", c(rep("A", 2), "B", rep("C",3),"D",rep("E",4),"F",rep("G",3),rep("H",3))),
                  District=paste("Dist", letters[1:18]))


# Define UI 
ui <- fluidPage(

  verticalLayout(
    selectInput("region", "Select region",
                choices = dat %>% group_by(Region) %>% summarize()
    ), 
    selectInput("province", "Select province or indie", choices = NULL),
    selectInput("district", "Select this", choices = NULL)
  )
)

# Define server logic 
server <- function(input, output, session) {

  observe({
    y <- input$region

    regsub <- dat %>% filter(Region == y) %>% group_by(Province) %>% summarize()


    updateSelectInput(session, "province", "Select province or independent city", choices = regsub)




  })
  observe({
  zee <- input$province

  provsub <- dat %>% filter(Province == zee) %>% group_by(District) %>% summarize()

  updateSelectInput(session, "district", "Select municipality or district", choices = provsub)

  })
}

# Run the application 
shinyApp(ui = ui, server = server)