2

I'm trying to plot a basic database onto Leaflet. The data is csv with "Artist""Address""lat""Lon"

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))

I've tried using it as CSV, tibble, data.frame, and selecting the individual columns, but it responds that the "Lon" col is not an object.

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

I tried calling the columns in the addMarkers() and still got an error.

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

What the what

5
  • Could you provide some example data to make your code reproducible? Minor code comment: you should avoid using T as it is not a reserved value; try T <- FALSE; F <- TRUE :) Commented Nov 8, 2019 at 18:07
  • 1
    Definitely formatted that data wonky in here, hope it's legible.
    – wattskiski
    Commented Nov 8, 2019 at 18:32
  • You should provide lng and lat arguments in your addMarkers call, like addMarkers(df, lng = ~Lng, lat = ~lat) and it should work (at least it worked in my system with the data provided). Also you may want to check the longitud coordinate for A Tribe Called Quest entry, as it should be negative I think. Commented Nov 8, 2019 at 19:11
  • @MalditoBarbudo I tried that and this is what I got map <- leaflet(db) %>% + addTiles() %>% + setView() %>% + addMarkers(lng = ~Lng, lat = ~lat) Error in evalAll(list) : argument "lat" is missing, with no default could it have something to do with my environment? I'm using rstudio.cloud as my workspace atm
    – wattskiski
    Commented Nov 8, 2019 at 19:20
  • @wattskiski the problem now is the setView call, it needs a lat and lng arguments ;) Replicate your original code (in the question) with my comment and you'll se it works, I tested it in my local system and in my rstudio cloud account. Commented Nov 8, 2019 at 19:41

1 Answer 1

1

My original Db was corrupted. I reformatted it to native CSV and it worked fine.

Not the answer you're looking for? Browse other questions tagged or ask your own question.