r - 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 工作,你可以选择任何你想要的状态。但是第二个选择器是空白的!为什么。我的观察功能有问题,但我一直坐在这里,没有可能的解决方案。如果可以的话请帮忙!谢谢!
解决方案
天哪,我是愚蠢的。
这是因为我在观察事件中将输入命名错误。
让它工作。回答:
observe({
updateSelectInput(session, "selectcounty", "Select County",
choices = counties$NAMELSAD[counties$STATE_NAME==input$selectstate])
})
推荐阅读
- javascript - 如果我在 index.html 的脚本标记中使用异步,我是否需要费心在 scripts.js 中编写异步代码?我要javascript
- typescript - 如何使用 Firebase 函数运行 PostgreSQL 服务器?
- beautifulsoup - 如何 Web 抓取 SEC Edgar 10-K 动态数据
- ruby-on-rails - 即使提供记录 ID,也无法更新嵌套属性
- authentication - 如果未登录,重定向用户时如何在 URL 中发送目标参数?
- selenium - 夜班 - Saucelabs 问题
- flutter - 显示特定的星期并编辑列表中的一行
- python - 多任务分类CNN
- python - 有没有办法用python中的POST数据修复这个错误?
- javascript - 对同一个补丁请求端点使用 stubby 多个响应