0

I'm trying to re-appropriate a Leaflet code I used six months ago, with which I had no problem at the time. I haven't used Leaflet since then and am now no longer able to generate maps like I was before.

Simply put, after switching out the variables in the same way I have for many other maps 6 months ago, I am receiving the following error message and am not sure how to fix it: Error in mutate_impl(.data, dots) : object 'address.lon' not found

I loaded the appropriate packages and data formats. Basic troubleshooting has been addressed. How do I fix this?

The contextual code is the following:

Example Data
org dept address latitude longitude
ABC, Inc., SPSG, 111111 North Whatever Houston, TX 77058, 29.5431888, -95.1023828
DEF, Inc., Security Systems, 111 North Sepulveda boulevard 2000 El Segundo, CA 90245, 42.6379953, -71.2459721


df %>%  
mutate(popup_info=paste(sep = "<br/>", paste0("<b>","<i>", org,"<i>", "</b>"), dept)) %>% 
filter(!is.na(longitude) & !grepl("CLOSED", org)) %>% 
filter(!is.na(latitude) & !grepl("CLOSED", org)) -> df1

## Plot the Maps
# Client Map
orgpal <- colorFactor(plasma(7), df1$org) 
leaflet(df1) %>%
addProviderTiles("CartoDB.Positron") %>%
addCircleMarkers(lng = ~longitude, 
               lat = ~latitude, 
               radius = 2.5, 
               fillColor = ~orgpal(orgpal),
               stroke=FALSE,
               fillOpacity = 1,
               popup = ~popup_info) %>%
addLegend("bottomright", pal = orgpal, values = ~org, labels = "Organization", title = "Client Locations") %>%
addMiniMap(tiles = providers$CartoDB.PositronNoLabels, width = 120, height=80)

Am I misunderstanding the function of address.lon? I apologize if my lack of understanding of coding prevents me from seeing an easy fix, I just haven't interacted with this code in a while. I appreciate any help I can receive.

  • 3
    Are you sure `address.lon` is a column in your dataset? It doesn't appear to be from your comment. Not sure what you were trying to accomplish in that `mutate()`. It's easier to help you if you provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input so we can run and test the code ourselves. – MrFlick Aug 18 '17 at 14:55
  • 1
    Double-check that `address.lon` is a column in your `df` - just check that that's the correct name in the csv you are reading in. Otherwise, change that to whatever the correct name should be. – meenaparam Aug 18 '17 at 14:55
  • Thanks for your guys' feedback. That was helpful. I gave a response to @sconfluentus, if you're interested in seeing where this has gone. – Bradley Thomas Anderson Aug 18 '17 at 20:49

1 Answers1

2

Your script is failing because your address.lon in the ifelse statement refers to nothing in your obvious local environment based on the code included.

It is not in "sp" package, or "rgdal" or "leaflet" , you have not defined it yourself as a function and I cannot see where you have loaded access to it prior to calling it.

It is also, NOT another column in your data frame. So, R has nowhere to pull that from when it reaches it in your code.

It is my guess that your prior leaflet project had more preceding code than this and address.lon was defined in it.

If you post all of that code in an edit, or link to it in a file somewhere out in the wilds of the web, one of us could probably help you figure out both what address.lon does, and how to get it working in this mapping script.

sconfluentus
  • 4,693
  • 1
  • 21
  • 40
  • I removed address.lon. I recognized the error when you mentioned it. I have made edits above due to your feedback, including adding the data.frame format. I now have a different error with my above code which is similar. I receive an error message saying: Error in as.character(x) : cannot coerce type 'closure' to vector of type 'character' In addition: Warning messages: 1: In validateCoords(lng, lat, funcName): Data contains 1 rows with either missing or invalid lat/lon values and will be ignored 2: In is.na(x) : is.na() applied to non-(list or vector) of type 'closure' – Bradley Thomas Anderson Aug 18 '17 at 20:48
  • 1
    run the function `str(df)` on your data frame and it will likely give me the clues I need to fix that problem. My guess is that your lat lon variables are coming in as character instead of numerical data. – sconfluentus Aug 18 '17 at 22:16
  • Hey @sconfluentus, I ran str but to no effect. The error message is still: Error in as.character(x) : cannot coerce type 'closure' to vector of type 'character' In addition: Warning messages: 1: In is.na(x) : is.na() applied to non-(list or vector) of type 'closure' 2: In is.na(x) : is.na() applied to non-(list or vector) of type 'closure' I also ran sapply(df$latitude, class) verifying that it is indeed numeric. So, no change. Also, did you see the edits I made from your suggestions? I provide a small list of df variables. Thoughts? – Bradley Thomas Anderson Aug 22 '17 at 17:24