4

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...

A. Suliman
  • 12,923
  • 5
  • 24
  • 37
lvaudor
  • 113
  • 5
  • `observe ({ output$map <- renderTmap({ data_map <- subset(nc,NAME==input$name) tm_shape(data_map)+ tm_borders() }) })` did the job, but frankly I don't know why, probably `renderTmap` doesn't work well when there is a dependency i.e. `input$name`, because it works fine with if we do `output$map <- renderTmap({ tm_shape(nc)+ tm_borders() })`. – A. Suliman Jan 08 '20 at 11:07
  • That's right, thanks! there is something close to this solution in the example associated to the renderTmap() function, although it uses a function tmapProxy() instead of renderTmap(). I'm still wondering why the need for an "observe()" though. I'll have a tough time explaining this to my students ;-). – lvaudor Jan 08 '20 at 13:20

1 Answers1

2

This issue was fixes on 2020-07-16. Now the code from your question should work without any problems.

You need to use the development version of the package for now:

remotes::install_github("mtennekes/tmaptools")
remotes::install_github("mtennekes/tmap")

Learn more at https://github.com/mtennekes/tmap/issues/474.

enter image description here

Jot eN
  • 6,120
  • 4
  • 40
  • 59