【问题标题】:Leaflet maps not appearing in tabbed HTML document output from Rmarkdown传单地图未出现在 Rmarkdown 的选项卡式 HTML 文档输出中
【发布时间】:2019-05-06 07:18:45
【问题描述】:

我使用 R Markdown 创建了一个带有选项卡部分的 HTML 文档,每个选项卡都应该包含一个 Leaflet 地图。当我运行代码时,Leaflet 地图仅出现在 HTML 文档的前两个选项卡中,而不会出现在任何后续选项卡中。以下是我尝试过的快速总结:

  • 我曾尝试在 RStudio Viewer、Chrome 和 Firefox 中查看 HTML 文档,但结果都相同(即没有从第三个选项卡开始的地图)。当我打开浏览器控制台时,出现错误,显示“错误:首先设置地图中心并缩放”。但是,我在 Leaflet 中使用 setView() 设置了地图中心和缩放。

  • 当我在 Markdown 文件或 R 控制台中独立运行代码块时,它们可以完美地生成所需的地图。

  • 当我从 Markdown 文件中删除选项卡式部分时,所有 Leaflet 地图都出现在各自的标题下。

  • 标签的顺序无关紧要,地图总是出现在前两个标签上,不会出现在后面的标签上(即在示例中,如果将east_map块移动到开头,则不会出现地图为 cent_state)。

  • 我有意将完整的代码集添加到每个代码块中,并重命名每个过滤迭代,以确保我不会错过从全局环境中提取某些内容。

  • 我也尝试过使用leafletOutput() 和renderLeaflet(),但无济于事。但我认为这无关紧要,因为它更适用于 Shiny 集成。

  • 我更新了 R (3.6.0)、RStudio (1.2.1335)、pandoc (2.7.2) 和所有相关包。

我已经搜索过 StackOverview,但完全没有想法,如果有任何想法或指导,我将不胜感激。

在我的文档中,我有 10 个选项卡,但为简洁起见,我在下面创建了一个示例,其中只有 3 个选项卡可以重现我的问题。 (另外,我已经从我的完整 Markdown 文件中删除了很多代码,以使其尽可能简单,所以如果有一些挥之不去的包等,请道歉。)

---
title: "REPRODUCIBLE EXAMPLE <br> April 2019"
always_allow_html: yes
output:
  html_document:
    df_print: paged
---


# {.tabset .tabset-fade}

## TAB 1

### HEADING 1

```{r natl_map, echo = FALSE, message=FALSE, warning=FALSE, comment=NA, out.width='100%'}
setwd()
library(tidyverse)
library(leaflet)
library(leaflet.extras)
library(htmltools)
library(htmlwidgets)
library(shiny)
library(rgdal)

dat <- tibble(
  state = c("lak", "cent", "east", "east"),
  org = c("abc", "cbs", "fox", "abc"),
  item = c("pump 1", "pump 2", "pump 3", "pump 4"),
  status = c("terrible", "poor", "good", "excellent"),
  lat = c(6.87239, 4.01313, 5.00959, 4.77239),
  lon = c(29.57524, 30.56462, 32.39547,     33.59156)
)

dat$status <- factor(dat$status, levels = c("terrible", "poor", "good", "excellent"))

#Set color pallette for by status
pal <- colorFactor(palette = c("#FF0000", "yellow", "#2cb42c", "blue"),
                   levels = levels(dat$status))

#Create factor layers by facility_type
abc_data <- dat %>%
  filter(org == "abc")

cbs_data <- dat %>%
  filter(org == "cbs")

fox_data <- dat %>%
  filter(org == "fox")

ssd_map <- leaflet() %>%
  addProviderTiles("OpenStreetMap.BlackAndWhite") %>%
  setView(lng = 30.2189853, lat = 7.1751893, zoom = 7) %>%
  setMaxBounds(
    lng1 = 22.625227,
    lat1 = 1.422041, 
    lng2 = 36.978083,
    lat2 = 13.528717 
  )

natl_map <- ssd_map %>% 
  clearMarkers() %>%
  addCircleMarkers(data =   abc_data,
                   color = ~pal(status),
                   radius   =   2,
                   group = "abc") %>%
  addCircleMarkers(data =   cbs_data,
                   color = ~pal(status),
                   radius   =   2,
                   group = "cbs") %>%
  addCircleMarkers(data =   fox_data,
                   color = ~pal(status),
                   radius   =   2,
                   group = "fox") %>%
  addLayersControl(overlayGroups = c("abc",
                                     "cbs",
                                     "fox")) %>%
  addLegend(position = "topright",
            pal = pal,
            title = "Status",
            values = dat$status) %>%
  addResetMapButton()

natl_map

```

## TAB 2

### HEADING 2.1

