简体   繁体   中英

Reactivity problem while using renderTmap within R/shiny

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?

PS: I'm really set on renderTmap rather than renderLeaflet: I'm trying to teach shiny to students that have worked with tmap so far...

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 .

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM