0
votes

I am trying to map San Francisco's crimes in a map. The below code intends to map every crime (lat, lng) and when the marker is clicked, show the "category" column of the dataset. Right now the below code shows a blank text box when I click the marker. Can anybody help?

    sf <- read.csv("https://raw.githubusercontent.com/uwescience/datasci_course_materials/master/assignment6/sanfrancisco_incidents_summer_2014.csv")
crime <- data.frame(lat = c(sf$Y),
                      lng = c(sf$X))
cat <- c(sf$Category)

library(leaflet)
crime %>% 
  leaflet() %>%
  addTiles() %>%
  addMarkers(popup = paste(sf$Category), clusterOptions = markerClusterOptions())
1
The popups are working fine for me - I'm seeing the category of crime in the pop-upSymbolixAU
@SymbolixAU, what do you think would be my problem? I have latest RStudio.Daniel Vargas
Works for me too. Using R 3.4.0, leaflet 1.1.0, latest RStudio Windows 7.neilfws
I am in R3.3.3, RStudio 1.0.136 and just re-downloaded leaflet, no success.Daniel Vargas
Latest RStudio is 1.0.143. Don't know whether that matters.neilfws

1 Answers

0
votes

Try the following:


sf <- read.csv("https://raw.githubusercontent.com/uwescience/datasci_course_materials/master/assignment6/sanfrancisco_incidents_summer_2014.csv")

library(leaflet)

sf %>% 
   leaflet() %>%
   addTiles() %>%
   addMarkers(lat = ~Y, lng = ~X, popup = ~Category, clusterOptions = markerClusterOptions())

I'm not sure what your issue is, but using the formula syntax allows leaflet to build the list of pop-up labels on its own and does not require calling paste explicitly or subsetting the original dataframe.