简体   繁体   English

Shiny中的动态下拉选择

[英]Dynamic drop-down selection in Shiny

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

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM