0

I try to use addLayersControl function to create 2 optional layers. However, There is an error. I want to get helps.

        popup_LU <- paste0("<strong>Use Name: </strong>", 
                             LandUse$name, 
                             "<br><strong>Link: </strong>", 
                             LandUse$url)

          popup_NationalPark<-paste0("<strong>Use Name:</strong>",
                                     NationalPark$name,
                                     "<br><strong>National Park Area: 
                                     </strong>",
                                     NationalPark$area)


          pal <- colorQuantile("YlOrRd", NULL, n = 9)
          gmap <- leaflet(data = cancermap) %>%
            # Base groups
            addTiles() %>%
            setView(lng = -105, lat = 40, zoom = 4) %>% 
            addPolygons(fillColor = ~pal(rate), 
                        fillOpacity = 0.8, 
                        color = "#BDBDC3", 
                        weight = 1,
                        popup = popup_dat,
                        group="Cancer Rate/100,000 by Counties") %>% 

            # Overlay groups
            addMarkers(data=LandUse,lat=~lat, lng=~lng, popup=popup_LU, 
            group = "Land Use Sites") %>% 
            addCircleMarkers(data=NationalPark, lat=~lat, lng=~lng,color = 
            "green",popup = popup_NationalPark,group="National Park 
            Sites")%>% 

            # Layers control
            addLayersControl(
              baseGroups = c("Cancer Rate/100,000 by Counties"),
              overlayGroups = c("Land Use Sites","National Park Sites"),
              options = layersControlOptions(collapsed = FALSE)
            )

the error is:

     +       addLayersControl(
     +       baseGroups = c("Cancer Rate/100,000 by Counties"),
     +       overlayGroups = c("Land Use Sites","National Park Sites"),
     +      options = layersControlOptions(collapsed = FALSE)
     +                 )
      Error in cut.default(x, binsToUse, labels = FALSE, include.lowest = 
      TRUE,  : 
      'x' must be numeric     

The heads of my data are:

    > head(NationalPark)
                name    lat     lng             area recreation visitors
    1         Acadia  44.35  -68.21  49,057.36 acres           3,303,393
    2 American Samoa -14.25 -170.68   8,256.67 acres              28,892
    3         Arches  38.68 -109.57  76,678.98 acres           1,585,718
    4       Badlands  43.75 -102.50 242,755.94 acres             996,263
    5       Big Bend  29.25 -103.25 801,163.21 acres             388,290
    6       Biscayne  25.65  -80.08 172,971.11 acres             514,709

    > head(LandUse)
        offset lat        lng                 url
    1      0 38.05421 -108.86430 http://clui.org/ludb/site/slick-rock-disposal-cell
    2      1 32.95283 -111.81657              http://clui.org/ludb/site/sacaton-pit
    3      2 32.51136 -111.32519            http://clui.org/ludb/site/pinal-airpark
    4      3 37.04954  -88.32650          http://clui.org/ludb/site/lwd-incinerator
    5      4 37.18685  -86.09882             http://clui.org/ludb/site/mammoth-cave
    6      5 37.88329  -85.96521                http://clui.org/ludb/site/fort-knox
                  name
   1 Slick Rock Disposal Cell
   2              Sacaton Pit
   3            Pinal Airpark
   4          LWD Incinerator
   5             Mammoth Cave
   6                Fort Knox
Cœur
  • 37,241
  • 25
  • 195
  • 267
Yipin
  • 173
  • 1
  • 2
  • 12
  • Looks like the problem is that the values in your data.frame aren't numeric. There seems to be a lot of formatting and units in there which prevent R from treating those are numeric. It's also a bit confusing that there are references to both cancer data and national park data. Make sure you provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) such that we can actually copy/paste your code to run it. Showing the `head()` of a data.frame isn't that helpful. – MrFlick Jul 10 '17 at 14:18
  • Thank you very much. It is indeed the problem of the values in my data.frame. I transform the format from character to numeric and thus the problem is solved! The code is my assignment, so it has data of cancer and national park. Thank you! Have a nice day! – Yipin Jul 12 '17 at 09:38

0 Answers0