I'm creating an interactive map of the Basque Country (in Spain) using tmap, leaflet and shiny. I've been able to create the map, but I cannot edit the default behavior of renderLeaflet, specifically:
I'd like to edit the default labels and popups. Ideally I'd like no popups (on click), only labels (on mouseover); the label would contain info currently shown in the popup, plus some additional info, and appear on the top right corner of the map instead of on the area (something like this https://leafletjs.com/examples/choropleth/)
I'd like to remove the leaflet tiles so that only Esri.WorldGrayCanvas is shown, and therefore, there should be no option to switch between tiles.
The following code shows what I'm talking about. The required shape file can be downloaded here ftp://ftp.geo.euskadi.net/cartografia/Limites/ or here https://www.dropbox.com/s/zx3bg5131pomyfy/CB_COMARCAS_5000_ETRS89.shp?dl=0
library(shiny)
library(shinyWidgets)
library(leaflet)
library(rgdal)
library(tmap)
library(tmaptools)
library(dplyr)
setwd(~)
shape <- read_shape(file="CB_COMARCAS_5000_ETRS89.shp") # Load shape file
shape$COMARCA <- as.character(shape$COMARCA)
shape$COMARCA <- c(# Vector with names of the areas
"Alto Deba", "Arratia-Nervión", "Bajo Bidasoa", "Bajo Deba", "Cantábrica Alavesa",
"Donostialdea", "Duranguesado", "Encartaciones", "Estribaciones del Gorbea",
"Gernika-Bermeo", "Goierri", "Gran Bilbao", "Llanada Alavesa", "Markina-Ondarroa",
"Montaña Alavesa", "Plentzia-Mungia", "Rioja Alavesa", "Tolosa", "Urola Costa", "Valles Alaveses")
datawvals$COMARCA <- c(# Vector with names of the areas
"Alto Deba", "Arratia-Nervión", "Bajo Bidasoa", "Bajo Deba", "Cantábrica Alavesa",
"Donostialdea", "Duranguesado", "Encartaciones", "Estribaciones del Gorbea",
"Gernika-Bermeo", "Goierri", "Gran Bilbao", "Llanada Alavesa", "Markina-Ondarroa",
"Montaña Alavesa", "Plentzia-Mungia", "Rioja Alavesa", "Tolosa", "Urola Costa", "Valles Alaveses")
datawvals <- bind_rows(datawvals, datawvals, datawvals)
datawvals$time <- c(rep("2010",20), rep("2014",20), rep("2016",20))
datawvals$var1 <- runif(60)
datawvals$var2 <- runif(60)
datawvals$var3 <- runif(60)
data4map <- left_join(shape, datawvals, by="COMARCA") #join shape with data to be depicted
years <- c(2010,2014,2016)
runApp(list(
ui = fluidPage(
titlePanel("Shiny tmap!"),
sidebarLayout(
sidebarPanel(
selectInput("var", label = "Variable", choices = c("var1", "var2", "var3"), selected = "var1"),
sliderTextInput(
inputId = "time", label = "year",
choices = years, selected = 2010,
grid = FALSE
)
),
mainPanel(
leafletOutput("map", width=600)
)
)
),
server = function(input, output) {
#Subset data based on year
subsetData <- reactive({
new_data <- data4map[data4map$time==input$time, ]
return(new_data)
})
#Different color palettes for diff vars
colores <- reactive({
if(input$var=="var1"){
colores0 <- "Blues"
} else{
colores0 <- "Reds"
}
return(colores0)
})
#Actual plot
output$map = renderLeaflet({
tm <- tm_shape(subsetData()) +
tm_polygons(input$var, palette=colores())
tmap_leaflet(tm)
})
}
))
Thanks in advance for any tips and suggestions on how to edit these defaults.