tmap in R Shiny based on user input

I am trying to produce a reactive NZ map (using tmap package) based on selectInput. Is it possible to have the border around the selected input region filled/highlighted as shown?

.

The data used is nz data obtained from the spdata package. I prefer using this data over GADM data because the nz data has the 16 regions that I want to use for the application's purpose. I am still very new to mapping in general and mapping in R shiny in particular so any tips would be greatly appreciated.

Global

library(shiny)
library(shinydashboard)
library(tmap)
library(spData)

# data used for adding name to each region as shown on map
points <- data.frame(name = c("Northland", "Auckland", "Waikato", "Bay of Plenty", "Gisborne",
                              "Hawke's Bay", "Taranaki", "Manawatū-Whanganui", "Wellington", "Tasman",
                              "Nelson", "Marlborough", "West Coast", "Cantebury", "Otago", "Southland"),
                     x = c(173.7624, 174.7633, 175.1894, 176.9128, 178.0,
                           176.7416, 174.4384, 175.01667, 176.1286, 172.7347,
                           173.28401, 173.5, 171.2281, 171.5, 170.51107, 168.0),
                     y = c(-35.5795, -36.8485, -37.4558, -38.1648, -38.65,
                           -39.109, -39.3538, -39.95, -41.08777, -41.2122,
                           -41.27089, -41.66667, -42.45658, -43.5, -45.88445, -45.66667),
                     stringsAsFactors = F)
points <- st_as_sf(points, coords = c("x", "y"), crs = 4326)

ui.R

ui <- dashboardPage(
  dashboardHeader(title = "NZ Map"),
  dashboardSidebar(uiOutput(outputId = "NZ")
),
  dashboardBody(tmapOutput(outputId = "my_tmap", width = "50%"))
)

server.R

server <- function(input, output, session){
  output$NZ <- renderUI({
    selectInput(inputId = "region", label = "Region", choices = nz$Name)
  })
  
  output$my_tmap <- renderTmap({
    tm_shape(nz) +
      tm_borders(lty = "solid", col = "khaki") +
      tm_polygons(col = "lightblue") +
      tm_shape(points) + tm_text("name") 
  })
  
filtered <- reactive({
    nz[nz$Name == input$region,]
  })

  mymap_proxy <- tmapProxy(mapId = my_tmap)
  
  # observe({
    # filtered <- filtered()
    # mymap_proxy 
  # })
}

shinyApp(ui, server)

References :







This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.