I'm trying to integrate a tmap into a shiny app and doing so I have encountered a reactivity problem. I get an error implying that the function renderTmap() does not create a reactive environment (while "classic" renderXXX() functions do).
Here is the error message I get:
Error in .getReactiveEnvironment()$currentContext() : Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)
Hence I haven't managed to create a map taking into account input values...
Here is a simple example of what I'm trying to do (which only works if I don't update data_map
based on input values!)
library(shiny)
library(sf)
library(tmap)
nc=st_read(system.file("shape/nc.shp", package="sf"))
# Define UI for application that draws a histogram
ui=fluidPage(
selectInput("name",
"name",
unique(nc$NAME)),
tmapOutput("map"))
)
server=function(input, output) {
output$map <- renderTmap({
data_map <- subset(nc,NAME==input$name)
tm_shape(data_map)+
tm_borders()
})
}
shinyApp(ui=ui,server=server)
Does anybody have an explanation/a workaround to this problem?
P.S.: I'm really set on renderTmap rather than renderLeaflet: I'm trying to teach shiny to students that have worked with tmap so far...