1

I want to make a loop to add polygons to a leaflet map according to different columns of a data frame.

for (i in 18:22)
createMap = lapply((concello_wgs84_Datos@data[,i]),function(x){
data <- concello_wgs84_Datos@data[,i]
nombre <- names(concello_wgs84_Datos@data[,i])

pal3 <- leaflet::colorBin("YlOrRd", domain = data, 20, pretty = TRUE)  

m = leaflet(data=concello_wgs84_Datos[1:17 & concello_wgs84_Datos@data[,i] == x], options = leafletOptions(minZoom = 8, maxZoom = 18)) %>%
    addProviderTiles(providers$Esri.WorldImagery) %>% setView(-8.00, 42.80, zoom = 8.3) %>%
    addPolygons(data=datos, 
                fill= TRUE,
                fillColor = ~pal3(data), 
                stroke = FALSE, 
                color = "#0000CD",
                smoothFactor = 0.5,
                opacity = 1.0, 
                fillOpacity = 1,
                weight = 0.5,
                label = ~as.character(datos$CONCELLO),
                labelOptions = labelOptions(noHide = F,
                                            textsize = "7px",
                                            direction = "topright"),
    highlightOptions = highlightOptions(color = "white", 
                                        weight = 1,
                                        bringToFront = TRUE)) %>%
    addCircles(data = coordenadasOAC, 
               lat = ~ coordenadasOAC$LAT, 
               lng = ~ coordenadasOAC$LONG,radius =0.3, 
               color="#CD3333", 
               fillOpacity = 0.8, 
               popup = ~as.character(coordenadasOAC$OAC)) %>%
    addLegend("bottomleft", 
              pal = pal3, 
              values = data,
              title = nombre,
              labFormat = labelFormat(prefix = ""),opacity = 1)})
htmltools::tagList(createMap)

but I have the following error: Error in`[.data.frame´(x@data,i,j,...,drop=False): undefined columns selected. Could you help me?

far
  • 11
  • 1
  • 1
    Hello and welcome to StackOverflow! If you could provide us with a sample of your data, maybe we can test the loop. The following post will give you some help: https://stackoverflow.com/a/5963610/8382633. Having said that, I have a hunch. The error that you are getting is usually asociated with a missing comma when subsetting a data.frame. `concello_wgs84_Datos@data` is a data.frame or a vector? `concello_wgs84_Datos` is a data.frame or a list? – Santiago Capobianco Apr 11 '19 at 18:56

0 Answers0