I'm trying to create an interactive map that allows the user to edit the atributes of a selected polygon in the viewer panel. Is there a way to do so?
I found this answered question that matches exactly my task, but can't be applied to R.
The following code reproduces my problem:
library(rgdal)
library(leaflet)
#Download the shapefiles
download.file("http://thematicmapping.org/downloads/TM_WORLD_BORDERS_SIMPL-0.3.zip" ,destfile="world_shape_file.zip")
system("unzip world_shape_file.zip")
#Read the data
world_spdf=readOGR( dsn= getwd() , layer="TM_WORLD_BORDERS_SIMPL-0.3")
#Create the objective vector with random values
world_spdf$year <- factor(sample(c(NA,2020,2021,2022),nrow(world_spdf), replace = TRUE))
#Labels to be shown when mouseover
labels=paste("Country: ", world_spdf@data$NAME,"<br/>", "Year: ", world_spdf$year, sep="") %>%
lapply(htmltools::HTML)
#Color legend
pal <- colorFactor(palette = c('yellow','green','blue'),
levels = levels(world_spdf$year))
#Show map
leaflet(world_spdf) %>%
addTiles() %>%
setView( lat=10, lng=0 , zoom=2) %>%
addPolygons(fillColor=~pal(year),stroke=FALSE,
highlightOptions = highlightOptions(weight=5,fillOpacity = 0.3,bringToFront = TRUE),
label=labels)%>%
addLegend(pal=pal,values=levels(world_spdf$year),
title = "Travel year", position = "bottomright" )
What I want to do, is allow the user to edit the value in world_spdf$year that corresponds to the selected feature in the map.
Actually, i'm using the following code to change the value manually:
change_to_na <- c('COUNTRY NAME')#Vector of countries whose $year will be set to NA
change_to_2020 <- c('COUNTRY NAME')#Vector of countries whose $year will be set to 2020
change_to_2021 <- c('COUNTRY NAME')#Vector of countries whose $year will be set to 2021
change_to_2022 <- c('COUNTRY NAME')#Vector of countries whose $year will be set to 2022
world_spdf$year[which(world_spdf$NAME%in%change_to_na)] <- NA
world_spdf$year[which(world_spdf$NAME%in%change_to_2020)] <- 2020
world_spdf$year[which(world_spdf$NAME%in%change_to_2021)] <- 2021
world_spdf$year[which(world_spdf$NAME%in%change_to_2022)] <- 2022
Using this code, I only need to fill the vectors with the country names, matching the names given in world_spdf$NAME. Now i'm looking forward to make things easier, without lines of code. Any help will be appreciated.