rshinyr-leaflettmap

Interactive map with tmap does not appear in Shiny app, but does show in Rstudio viewer


The interactive map I created with tmap does show up in the RStudio Viewer, however, if I try to include it in my Shiny application it does not appear. I've used the tmap_leaflet function, renderLeaflet and leafletOutput. Does anyone know how to solve this?

Below is an example code of what I used in my Shiny application:

  library(shiny)
  library(c(tmap,tmaptools,leaflet))

  ui <- fluidPage(
        leafletOutput("test"))

  server <- function(input, output) {

     output$test <- renderLeaflet({

     # data is a shapefile with geometry properties and a mapping variable and facet variable. 

     tmap_mode("view")   
     map  <- tm_shape(data) +
             tm_polygons() +
             tm_facets(by = "facet_variable",nrow = 2, ncol = 2) +
             tm_shape(data) +
             tm_fill(col = "mapping_variable",
                     legend.show = T, 
                     colorNA = "grey",
                     palette = "Reds",n=9) +
             tm_shape(data) +
             tm_borders("white",alpha=.8, lwd=1.5) +
             tm_layout(outer.margins = 0) +
             tm_view(view.legend.position = c("left","bottom"))
     tmap_leaflet(map,mode="view",show=T)
     })
    }

  shinyApp(ui = ui, server = server)

Solution

  • With some dummy data I am able to see the map.

    I am not sure if you can load packages like this : library(c(tmap,tmaptools,leaflet)) It gives me this error:

    Error in library(c(tmap, tmaptools, leaflet)) : 'package' muss Länge 1 haben

    And I dont think there is an argument called view.legend.position. Try with legend.position only.

    I think the problem comes with the facets. (Example below doesnt use them)

    library(shiny)
    library(tmap)
    library(tmaptools)
    library(leaflet)
    library(sp)
    
    Sr1 = Polygon(cbind(c(2,4,4,1,2),c(2,3,5,4,2)))
    Sr2 = Polygon(cbind(c(5,4,2,5),c(2,3,2,2)))
    Sr3 = Polygon(cbind(c(4,4,5,10,4),c(5,3,2,5,5)))
    Sr4 = Polygon(cbind(c(5,6,6,5,5),c(4,4,3,3,4)), hole = TRUE)
    
    Srs1 = Polygons(list(Sr1), "s1")
    Srs2 = Polygons(list(Sr2), "s2")
    Srs3 = Polygons(list(Sr3, Sr4), "s3/4")
    SpP = SpatialPolygons(list(Srs1,Srs2,Srs3), 1:3)
    
    grd <- GridTopology(c(1,1), c(1,1), c(10,10))
    polys <- as(grd, "SpatialPolygons")
    centroids <- coordinates(polys)
    x <- centroids[,1]
    y <- centroids[,2]
    z <- 1.4 + 0.1*x + 0.2*y + 0.002*x*x
    ex_1.7 <- SpatialPolygonsDataFrame(polys,
                                       data=data.frame(x=x, y=y, z=z, row.names=row.names(polys)))
    ex_1.7$face <- c(rep(1, 50), rep(2,50))
    
    
    ui <- fluidPage(
      leafletOutput("test"))
    
    server <- function(input, output) {
    
      output$test <- renderLeaflet({
    
        # data is a shapefile with geometry properties and a mapping variable and facet variable. 
    
        # tmap_mode("view")   
        map  <- tm_shape(ex_1.7) +
          tm_polygons() +
          # tm_facets(by = "facet_variable",nrow = 2, ncol = 2) +
          # tm_facets(by = "face", nrow = 2, ncol = 2) +
          tm_shape(ex_1.7) +
          # tm_fill(col = "mapping_variable",
          tm_fill(col = "y",
                  legend.show = T,
                  colorNA = "grey",
                  palette = "Reds",n=9) +
          tm_shape(ex_1.7) +
          tm_borders("white",alpha=.8, lwd=1.5) +
          tm_layout(outer.margins = 0) +
          tm_view(legend.position = c("left","bottom"))
    
    
        tmap_leaflet(map)
      })
    }
    
    shinyApp(ui = ui, server = server)