Problems with leaflet()

Hi, I am trying to get the following leaflet code to work in R(4.0) Studio it creates the initial maptiles but it stops there, it doesn't complete the map...any help, thank you all...A.

library(curl)
library(leaflet)
data_filter = read.csv(curl('https://raw.githubusercontent.com/joeyklee/aloha-r/master/data/calls_2014/201401CaseLocationsDetails-geo-filtered.csv'), header=TRUE)
# subset out your data_filter:
df_graffiti_noise = subset(data_filter, cid == 1)
df_street_traffic_maint = subset(data_filter, cid == 2)
df_garbage_recycling_organics = subset(data_filter, cid == 3)
df_water = subset(data_filter, cid == 4)
df_animal_vegetation = subset(data_filter, cid == 5)
df_other = subset(data_filter, cid == 0)

# initiate leaflet
m = leaflet()

# add openstreetmap tiles (default)
m = addTiles(m)

# you can now see that our maptiles are rendered
m
colorFactors = colorFactor(c('red', 'orange', 'purple', 'blue', 'pink', 'brown'),
                           domain = data_filter$cid)
m = addCircleMarkers(m, 
                     lng = df_graffiti_noise$lon_offset, # we feed the longitude coordinates 
                     lat = df_graffiti_noise$lat_offset,
                     popup = df_graffiti_noise$Case_Type, 
                     radius = 2, 
                     stroke = FALSE, 
                     fillOpacity = 0.75, 
                     color = colorFactors(df_graffiti_noise$cid),
                     group = "1 - graffiti & noise"
)
m = addCircleMarkers(m, 
                     lng = df_street_traffic_maint$lon_offset, lat=df_street_traffic_maint$lat_offset, 
                     popup = df_street_traffic_maint$Case_Type, 
                     radius = 2, 
                     stroke = FALSE, fillOpacity = 0.75,
                     color = colorFactors(df_street_traffic_maint$cid),
                     group = "2 - street & traffic & maintenance")
m = addCircleMarkers(m, 
                     lng = df_garbage_recycling_organics$lon_offset, lat=df_garbage_recycling_organics$lat_offset, 
                     popup = df_garbage_recycling_organics$Case_Type, 
                     radius = 2, 
                     stroke = FALSE, fillOpacity = 0.75,
                     color = colorFactors(df_garbage_recycling_organics$cid),
                     group = "3 - garbage related")
m = addCircleMarkers(m, 
                     lng = df_water$lon_offset, lat=df_water$lat_offset, 
                     popup = df_water$Case_Type, 
                     radius = 2, 
                     stroke = FALSE, fillOpacity = 0.75,
                     color = colorFactors(df_water$cid),
                     group = "4 - water related")
m = addCircleMarkers(m, 
                     lng = df_animal_vegetation$lon_offset, lat=df_animal_vegetation$lat_offset, 
                     popup = df_animal_vegetation$Case_Type, 
                     radius = 2,
                     stroke = FALSE, fillOpacity = 0.75,
                     color = colorFactors(df_animal_vegetation$cid),
                     group = "5 - animals & vegetation")
m = addCircleMarkers(m, 
                     lng = df_other$lon_offset, lat=df_other$lat_offset, 
                     popup = df_other$Case_Type, 
                     radius = 2,
                     stroke = FALSE, fillOpacity = 0.75,
                     color = colorFactors(df_other$cid),
                     group = "0 - other")

m = addTiles(m, group = "OSM (default)") 
m = addProviderTiles(m,"Stamen.Toner", group = "Toner")
m = addProviderTiles(m, "Stamen.TonerLite", group = "Toner Lite")
m = addLayersControl(m,
                     baseGroups = c("Toner Lite","Toner"),
                     overlayGroups = c("1 - graffiti & noise", "2 - street & traffic & maintenance",
                                       "3 - garbage related","4 - water related", "5 - animals & vegetation",
                                       "0 - other")
)

# make the map
m

You've got a lot going on in your code. I highly recommend that you take away all the extraneous code to make a more simple example that illustrates the problem you are having. That way it will be easier for others (and yourself!) to identify which part of your code is causing the issues.

1 Like

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

thanks for your advice, i did a few changes and the code is working now.