【问题标题】:Interactive map with tmap does not appear in Shiny app, but does show in Rstudio viewer带有 tmap 的交互式地图不会出现在 Shiny 应用程序中,但会显示在 Rstudio 查看器中
【发布时间】:2019-01-07 15:50:29
【问题描述】:

我使用 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 leaflet maps tmap


【解决方案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"))

【讨论】:

    【解决方案2】:

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

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

    库中的错误(c(tmap,tmaptools,传单)):'package'muss Länge 1 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)
    

    【讨论】:

    • 非常感谢您的回复!如果没有这些方面,它也可以很好地处理原始数据:)。仍然想知道为什么刻面不可能?至于图书馆,那是一个错字。我确实将它们分别加载到我的文件中。请注意 view.legend.position 最近已经替换了 tm_view 函数中的 legend.position (rdocumentation.org/packages/tmap/versions/2.0/topics/tm_view)。
    • 正确,我仍在使用 tmap 1.11。我不知道它将如何在地图上放置不同的方面。你知道在 tmap 上使用 facets 的任何例子吗?
    猜你喜欢
    • 2019-05-24
    • 1970-01-01
    • 2010-11-19
    • 2017-05-18
    • 2021-06-14
    • 2019-12-20
    • 1970-01-01
    • 1970-01-01
    • 2011-12-30
    相关资源
    最近更新 更多