带有tmap的交互式地图不会出现在Shiny应用程序中,但会在Rstudio查看器中显示

时间:2018-07-31 09:34:48

标签: r shiny leaflet maps tmap

我用tmap创建的交互式地图确实显示在RStudio Viewer中,但是,如果我尝试将其包含在Shiny应用程序中,则不会出现。我使用了tmap_leaflet函数,renderLeaflet和leafletOutput。有人知道如何解决这个问题吗?

下面是我在Shiny应用程序中使用的示例代码:

  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)

2 个答案:

答案 0 :(得分:1)

您可以使用tmap函数与以下代码一起绘制多面地图。 生成的地图如下所示:https://i.stack.imgur.com/9gmJf.jpg

library(tidyverse)
library(tmap)
library(tmaptools)
library(rgdal)
library(dplyr)
library(leaflet)

# Loading shape and select four largest cities in the Netherlands
country  <- read_shape("/local_directory/wijk_2017.shp")

# Cleaning and preparing data
cities   <- country %>% 
            filter(GM_CODE %in% c("GM0363","GM0599","GM0518","GM0344"),
                  !WK_CODE %in% c("WK036399","WK059999","WK051899","WK034499"))
cities   <- cities %>%
            mutate(WK_NAAM = as.character(WK_NAAM),
                 OPP_TOT = as.numeric(as.character(OPP_TOT)),
                 OPP_TOT = cut(OPP_TOT,breaks = c(-Inf,seq(500,2000,by=500),Inf)))

# Plotting facetted tmap of four largest cities in the Netherlands.
tmap_mode("view")
tm_shape(cities) +
  tm_polygons() +
  tm_facets(by = "GM_NAAM",
            nrow = 2, ncol = 2) +
  tm_shape(cities) +
  tm_fill(col = "OPP_TOT",
          legend.show = T, 
          colorNA = "grey",
          title = "Total area per neighbourhood",
          textNA = "No data",
          palette = "Greens",n=5,
          id = "WK_NAAM") +
  tm_shape(cities) +
  tm_borders("white",alpha=.8, lwd=1.5) +
  tm_layout(outer.margins = 0) +
  tm_view(view.legend.position = c("left","bottom"))

答案 1 :(得分:0)

使用一些虚拟数据,我可以看到地图。

我不确定是否可以加载这样的软件包:library(c(tmap,tmaptools,leaflet)) 它给了我这个错误:

  

库中的错误(c(tmap,tmaptools,传单)):'package'muss   Länge1 haben

我不认为有一个名为view.legend.position的论点。仅尝试使用legend.position

我认为问题出在facets上。 (下面的示例不使用它们)

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)