Unable to generate maps using leaflet

Hi all,

I have been trying to generate a map using leaflet to locate sites based on longitude and latitude column values in my csv file.

Preformatted textlibrary(tidyverse)
library(leaflet)
library(stringr)
library(sp)
library(maps)

data2 <- read.csv('siteinfo.csv')
names(data2)
mapSites = map("data2$location ", fill = TRUE, plot = FALSE)
leaflet(data = mapSites) %>% addTiles() %>%
addMarkers(~long, ~lat)Preformatted text

This is what I'm trying with.
Here is how my data frame looks

location longitude latitide
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

How can I create a map? Thanks in Advance..

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

1 Like

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

And is there a problem with reading the csv in? I mean, does this not work for you?

library(dplyr)
library(sf)
library(leaflet)

data <- readr::read_csv("siteinfo.csv") %>% 
  st_as_sf(coords = c("longitude", "latitude"), 
           crs = 4326)

leaflet(data = data) %>% 
  addProviderTiles("CartoDB.Positron") %>%  # a basemeap
  addMarkers(popup = ~location) # addCircleMarkers is another possibility

Nope, It is telling that cannot find function like st_as_sf

library(tidyverse)
library(leaflet)
library(stringr)
library(sp)
library(maps)

data <- read.csv('siteinfo.csv')
names(data2)
mapSites <- Map("data$location", fill = TRUE, plot = FALSE)
leaflet(data = mapSites) %>% addTiles() %>%
addMarkers(~long, ~lat)

I'm trying this but it is giving error output data$location of function mode is not found.
@jlacko

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.

Nope. It's still the same.

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

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.