Error in validateCoords Assistance

Hello, I have been using leaflet to create maps for a project.

Recently I have gotten the issue "Error in validateCoords(lng, lat, funcName) : addCircleMarkers requires numeric longitude/latitude values"

My data does have longitude and latitude values, so I was wondering if anyone knew of how to fix this issue.

My code is:

library(leaflet)
library(tidyverse)
library(farver)
library(dplyr)
library(stringr)
library(RColorBrewer)
#Set working directory and import data
setwd("/Users/JohnDow/Desktop/RStudioMap")
Data<-read.csv("FinalR2.csv", stringsAsFactors=F)

#Subset data by MSI category
HSI <- Data %>% filter(Data$MSI.category =="HSI")
HBCU <- Data %>% filter(Data$MSI.category =="HBCU")
TCU <- Data %>% filter(Data$MSI.category =="TCU")
Fed <- Data %>% filter(Data$MSI.category =="Federal Institution")
Ind <- Data %>% filter(Data$MSI.category =="Industry")
Academ <- Data %>% filter(Data$MSI.category =="Non-MSI Academic Institution")
np <- Data %>% filter(Data$MSI.category =="Non-Profit")
prof <- Data %>% filter(Data$MSI.category =="Professional Society")
none <- Data %>% filter(Data$MSI.category =="No MSI Catagory")
AANAPISI <- Data %>% filter(Data$MSI.category =="AANAPISI")
NASACenter <- Data %>% filter(Data$MSI.category == "NASA Center")

hsi_loc <-select(HSI, Institution, Lat, Long, City, State, Country, MSI.type, MSI.category)
hbcu_loc <-select(HBCU,Institution, Lat, Long, City, State, Country, MSI.type, MSI.category)
tcu_loc <-select(TCU, Institution, Lat, Long, City, State, Country, MSI.type, MSI.category)
Ind_loc <-select(Ind, Institution, Lat, Long, City, State, Country, MSI.type, MSI.category)
fed_loc <-select(Fed, Institution, Lat, Long, City, State, Country, MSI.type, MSI.category)
Academ_loc <-select(Academ, Institution, Lat, Long, City, State, Country, MSI.type, MSI.category)
np_loc <-select(np, Institution, Lat, Long, City, State, Country, MSI.type, MSI.category)
prof_loc <-select(prof, Institution, Lat, Long, City, State, Country, MSI.type, MSI.category)
n_loc <-select(none, Institution, Lat, Long, City, State, Country, MSI.type, MSI.category)
a_loc <-select(AANAPISI, Institution, Lat, Long, City, State, Country, MSI.type, MSI.category)
nasa.center_loc <-select(NASACenter, Institution, Lat, Long, City, State, Country, MSI.type, MSI.category)

md <-rbind(hsi_loc, hbcu_loc,tcu_loc, n_loc, Academ_loc, fed_loc, Ind_loc,prof_loc,np_loc, a_loc) 

#factpal <- colorFactor(topo.colors(7), md$MSI.type)
#factpal <- colorFactor(brewer.pal(n = 7, name = "Dark2"), md$MSI.type)
factpal <- colorFactor(brewer.pal("Set1", n = 7), md$MSI.type)
#factpal <- colorFactor(brewer.pal("Paired", n = 7), md$MSI.type)

loc <- paste(md$City,md$State,md$Country, sep = ", ") #Returns "City, ST, Country"
bold.ins <- paste("<b><font color=\"#284D8E\">",md$Institution,"</font></b>")

content <- paste(sep = "<br/>",
                 bold.ins,
                 loc,
                 md$MSI.category
)

m<- leaflet(md)%>% 
  addTiles('http://{s}.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}.png')%>%
  setView(-97.655210, 30.324303, zoom = 4) %>%
  addCircleMarkers(~Long, ~Lat, popup=content, weight = 3, radius= (~ifelse(MSI.type == "NASA Center", 4.5, 4.5)), color=~factpal(MSI.type), stroke = F, fillOpacity = 1)%>%
  addLegend("bottomright", pal = factpal, values = ~MSI.type, title = "Categories")

m

Any help is appreciated.

Hard to tell without a reprex. See the FAQ. A skinnier dataset with fewer rows and variables is all that's needed for the minimal reproducible example.

It seems like your Long and (possibly) Lat fields of your FinalR2.csv file are not numeric.

What does is.numeric(Data$Long) tell you? if FALSE you have found your culprit.

Also, and this is tangential to the question on hand, you seem to be doing the map in a rather convoluted fashion - starting with a single Data object, and ending with a single md object... You likely could get away with only using Data, perhaps filtered & converted to {sf} package format.

1 Like

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.