如何解决R开发Shiny交互地图时select报错:无方法适配function类对象
错误原因
- 传入
STATE函数的stat是Shiny输入的字符串变量,不能用enquo()语法处理,应该用.data[[stat]]的方式取对应数据列 STATE函数中定义了Month参数但没有实际用于过滤数据,滑块选择的月份值完全没有生效- UI下拉选项的取值存在拼写错误,
total_staff_cases、total_prisoner_cases都少写了字母l,导致匹配不到数据集里的对应列 STATE函数里的n_players是棒球数据集的残留变量,当前数据集不存在该字段,且你不需要做top_n筛选,需要的是每个州对应月份的全量统计值COVID_Plot函数没有将STATE函数的输出结果和空间地图数据关联,直接用预定义的TEST_JOIN绘图取不到计算后的plot_stat字段
修正后可运行代码
if (!require(Lahman)) install.packages('Lahman') if (!require(plotly)) install.packages('plotly') if (!require(shiny)) install.packages('shiny') if (!require(tidyverse)) install.packages('tidyverse') if (!require(albersusa)) install.packages('albersusa') library(albersusa) library(shiny) library(Lahman) library(tidyverse) library(plotly) my_map_theme <- function(){ theme(panel.background=element_blank(), axis.text=element_blank(), axis.ticks=element_blank(), axis.title=element_blank()) } # 从CSV加载已整理的数据集 cpc <- read_csv("cpc.csv") us_states <- usa_sf("laea") cpc_tidy <- cpc %>% select(name, staff_tests, total_staff_cases, total_staff_deaths, prisoner_tests, total_prisoner_cases, total_prisoner_deaths, as_of_date, year, Month) %>% arrange(name) cpc_tidy_f <- cpc_tidy %>% group_by(name, year, Month) %>% summarise(across(everything(), last)) cpc_sel <- cpc_tidy_f cpc_sel$merge <- as.character.Date(paste(cpc_sel$year, cpc_sel$Month, sep = "-")) TEST_JOIN <- left_join(us_states, cpc_sel, c=("name"="name")) STATE <- function(stat, Month = 11, data = cpc_sel) { # 过滤选中月份,用.data[[stat]]读取字符串对应的列 data %>% filter(Month == !!Month) %>% select(name, merge, plot_stat = .data[[stat]]) } COVID_Plot <- function(stat_data) { # 关联统计结果与空间数据 plot_data <- left_join(us_states, stat_data, by = "name") p <- plot_data %>% mutate(text_y = paste("<b>",name, "</b>\n 统计值:", signif(plot_stat,3), "\n统计时段:2020-2021")) %>% ggplot() + geom_sf(aes(fill=plot_stat, text=text_y), color="black") + scale_fill_continuous("统计指标总计:", low="#EEFBE5", high="#082573") + my_map_theme() ggplotly(p, tooltip = "text") %>% style(hoveron = "fill") } # 定义应用UI ui <- fluidPage( # 应用标题 titlePanel("美国监狱新冠疫情数据"), sidebarLayout( sidebarPanel( selectInput("my_stat", "选择要绘制的统计指标:", choices = list("工作人员累计确诊" = "total_staff_cases", "囚犯累计确诊" = "total_prisoner_cases", "工作人员累计死亡" = "total_staff_deaths", "囚犯累计死亡" = "total_prisoner_deaths" ), selected = "total_prisoner_cases"), sliderInput("n", "选择月份:", min = 1, max = 15, value = 11)), # 主面板展示内容 mainPanel( h3(textOutput("TitleText")), h5(textOutput("SubtitleText")), plotlyOutput("statPlot"), h5("数据来源:The Marshall Project监狱新冠数据集"), h5("图表参考:The Marshall Project文章《各州监狱15个月新冠疫情情况》") ) ) ) # 定义服务端逻辑 server <- function(input, output) { output$TitleText <- renderText(paste(input$my_stat, "历史记录")) output$SubtitleText <- renderText(paste("下图展示第", input$n, "个月美国各州对应指标统计值")) output$statPlot <- renderPlotly({ COVID_Plot(STATE(stat = input$my_stat, Month = input$n, data = cpc_sel)) }) } # 运行应用 shinyApp(ui = ui, server = server)
补充说明
- 代码补充了死亡指标的下拉选项,你可以根据需求自行增删
- 如果月份和年份的对应关系需要调整,可以在
STATE函数的过滤逻辑里修改 - 绘图提示文本已经做了汉化,你可以根据需求调整内容
内容的提问来源于stack exchange,提问作者speeeZy
相关产品推荐
相关产品推荐

