Correctly Specifying Vectors in R

Viewed 92

I have this map in leaflet/r:

library(leaflet)
library(leaflet.extras)
library(dplyr)

# using the same reproducible data from the question/example
cities <- na.omit(read.csv(
    textConnection("City,Lat,Long,Pop, term1, term2
                    Boston,42.3601,-71.0589,645966, AAA, BBB
                    Hartford,41.7627,-72.6743,125017, CCC, DDD
                    New York City,40.7127,-74.0059,8406000, EEE, FFF
                    Philadelphia,39.9500,-75.1667,1553000, GGG, HHH
                    Pittsburgh,40.4397,-79.9764,305841, III, JJJ
                    Providence,41.8236,-71.4222,177994, JJJ, LLL
                    ")))

# leaf-green.png
#https://leafletjs.com/examples/custom-icons/leaf-green.png


leaflet(cities) %>%
    addProviderTiles(providers$OpenStreetMap) %>%
    addMarkers( clusterOptions = markerClusterOptions()) %>%
                    addResetMapButton() %>%
                    # these markers will be "invisible" on the map:
                    addMarkers(
                        data = cities, lng = ~Long, lat = ~Lat, label = cities$City,
                        group = 'cities', # this is the group to use in addSearchFeatures()
                        # make custom icon that is so small you can't see it:
                        icon = makeIcon(
                            iconUrl = "https://leafletjs.com/examples/custom-icons/leaf-green.png",
                            iconWidth = 1, iconHeight = 1
                        )
                    ) %>%
                    addSearchFeatures(
                        targetGroups = 'cities', # group should match addMarkers() group
                        options = searchFeaturesOptions(
                            zoom=12, openPopup = TRUE, firstTipSubmit = TRUE,
                            autoCollapse = TRUE, hideMarkerOnCollapse = TRUE
                        )
                    )

Using this map, I am able to "search" for a city using the search bar:

enter image description here

I would like to modify this code so that I can search based on "city", "term1" or "term2".

I tried this code over here:

leaflet(cities) %>%
    addProviderTiles(providers$OpenStreetMap) %>%
    addMarkers( clusterOptions = markerClusterOptions()) %>%
                    addResetMapButton() %>%
                    # these markers will be "invisible" on the map:
                    addMarkers(
                        data = cities, lng = ~Long, lat = ~Lat, label = cities$City,
                        group = 'cities', # this is the group to use in addSearchFeatures()
                        # make custom icon that is so small you can't see it:
                        icon = makeIcon(
                            iconUrl = "https://leafletjs.com/examples/custom-icons/leaf-green.png",
                            iconWidth = 1, iconHeight = 1
                        )
                    ) %>%
                    addSearchFeatures(
                        targetGroups = c('cities', 'term1', 'term2'), # group should match addMarkers() group
                        options = searchFeaturesOptions(
                            zoom=12, openPopup = TRUE, firstTipSubmit = TRUE,
                            autoCollapse = TRUE, hideMarkerOnCollapse = TRUE
                        )
                    )

This code runs without error, but I can not search using "term1" or "term2":

enter image description here

According to the documentation (https://www.rdocumentation.org/packages/leaflet.extras/versions/1.0.0/topics/addSearchFeatures), "addSearchFeatures" should accept a "vector of group names of groups whose features need to be searched". I was under the impression that vectors in R are specified using c('arg1', 'arg2', 'arg3') - but apparently in this function, this is not the case?

  • Could someone please show me how to fix this?

Thank you!

1 Answers

For the element to be a search term in addSearchFeatures, I'm pretty sure that it has to be a group element. Check it out:

leaflet(cities) %>%
  addProviderTiles(providers$OpenStreetMap) %>%
  addMarkers(clusterOptions = markerClusterOptions()) %>%
  addResetMapButton() %>%
  # these markers will be "invisible" on the map:
  addMarkers(
    data = cities, lng = ~Long, lat = ~Lat, label = cities$City,
    group = 'cities',# this is the group to use in addSearchFeatures()
    # make custom icon that is so small you can't see it:
    icon = makeIcon(
      iconUrl = "https://leafletjs.com/examples/custom-icons/leaf-green.png",
      iconWidth = 1, iconHeight = 1
    )) %>%
  addMarkers(data = cities, lng = ~Long, lat = ~Lat, 
             label = cities$term1, group = 'term1') %>% 
  addMarkers(data = cities, lng = ~Long, lat = ~Lat, 
             label = cities$term2, group = 'term2') %>% 
  addSearchFeatures(
    targetGroups = c('cities', 'term1', 'term2'), # group should match addMarkers() group
    options = searchFeaturesOptions(
      zoom=12, openPopup = TRUE, firstTipSubmit = TRUE,
      autoCollapse = TRUE, hideMarkerOnCollapse = TRUE
    )
  )

enter image description here

Related