You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用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函数,传入该函数的数据变成了包含标签和虚拟变量的列表。

数据样例

dateyearmonthparticle_sizeconcentrationwdwsLATLON
0120210110.8192830180542-71
022021016.445028180042-71
0320210120.947293310242-71
04202101295.23389418542-71
05202101365.1173628270742-71
0620210149.21029832101042-71
0720210165.7473628180942-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自身版本迭代,使得原本传入数据框的地方变成了列表。另外,数据传入方式的细微变化也可能触发这个问题。

解决步骤

  1. 显式指定data参数
    修改addPolarMarkers调用,明确将数据赋值给data参数,避免函数参数解析出错:

    m %>% addProviderTiles(providers$CartoDB.Voyager) %>% 
      addPolarMarkers(
        data = data,  # 显式指定data参数
        pollutant = "concentration",
        fun=openair::polarPlot,
        lng= "LON",
        lat= "LAT"
      )
    
  2. 检查并锁定包版本
    半年间相关包可能已更新,建议检查当前包版本:

    packageVersion("openairmaps")
    packageVersion("tidyr")
    packageVersion("openair")
    

    若确认是版本更新导致的问题,可安装之前能正常运行的旧版本(以openairmaps为例):

    # 需要先安装remotes包
    install.packages("remotes")
    remotes::install_version("openairmaps", version = "x.y.z")  # 替换为之前的版本号
    
  3. 确保输入数据为纯数据框
    检查输入数据的类型,若不是标准数据框,转换后再传入:

    # 检查数据类型
    class(data)
    # 转换为数据框
    data <- as.data.frame(data)
    
  4. 处理单点位数据的分组问题
    你的样例数据中所有点的经纬度完全相同,openairmaps在按经纬度分组时可能生成异常的列表结构。可以尝试添加一个微小的经纬度偏移,或者先聚合数据再传入:

    # 给经纬度添加微小偏移(仅测试用)
    data$LON <- data$LON + rnorm(nrow(data), 0, 0.0001)
    data$LAT <- data$LAT + rnorm(nrow(data), 0, 0.0001)
    

内容的提问来源于stack exchange,提问作者bre123

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 22:44:52