I suggest you use the {sf} package format for your data. It will make a lot of things easier.
In my example I am first transforming your data from a regular data frame to a spatial data frame as per the {sf} package.
The {leaflet} package plays nicely with {sf}, and when you provide your data in sf format it will take care of the rest.
library(dplyr)
library(sf)
library(leaflet)
# your data:
data <- tibble::tribble(~location, ~longitude, ~latitude,
"loc1", 24.600, 67.524,
"loc1", 24.600, 67.535,
"loc2", 28.478, -17.789,
"loc3", 30.654, 12.397,
"loc3", 30.654, 12.397,
"loc3", 30.654, 12.397)
# make data feel spatial = tranform to sf format
data <- data %>%
st_as_sf(coords = c("longitude", "latitude"),
crs = 4326)
# call leaflet with your data as data object
leaflet(data = data) %>%
addProviderTiles("CartoDB.Positron") %>% # a basemeap
addMarkers(popup = ~location) # addCircleMarkers is another possibility
As a shameless plug: a while back I wrote a long form blog post about using leaflet.js in R, you might find it interesting: Leaflet in R · Jindra Lacko
Hi jlacko,
But my concern is that I have all the location information in a csv file and there are so many. How can I make the R file itself extract all the location details? Any advice...
I suggest you install the sf package (= install.packages("sf")) - that will make the error of "cannot find function like st_as_sf" go away - and use the code I proposed. It works.
you are simply not running the code the jlacko provided for you.
you still are using this Map() function... jlacko doesnt use that in his working code example