Not entirely sure about your first question, as I'm not aware of any way that can be assigned using mapview()
. However, here is a reproducible solution using addPolygons()
:
library(dplyr)
library(shiny)
library(leaflet)
library(leaflet.extras)
library(rgdal)
library(sp)
library(tigris)
library(htmltools)
setwd(dirname(rstudioapi::getActiveDocumentContext()$path)) # set your working directory
philly <- tracts(state = 'PA', county = c('Philadelphia'))
ui <- fluidPage(
title = "Test Map",
leafletOutput("mymap", width = 600)
)
server <- function(input, output, session) {
RV <- reactiveValues(Clicks=list()) # used for storing leaflet variables
tract_labels <- sprintf( # labels for mouseover tooltip
"<strong>%s</strong>, <strong>%s</strong>
<br/><b>Land Area:</b> %s",
philly$COUNTYFP,
philly$STATEFP,
philly$ALAND
) %>% lapply(htmltools::HTML)
output$mymap <- renderLeaflet({ # leaflet map
leaflet(data = philly) %>%
setView(-75.16, 39.9523, zoom = 10) %>%
addTiles(urlTemplate = "https://{s}.tile.openstreetmap.se/hydda/full/{z}/{x}/{y}.png",
attribution = NULL) %>%
addPolygons(data = philly,
layerId = philly@data$ALAND,
group = "regions",
fillColor = "#bdd7e7",
weight = 1,
opacity = 1.0,
fillOpacity = 0.5,
smoothFactor = 0.5,
label = tract_labels,
labelOptions = labelOptions(
style = list("font-weight" = "normal", padding = "3px 8px"),
textsize = "12px",
direction = "auto"),
highlightOptions = highlightOptions(color = "white",
weight = 2,
bringToFront = TRUE))
})
observeEvent({input$mymap_shape_click}, {
#create object for clicked polygon
click <- input$mymap_shape_click
RV$Clicks <- c(RV$Clicks,click$id)
#define leaflet proxy for second regional level map
proxy <- leafletProxy("mymap")
#subset regions shapefile by the clicked on polygons
selectedReg <- philly[philly@data$ALAND == click$id,]
#map clicked on polygons
proxy %>% addPolygons(data = selectedReg,
fillColor = "red",
fillOpacity = 1,
weight = 1,
color = "black",
stroke = T,
group = "selected",
layerId = selectedReg@data$ALAND)
# remove polygon group that are clicked twice
if(click$group == "selected"){
proxy %>%
clearGroup(group = "selected")
RV$Clicks <- 0 # resets values if polygons are clicked twice
}
mean.land <- mean(as.numeric(RV$Clicks)) # stores the values of polygons that are clicked
print(mean.land)
})
}
shinyApp(ui, server)
Essentially the map has two layers: the base tracts layer, and another tracts polygon that highlights what you click. You can click on each polygon to 'retrieve' a value (in this case it's land area, or variable ALAND) from each polygon and make calculations on it. Here I have selected three polygons, and I have used mean.land
variable to display the average land area of all three.
The reactiveValues RV
object is used to store a numeric value of the layerId
variable on any polygon that you click. This allows you to store and 'retrieve' it for other calculations you may want to make.
[1] 717210 # first click, first value
[1] 571940 # second click, averaged value
[1] 488678.3 # third click, averaged value
You can change extract the layerId
attribute by changing any references to variable ALAND in the code.