I need to create a document to show the interventions done in different neighborhoods, I´d like to do it on a loop as the maps are all the same, except with different shapefiles that are named the same (i.e., streets_zone1, streets_zone2, lights_zone1, lights_zone2, ...)
I´ve tried several things but I end up copy pasting the code multiple times
This is the code I came up with in markdown
### Zone1
```{r MapZone1, echo=FALSE, message=FALSE, warning=FALSE}
lights_zone1 <- st_intersection(lights.shp, zone1.shp)
streets_zone1 <- st_intersection(streets.shp, zone1.shp)
tmap_mode("view")
tm_basemap(leaflet::providers$OpenStreetMap) +
tm_shape(Zone1.shp) +
tm_dots(col = "#1FC17B", alpha = 0.7) +
tm_borders(col = "#808080") +
tm_shape(streets_zone1) +
tm_fill(col = "#1FC17B", alpha = 0.7) +
tm_borders(col = "#808080") +
tm_shape(lights_zone1) +
tm_fill(col = "#1FC17B", alpha = 0.7) +
tm_borders(col = "#808080")
### Zone2
```{r MapZone2, echo=FALSE, message=FALSE, warning=FALSE}
lights_Zone2 <- st_intersection(lights.shp, Zone2.shp)
streets_Zone2 <- st_intersection(streets.shp, Zone2.shp)
tmap_mode("view")
tm_basemap(leaflet::providers$OpenStreetMap) +
tm_shape(Zone2.shp) +
tm_dots(col = "#1FC17B", alpha = 0.7) +
tm_borders(col = "#808080") +
tm_shape(streets_Zone2) +
tm_fill(col = "#1FC17B", alpha = 0.7) +
tm_borders(col = "#808080") +
tm_shape(lights_Zone2) +
tm_fill(col = "#1FC17B", alpha = 0.7) +
tm_borders(col = "#808080")
and so on...
another thing I tried was filtering instead of the st_intersection
but it would be the same
eventually y came up with the code
```{r echo=FALSE, message=FALSE, warning=FALSE, results='asis'}
anexos <- as.list(Zonas.shp$NOMBRE)
for (k in 1:length(anexos)) {
zonas <- paste0("Zona_", anexos[k])
wd <- anexos[k]
assign(Zonas, subset(Zonas.shp, Zonas.shp$NOMBRE == wd))
ligths <- paste0("ligths_", anexos[k])
assign(ligths, raster::intersect(paste0("ligths_", wd), ligths.shp))
cat(paste0("\n\n### ", wd, "\n\n"))
map <- tm_basemap(leaflet::providers$OpenStreetMap) +
tmap_options(check.and.fix = TRUE) +
tm_shape(wd) +
tm_fill(col = "#1FC17B", alpha = 0.7) +
tm_borders(col = "#808080")
tm_shape(ligths) +
tm_dots(col = "#1FC17B", alpha = 0.7) +
map
cat("\n\n\\newpage")
}
but even when it generates de headings it does not print the map