r - R Shiny updateSelectizeInput 抛出错误并且不更新
问题描述
我正在尝试创建两个闪亮的下拉菜单 - 一个是州列表,然后过滤第二个下拉列表,其中列出了所选州的所有县。以下代码有效,但是当您更改状态时县不会更新。
用户界面
pageWithSidebar(
headerPanel("My chart"),
sidebarPanel(
uiOutput("sel_state"),
uiOutput("sel_county")
),
mainPanel(
tableOutput("table")
)
)
服务器
function(input, output, session) {
output$sel_state <- renderUI({
selectizeInput('state', 'Select a State', choices=c("Choose One" = "", state_list))
})
output$sel_county <- renderUI({
county_list <- reactive({
df %>%
filter(state == input$state) %>%
pull(county) %>%
sort() %>%
as.character()
})
selectizeInput('county', 'Select a County', choices=c("Choose One" = "", county_list()))
})
tab <- reactive({
df %>%
filter(state == input$state) %>%
filter(county == input$county)
})
output$table <- renderTable({
tab()
})
}
我试图用以下“updateSelectizeInput”替换县下拉列表中的“selectizeInput”,但不断收到以下错误
新服务器线
updateSelectizeInput(session, 'county', 'Select a County', choices=c("Choose One" = "", county_list()))
错误:无法将类型“环境”强制转换为“字符”类型的向量
创建数据框的代码 - 县列表还不是正确的列表
x <- getURL("https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-counties.csv")
csv <- read.csv(text=x)
df <- as.data.frame(csv)
state_list <- c(levels(df$state))
county_list <- subset(df, select = c(3,2))
输入输出
dput(head(cl))
structure(list(state = c("Washington", "Washington", "Washington",
"Illinois", "Washington", "California"), county = c("Snohomish",
"Snohomish", "Snohomish", "Cook", "Snohomish", "Orange")), row.names =
c(NA, 6L), class = "data.frame")
解决方案
我没有您的州/县信息(或其结构),但是这个小示例完成了我认为您所描述的内容。注意事项:
您需要首先使用 selectizeInput 制作选择输入 UI 元素,然后您可以使用 updateSelectizeInput 更新其内容。所以你需要这两个函数,但是对于我创建的这个小代表,虽然不需要 updateSelectizeInput,所以我可能误解了你想要做的事情。
在此示例中,县 selectInput 接收一个反应性对象作为其选择输入,基于用户状态选择对县列表进行子集化,因此县选择输入将随着用户在状态之间切换而更新。
我还使用 conditionalPanel 包装了县输入 - 此输入仅在用户使用 state selectInput 选择了一个州时显示。
UPDATE* 添加 'req(length(input$state) > 0)' 所以反应在选择状态之前不会在服务器中执行
library(shiny)
library(tidyverse)
ui <- fluidPage(
pageWithSidebar(
headerPanel("My chart"),
sidebarPanel(
uiOutput("sel_state"),
conditionalPanel(condition = "input.state.length > 0",
uiOutput("sel_county")
)
),
mainPanel(
)
)
)
server <- function(input, output) {
#create example of data structure
df <- structure(list(state = c("Washington", "Washington", "Washington",
"Illinois", "Washington", "California"), county = c("Snohomish",
"Snohomish", "Snohomish", "Cook", "Snohomish", "Orange")), row.names =
c(NA, 6L), class = "data.frame")
#pull out unique state names
state_list <- as.character(unique(df$state))
#filter for counties only in desired state and pull column to vector
selected_state_counties <- reactive({
req(length(input$state) > 0)
df %>%
filter(state == input$state) %>%
pull(county) %>%
as.character()
})
output$sel_state <- renderUI({
selectizeInput('state', 'Select a State', choices=c("Choose One" = "", state_list))
})
output$sel_county <- renderUI({
selectizeInput('county', 'Select a County', choices=c("Choose One" = "", selected_state_counties()))
})
}
# Run the application
shinyApp(ui = ui, server = server)
推荐阅读
- python - python中的CSV标头
- reactjs - 使用 mobx 获取数据进入无限循环
- c# - EF Core:无法跟踪实体类型的实例,因为另一个实例具有相同的键值
- javascript - 第 530 行的错误 1062 (23000):重复条目 '20190617051131-apply-schema.js' 用于键 'PRIMARY'
- laravel - Laravel 附加在关联键上
- opencv - 将 numpy ndarray (1, 160, 160) 保存到灰度图像中
- java - 创建一个 ID 数组并随机化他们的年龄
- arrays - 如何分配 ID 来识别重复的数组元素?
- node.js - 如何在猫鼬中为模式类型日期分配日期值
- algorithm - Big-o 复杂性问题 - 线性累积功率