Reputation: 11
I'm learning shiny and tmap. I'm able to get the map I want on Rstudio (a static tmap). But when I try a similar tmap code with shiny, I always get an interactive map.
From this reprex, I try to get the same map I get on Rstudio.
I try to add tmap_mode("plot")
after the renderTmap
, but it doesn't work.
My question look too simple... but can't find the answer! Thank for your help.
library(shiny)
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({
tmap_mode("plot")
tm_shape(World) +
tm_polygons(world_vars[1], zindex = 401)
})
}
shinyApp(ui, server)
Upvotes: 1
Views: 732
Reputation: 31
tmap_mode()
doesn't work inside Shiny for me either, but there's actually a very easy alternative: instead of tmapOutput
and renderTmap
for interactive maps, you can just use plotOutput
and renderPlot
.
So the following code creates the exact map from your image and lets the user select a variable to plot:
library(shiny)
library(tmap)
data(World)
world_vars <- setdiff(names(World), c("iso_a3", "name", "sovereignt", "geometry"))
ui <- fluidPage(
plotOutput("map"),
selectInput("var", "Variable", world_vars)
)
server <- function(input, output, session) {
output$map <- renderPlot({
tm_shape(World) +
tm_polygons(input$var)
})
}
shinyApp(ui, server)
Upvotes: 3