I'm trying to generate tmaps with a for loop inside an rmarkdown file. They will not show in the html output. I read elsewhere using the tagList function could help but in my case that did not and returned an error (i'm probably doing it wrong). Any ideas?
This attempt with tmap does not work. I'm getting this error: "Error in as.character.default(x) : no method for coercing this S4 class to a vector"
html <- list()
for (i in 1:4) {
tmap_mode("view")
html <- c(html,
list(h3(paste0("Carte_Dept_", i)),
tm_basemap(leaflet::providers$Stamen.Toner) +
tm_shape(communes_dept_pct) +
tm_fill("pct",
palette = "-RdYlGn",
style ="quantile",
title ="% bât. FG",
alpha = 0.6,
popup.vars = c("Commune:" = "nom","% bât. FG:" = "pct"),
group = "Communes") +
tm_borders(col = "black", lwd = 0.5) +
tm_shape(spdf) +
tm_dots(size = 0.05,
col = "red",
title = "Bâtiments FG",
cluster = T,
popup.vars = c("Type bâtiment:" = "tr002_type_batiment_description","Classe GES: " = "classe_estimation_ges",
"Nom méthode:" = "nom_methode_dpe"),
group = "Bâtiments F-G") +
tm_shape(spdf2) +
tm_dots(size = 0.05,
shape = 21,
col = "Domaine de travaux",
title = "Artisans RGE",
legend.show = F,
cluster = F,
popup.vars = c("Nom:" = "Nom de l'entreprise","Adresse: " = "Adresse", "Ville:" = "Ville",
"Domaine:" = "Domaine de travaux", "Effectif:" = "Tranche d'effectif salarié de l'établissement (INSEE)"),
group = "Artisans RGE")
)
)
}
tagList(html)
This does work however, using the most simple leaflet map:
html <- list()
for (i in 1:4) {
html <- c(html,
list(h3(paste0("Carte_Dept_", i)),
leaflet(communes_Occitanie_pct) %>% addProviderTiles('Stamen.Toner') %>%
setView(lng = 1.54, lat = 43.7,
zoom = 7)
)
)
}
tagList(html)
I'd like to use tmap though, so any suggestion could help...