【问题标题】:Need help making dependent dropdown boxes in the RStudio package Shiny需要帮助在 RStudio 包中制作依赖下拉框 Shiny
【发布时间】:2020-12-19 23:09:02
【问题描述】:

我有两个数据集,一个包含 200 个城市及其相应州的列表,另一个更大的数据集,我想制作一个应用程序进行排序。我需要帮助在我闪亮的应用程序中制作两个下拉框,其中第一个是状态变量,第二个是所选州内的城市列表。然后,我希望这些选择能够过滤输出中更大的第二个数据集。我已经尝试了几个类似但略有不同的在线示例的解决方案,但我无法将其转化为我正在做的事情。

到目前为止,我有这个:

ui <- fluidPage(
    headerPanel(''),
    sidebarPanel(
            #add selectinput boxs
      htmlOutput("state_selector"),
      htmlOutput("city_selector"),
    ),
    mainPanel(
        fluidRow(
        # Create a new row for the table.
        DT::dataTableOutput("table")
    )

 server <- function(session, input, output) {
    
    output$state_selector = renderUI({
      selectInput("state", label = h4("State"), 
                  choices = as.character(unique(citystatedata$state)), selected = NULL)
    })
    
    output$city_selector = renderUI({
      
      data_available = citystatedata[citystatedata$State == input$state, "state"]
    
      selectInput(inputId = "city", #name of input
                  label = "City", #label displayed in ui
                  choices = unique(data_available), #calls list of available cities
                  selected = unique(data_available)[1])
    })

shinyApp(ui = ui, server = server)

我试图删除与下拉框无关的代码部分,因为这是我更具体地询问的内容。所以如果我遗漏了什么,我很抱歉!让我知道是否需要添加其他内容

【问题讨论】:

  • renderUI 仅在选择依赖于用户所做的其他事情时使用。这里不需要output$state_selector,因为不依赖于另一个输入。相反,将 selectInput("state", ...) 直接移至 UI 部分,替换 htmlOutput("state_selector"),。如果这不能解决问题,请提供citystatedata 的示例,以便您的问题可以重现。
  • 你的变量名是state还是State?。您必须在服务器的两个地方都使用相同的。目前您正在使用citystatedata$Statecitystatedata$state。另外,假设city 是变量名,请将unique(data_available) 更改为unique(data_available$city)data_available 是一个数据框。

标签: r shiny


【解决方案1】:

使用可用的gapminder 数据,你可以试试这个。

df <- gapminder
df$state <- gapminder$continent
df$city <- gapminder$country
citystatedata <- df

ui <- fluidPage(
  headerPanel('Test'),
  sidebarPanel(
    #add selectinput boxs
    uiOutput("state_selector"),
    uiOutput("city_selector"),
  ),
  mainPanel(
    fluidRow(
      # Create a new row for the table.
      DTOutput("table")
    )
  )
)

server <- function(session, input, output) {
  
  output$state_selector = renderUI({
    selectInput("state", label = h4("State"), 
                choices = as.character(unique(citystatedata$state)), selected = NULL)
  })
  
  output$city_selector = renderUI({
    
    data_available = citystatedata[citystatedata$state == req(input$state),]
    
    selectInput(inputId = "city", #name of input
                label = "City", #label displayed in ui
                choices = unique(data_available$city), #calls list of available cities
                selected = 1)
  })
  
  mydt <- reactive({
    citystatedata %>% filter(citystatedata$state == req(input$state) &  citystatedata$city %in% req(input$city))
  })
  
  output$table <- renderDT(mydt())
  
}

shinyApp(ui = ui, server = server)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-09
    • 1970-01-01
    • 2011-12-18
    • 2020-06-10
    • 2013-03-25
    • 2018-09-17
    相关资源
    最近更新 更多