R Shining updateSelectizeInput引发错误且未更新
我正在尝试创建两个下拉列表,一个是州列表,然后过滤第二个下拉列表,列出所选州的所有县。以下代码起作用,但更改状态时县不会更新 用户界面 服务器R Shining updateSelectizeInput引发错误且未更新,r,shiny,R,Shiny,我正在尝试创建两个下拉列表,一个是州列表,然后过滤第二个下拉列表,列出所选州的所有县。以下代码起作用,但更改状态时县不会更新 用户界面 服务器 function(input, output, session) { output$sel_state <- renderUI({ selectizeInput('state', 'Select a State', choices=c("Choose One" = "", state_list)) }) output$sel_
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()
})
}
错误:无法将“环境”类型强制为“字符”类型的向量
创建数据帧的代码-县列表还不是一个合适的列表
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))
我没有您的州/县信息(或其结构),但这个小示例完成了我认为您描述的内容。注意事项: 您需要首先使用selectizeInput创建selectInputUI元素,然后使用updateSelectizeInput更新其内容。所以您需要这两个函数,但是对于我创建的这个小reprex,不需要updateSelectizeInput,因此我可能错误地解释了您想要执行的操作 在本例中,county selectInput根据用户状态选择对county列表进行子集设置,接收一个被动对象作为其选择输入,因此county selectInput将在用户在状态之间切换时更新 我还使用conditionalPanel包装了county输入-只有当用户使用state selectInput选择了一个州时,该输入才会显示 更新*add'req(长度(输入$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)
库(闪亮)
图书馆(tidyverse)
ui我没有您的州/县信息(或其结构),但这个小示例完成了我认为您描述的内容。注意事项:
您需要首先使用selectizeInput创建selectInputUI元素,然后使用updateSelectizeInput更新其内容。所以您需要这两个函数,但是对于我创建的这个小reprex,不需要updateSelectizeInput,因此我可能错误地解释了您想要执行的操作
在本例中,county selectInput根据用户状态选择对county列表进行子集设置,接收一个被动对象作为其选择输入,因此county selectInput将在用户在状态之间切换时更新
我还使用conditionalPanel包装了county输入-只有当用户使用state selectInput选择了一个州时,该输入才会显示
更新*add'req(长度(输入$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)
库(闪亮)
图书馆(tidyverse)
ui不使用updateSelectizeInput替换selectizeInput。将其添加到服务器。selectizeInput生成id为county的输入,updateSelectizeInput可以更新id为county的输入。getURL不适用于我。当我运行url.exists()时,它返回“FALSE”。你能尝试“dput(df)”并将输出复制/粘贴到问题中吗?如本答案所述:完成!我只是用head()拉了拉,因为它是一个大数据集,我还修复了URL,它的格式在问题中很奇怪,如果你愿意,现在应该可以重用它。谢谢-更新了答案。不要用updateSelectizeInput替换selectizeInput。将其添加到服务器。selectizeInput生成id为county的输入,updateSelectizeInput可以更新id为county的输入。getURL不适用于我。当我运行url.exists()时,它返回“FALSE”。你能尝试“dput(df)”并将输出复制/粘贴到问题中吗?如本答案所述:完成!我只是用head()拉了拉,因为它是一个大的数据集,我也修复了URL,它只是在问题中的格式很奇怪,如果你愿意,现在应该可以重用它。谢谢-更新的答案。这太棒了!我遇到的唯一问题是数据结构——正如你所说的。我把州和县放在一个数据框中,所以制作州列表很容易,但我正试图把我的两列数据框变成一个县列表,就像你们一样。我的数据框当前是一列州,然后是一列县(因此有多次列出的州)。如果您能够添加数据结构的一个小示例,我将尝试重新设计一个解决方案。如果这样做需要一个参考:我添加了我的代码以将数据拉到我的原始问题中,因为我很笨,无法正确设置注释的格式:)刚刚根据您的第一条注释更新了答案。此外,在所选的“状态”中添加“req(length(input$state)>0”。我注意到在R控制台中抛出了一个小警告。一、 再次更新答案。这太棒了!我遇到的唯一问题是数据结构——正如你所说的。我把州和县放在一个数据框中,所以制作州列表很容易,但我正试图把我的两列数据框变成一个县列表,就像你们一样。我的数据框当前是一列州,然后是一列县(因此有多次列出的州)。如果您能够添加数据结构的一个小示例,我将尝试重新设计一个解决方案。如果这样做需要一个参考:我添加了我的代码以将数据拉到我的原始问题中,因为我很笨,无法正确设置注释的格式:)刚刚根据您的第一条注释更新了答案。此外,在所选的“状态”中添加“req(length(input$state)>0”。我注意到在R控制台中抛出了一个小警告。一、 再次更新了答案。
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")
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)