0

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

1 Answer 1

1

When you copy-paste code, as you mention, your goal is to iterate over different values of a variable in a for loop, as you state in the OP title; in R there are several ways to iterate, the example here uses lapply and for; to replicate as is, you should rbind your zone_x.shp files together:

library(tmap)
library(sf)

# call some sample data
data(World, metro, rivers)

# lets just do it for some countries in South America
latam = World[World$continent == "South America",]
latam = latam[1:5, ]

tmaps_list = lapply(latam$name,
              function(x){ 
                latam_f = latam[latam$name == x,] 
                riv_f = st_intersection(rivers, latam_f)
                tm_shape(latam_f, is.master = T) + tm_polygons() +
                tm_shape(riv_f) + tm_lines()})

# check whether items on the list are maps:
class(tmaps_list[[1]])
# [1] tmap

# each item in the list is a tmap object;
# we save it with tmap_save
for(i in seq_along(tmaps_list)) { 
  tmap_save(tmaps_list[[i]], filename = paste0(latam$name[i], ".png") )
  
  }


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.