首页 > 解决方案 > R Shiny - 我的观察函数({UpdateSelectInput})有什么问题?

问题描述

我已经针对我的问题查看了类似的帖子,但没有任何帮助,而且我已经没有耐心了。

我有一个非常简单的应用程序,我试图在其中找到选定的美国州内的所有县。我在 UI 中使用 SelectInput 让用户选择他们想要的州,并在服务器中使用 UpdateSelectInput 以便用户可以从他们的州选择中选择他们想要的县。

这是我的简化数据的样子:

**STATE_NAME      NAMELSAD**
Alabama            Clay County  
Alabama            Marengo County
Arkansas           Scott County

我的代码如下所示:

全球.r

library(shiny)
library(leaflet)
library(sp)
library(rgdal)
library(htmltools)
library(DT)
library(ggplot2)
library(shinythemes)

path <- "C:/Users/user/Desktop/Countyapp/Countyapp/test_covid/"
setwd(path)

counties <- read.csv("us_counties1.csv")

用户界面.r

ui <- fluidPage(
        selectInput(inputId = "selectstate", label = "Select State", choices = (counties$STATE_NAME)),
        selectInput(inputId = "selectcounty", label =  "Select County", choices = NULL)
)

最后,server.R

server <- function(session, input, output) {

    observe({
        updateSelectInput(session, "selectcounty", "Select County", 
                          choices = counties$NAMELSAD[counties$STATE_NAME==input$STATE_NAME])
    })

}

shinyApp(ui = ui, server = [enter image description here][1]server) 

基本上,我的第一个 SelectInput 工作,你可以选择任何你想要的状态。但是第二个选择器是空白的!为什么。我的观察功能有问题,但我一直坐在这里,没有可能的解决方案。如果可以的话请帮忙!谢谢!

标签: rshinyselectinput

解决方案


天哪,我是愚蠢的。

这是因为我在观察事件中将输入命名错误。

让它工作。回答:

  observe({
        updateSelectInput(session, "selectcounty", "Select County", 
                          choices = counties$NAMELSAD[counties$STATE_NAME==input$selectstate])
    })

推荐阅读