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

问题描述 投票:0回答:2

我使用 tmap 创建的交互式地图确实显示在 RStudio 查看器中,但是,如果我尝试将其包含在我的 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)
r shiny r-leaflet tmap
2个回答
2
投票

您可以使用 tmap 函数结合以下代码来绘制多面图。 生成的地图如下所示:https://i.sstatic.net/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
投票

通过一些虚拟数据,我可以看到地图。

我不确定你是否可以加载这样的包:

library(c(tmap,tmaptools,leaflet))
它给了我这个错误:

库中的错误(c(tmap,tmaptools,传单)):“包”混乱 兰格 1 哈本

我不认为有一个叫做

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)
© www.soinside.com 2019 - 2024. All rights reserved.