firmo23
firmo23

Reputation: 8404

Combine shiny with tmap to display interactive map

I have the dataframe below for which I want to create a choropleth map. I downloaded the germany shapefile from here and now Im trying to display it via shiny app using tmap interactive version. Is this possible? Any other interactive solutions via shiny?

library(tidyverse)
library(sf)
library(shiny)
library(ggplot2)
library(tmap)

region<-c("09366", 
          "94130", 
          "02627", 
          "95336", 
          "08525", 
          "92637", 
          "95138", 
          "74177", 
          "08606", 
          "94152" )


value<-c( 39.5, 
          519.,  
          5.67,
          5.10,
          5.08,
          1165,  
          342,  
          775,  
          3532,  
          61.1 )

df<-data.frame(region,value)

ui <- fluidPage(
  plotOutput("map")
)

server <- function(input, output) {
  germany_sf <- sf::st_read(dsn = "plz-gebiete.shp") %>% 
    left_join(df, by = c("plz" = "region"))
output$map<-renderPlot({
  tmap_mode("view")
  
  tm_shape(shp = germany_sf) + 
    tm_polygons(col = "value", border.alpha = 0)
})  
  
  
}

shinyApp(ui, server)

Upvotes: 0

Views: 464

Answers (1)

Martijn Tennekes
Martijn Tennekes

Reputation: 2051

Use tmapOutput and renderTmap instead of plotOutput and renderPlot.

Upvotes: 1

Related Questions