```{r cent_map, echo = FALSE, message=FALSE, warning=FALSE, comment=NA, out.width='100%'}
setwd()
library(tidyverse)
library(leaflet)
library(leaflet.extras)
library(htmltools)
library(htmlwidgets)
library(shiny)
library(rgdal)

dat <- tibble(
  state = c("lak", "cent", "east", "east"),
  org = c("abc", "cbs", "fox", "abc"),
  item = c("pump 1", "pump 2", "pump 3", "pump 4"),
  status = c("terrible", "poor", "good", "excellent"),
  lat = c(6.87239, 4.01313, 5.00959, 4.77239),
  lon = c(29.57524, 30.56462, 32.39547,     33.59156)
)

dat$status <- factor(dat$status, levels = c("terrible", "poor", "good", "excellent"))

#Set color pallette for by status
pal <- colorFactor(palette = c("#FF0000", "yellow", "#2cb42c", "blue"),
                   levels = levels(dat$status))

cent_dat <- dat %>%
  filter(state == "cent")

#Create factor layers by facility_type
cent_abc_data <- cent_dat %>%
  filter(org == "abc")

cent_cbs_data <- cent_dat %>%
  filter(org == "cbs")

cent_fox_data <- cent_dat %>%
  filter(org == "fox")


cent_map <- leaflet() %>%
  addProviderTiles("OpenStreetMap.BlackAndWhite") %>%
  setView(lng = 31.3222933, lat = 4.734494, zoom = 8) %>%
  setMaxBounds(
    lng1 = 32.149583,
    lat1 = 6.259701, 
    lng2 = 29.753375,
    lat2 = 3.501536 
  )

cent_map <- cent_map %>% 
  clearMarkers() %>%
  addCircleMarkers(data =   cent_abc_data,
                   color = ~pal(status),
                   radius   =   2,
                   group = "abc") %>%
  addCircleMarkers(data =   cent_cbs_data,
                   color = ~pal(status),
                   radius   =   2,
                   group = "cbs") %>%
  addCircleMarkers(data =   cent_fox_data,
                   color = ~pal(status),
                   radius   =   2,
                   group = "fox") %>%
  addLayersControl(overlayGroups = c("abc",
                                     "cbs",
                                     "fox")) %>%
  addLegend(position = "topright",
            pal = pal,
            title = "Status",
            values = dat$status) %>%
  addResetMapButton()

cent_map

```

## TAB 3

### HEADING 3

```{r east_map, echo = FALSE, message=FALSE, warning=FALSE, comment=NA, out.width='100%'}
setwd()
library(tidyverse)
library(leaflet)
library(leaflet.extras)
library(htmltools)
library(htmlwidgets)
library(shiny)
library(rgdal)

dat <- tibble(
  state = c("lak", "cent", "east", "east"),
  org = c("abc", "cbs", "fox", "abc"),
  item = c("pump 1", "pump 2", "pump 3", "pump 4"),
  status = c("terrible", "poor", "good", "excellent"),
  lat = c(6.87239, 4.01313, 5.00959, 4.77239),
  lon = c(29.57524, 30.56462, 32.39547,     33.59156)
)

dat$status <- factor(dat$status, levels = c("terrible", "poor", "good", "excellent"))

#Set color pallette for by status
pal <- colorFactor(palette = c("#FF0000", "yellow", "#2cb42c", "blue"),
                   levels = levels(dat$status))

east_dat <- dat %>%
  filter(state == "east")

#Create factor layers by facility_type
east_abc_data <- east_dat %>%
  filter(org == "abc")

east_cbs_data <- east_dat %>%
  filter(org == "cbs")

east_fox_data <- east_dat %>%
  filter(org == "fox")

east_map <- leaflet() %>%
  addProviderTiles("OpenStreetMap.BlackAndWhite") %>%
  setView(lng = 33.731222, lat = 5.084033, zoom = 8) %>%
  setMaxBounds(
    lng1 = 35.964000,
    lat1 = 3.556817, 
    lng2 = 31.597152,
    lat2 = 5.906979  
  )

east_map <- east_map %>% 
  clearMarkers() %>%
  addCircleMarkers(data =   east_abc_data,
                   color = ~pal(status),
                   radius   =   2,
                   group = "abc") %>%
  addCircleMarkers(data =   east_cbs_data,
                   color = ~pal(status),
                   radius   =   2,
                   group = "cbs") %>%
  addCircleMarkers(data =   east_fox_data,
                   color = ~pal(status),
                   radius   =   2,
                   group = "fox") %>%
  addLayersControl(overlayGroups = c("abc",
                                     "cbs",
                                     "fox")) %>%
  addLegend(position = "topright",
            pal = pal,
            title = "Status",
            values = dat$status) %>%
  addResetMapButton()

east_map

```

预期的输出是一个包含三个选项卡的 HTML 文档,每个选项卡上都有一个传单地图。

【问题讨论】:

  • 请尝试执行您的传单的print
  • 谢谢,@Clemsang。最后尝试了print(east_map),但得到了相同的不良结果和错误消息。
  • 解决方案在这里:github.com/rstudio/leaflet/issues/623

标签: javascript html r leaflet r-markdown


【解决方案1】:

这是我第一次在 Stack Overflow 上发布答案,如果格式不正确,敬请见谅!

我遇到了和你一样的问题。我似乎通过从标签 2 和标签 3 上的地图代码中删除 leaflet.extras 包函数 addResetMapButton() 解决了您代码中的问题。您可以将其保留在标签 1 上。

我没有深入研究导致这种情况的幕后情况。当addResetMapButton() 包含在您的代码中时,第三个地图选项卡上会出现一个javascript 错误,显示Uncaught Error: Set map center and zoom first.。希望精通 Javascript 的人可以解释 addResetMapButton() 和地图选项卡之间发生了什么导致此错误。

【讨论】:

【解决方案2】:

感谢其他论坛的一些支持,这里分享了一个解决方案:https://github.com/rstudio/leaflet/issues/623

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-04
    • 2013-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多