使用openairmaps的addPolarMarkers时出现drop_na方法适配错误
问题
之前用R的openairmaps和leaflet包在地图上添加污染极坐标图运行正常,半年后复用相同代码和数据时出现报错:
Error in UseMethod("drop_na") : no applicable method for 'drop_na' applied to an object of class "list"
调试发现问题出在addPolarMarkers内部的create_polar_marker函数,传入该函数的数据变成了包含标签和虚拟变量的列表。
数据样例
| date | year | month | particle_size | concentration | wd | ws | LAT | LON |
|---|---|---|---|---|---|---|---|---|
| 01 | 2021 | 01 | 10.8 | 192830 | 180 | 5 | 42 | -71 |
| 02 | 2021 | 01 | 6.4 | 45028 | 180 | 0 | 42 | -71 |
| 03 | 2021 | 01 | 20.9 | 47293 | 310 | 2 | 42 | -71 |
| 04 | 2021 | 01 | 295.2 | 33894 | 18 | 5 | 42 | -71 |
| 05 | 2021 | 01 | 365.1 | 173628 | 270 | 7 | 42 | -71 |
| 06 | 2021 | 01 | 49.2 | 102983 | 210 | 10 | 42 | -71 |
| 07 | 2021 | 01 | 65.7 | 473628 | 180 | 9 | 42 | -71 |
所有字段均为数值型。
运行代码
m <- leaflet() %>% addTiles() %>% # Add default OpenStreetMap map tiles addMarkers(lng=data$LON, lat=data$LAT, popup="The birthplace of R") m %>% addProviderTiles(providers$CartoDB.Voyager) %>% addPolarMarkers( data, pollutant = "concentration", fun=openair::polarPlot, lng= "LON", lat= "LAT", #cols="jet", #d.icon=120, #popup="name", #limits = c(0, 50000) )
可能原因与解决办法
原因分析
报错核心是tidyr::drop_na函数被应用在了列表对象上,而该函数仅支持数据框类对象。大概率是包版本更新导致openairmaps内部处理数据的逻辑发生变化,比如依赖的tidyverse系列包(如dplyr、tidyr)或openairmaps自身版本迭代,使得原本传入数据框的地方变成了列表。另外,数据传入方式的细微变化也可能触发这个问题。
解决步骤
显式指定
data参数
修改addPolarMarkers调用,明确将数据赋值给data参数,避免函数参数解析出错:m %>% addProviderTiles(providers$CartoDB.Voyager) %>% addPolarMarkers( data = data, # 显式指定data参数 pollutant = "concentration", fun=openair::polarPlot, lng= "LON", lat= "LAT" )检查并锁定包版本
半年间相关包可能已更新,建议检查当前包版本:packageVersion("openairmaps") packageVersion("tidyr") packageVersion("openair")若确认是版本更新导致的问题,可安装之前能正常运行的旧版本(以
openairmaps为例):# 需要先安装remotes包 install.packages("remotes") remotes::install_version("openairmaps", version = "x.y.z") # 替换为之前的版本号确保输入数据为纯数据框
检查输入数据的类型,若不是标准数据框,转换后再传入:# 检查数据类型 class(data) # 转换为数据框 data <- as.data.frame(data)处理单点位数据的分组问题
你的样例数据中所有点的经纬度完全相同,openairmaps在按经纬度分组时可能生成异常的列表结构。可以尝试添加一个微小的经纬度偏移,或者先聚合数据再传入:# 给经纬度添加微小偏移(仅测试用) data$LON <- data$LON + rnorm(nrow(data), 0, 0.0001) data$LAT <- data$LAT + rnorm(nrow(data), 0, 0.0001)
内容的提问来源于stack exchange,提问作者bre123
相关产品推荐
相关产品推荐

