首页 > 解决方案 > 传单“无法推断 lng 列”,但 df 显示加载的 lat 和 lng 列

问题描述

我正在尝试在 Leaflet 上绘制一个基本数据库。数据是带有“Artist”“Address”“lat”“Lon”的csv

structure(list(Artist = structure(1:6, .Label = c("50 Cent", 
"A Tribe Called Quest", "Aesop Rock", "B.I.G.", "Beastie Boys", 
"Big Daddy Kane"), class = "factor"), Address = structure(c(1L, 
4L, 2L, 3L, 1L, 1L), .Label = c("", "45 JOHN STREET SUITE 711 NEW YORK, NEW YORK, 10038 ", 
"Crown Heights", "Linden Boulevard and S192nd"), class = "factor"), 
    Lng = c(-73.79191, 73.759296, -74.007918, -73.963667, -73.910054, 
    -73.941774), lat = c(40.680859, 40.692427, 40.709348, 40.682806, 
    42.02039, 40.687218), Base.of.Operations = structure(c(4L, 
    2L, 3L, 1L, 1L, 1L), .Label = c("Brooklyn", "Jamaica", "Lower East-Side", 
    "South Jamaica"), class = "factor"), Significance = structure(c(3L, 
    5L, 2L, 4L, 1L, 1L), .Label = c("", "First place he recorded, El-Ps studio Definitive Records.", 
    "Grew Up", "Home", "Where \"Check the Rhyme\" was Shot"), class = "factor"), 
    Date.of.first.release = structure(c(3L, 4L, 1L, 6L, 2L, 5L
    ), .Label = c("11/1/1999", "11/15/1986", "2/6/2003", "4/17/1990", 
    "6/28/1988", "9/13/1994"), class = "factor"), X = c(NA, NA, 
    NA, NA, NA, NA)), class = "data.frame", row.names = c(NA, 
-6L))

我尝试将其用作 CSV、tibble、data.frame 并选择各个列,但它响应“Lon”col 不是对象。

library(dplyr)
library(leaflet)
library(sp)

"example" <- read_csv("database.csv", T)

df <- tibble("example")

map <- leaflet(df) %>%
  addTiles() %>%
  setView(lat=40.742054, lng=-73.769417, zoom = 9) %>%
  addMarkers(df)

map

我尝试调用 addMarkers() 中的列,但仍然出现错误。

database <- read.csv("map.csv")

db <- tibble(database)
    map <- leaflet(db) %>%
    addTiles() %>%
    setView(lat=40.742054, lng=-73.769417, zoom = 9) %>%
    addMarkers(lng = ~Lng, lat = ~lat)

Result
Error in eval(f[[2]], metaData(data), environment(f)) : 
object 'Lng' not found

什么什么

标签: rleafletlatitude-longitude

解决方案


我原来的 Db 已损坏。我将它重新格式化为原生 CSV,它运行良好。


推荐阅读