Skip to content

Instantly share code, notes, and snippets.

@z3tt
Created August 27, 2020 18:35
Show Gist options
  • Save z3tt/18e32d34bd82bb0e4f5ad2547a3e1795 to your computer and use it in GitHub Desktop.
Save z3tt/18e32d34bd82bb0e4f5ad2547a3e1795 to your computer and use it in GitHub Desktop.
tmap in shiny
library(shiny)
library(tmap)
data(World)
world_vars <- setdiff(names(World), c("iso_a3", "name", "sovereignt", "geometry"))
ui <- fluidPage(
tmapOutput("map"),
selectInput("var", "Variable", world_vars)
)
server <- function(input, output, session) {
output$map <- renderTmap({
tm_shape(World) +
tm_polygons(world_vars[1], zindex = 401)
})
observe({
var <- input$var
tmapProxy("map", session, {
tm_remove_layer(401) +
tm_shape(World) +
tm_polygons(var, zindex = 401)
})
})
}
shinyApp(ui, server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment