【发布时间】: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$State和citystatedata$state。另外,假设city是变量名,请将unique(data_available)更改为unique(data_available$city);data_available是一个数据框。