我已经使用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文档,其中包含三个选项卡,每个选项卡上都有一个Leaflet映射。
答案 0 :(得分:1)
这是我第一次发布Stack Overflow的答案,如果格式不正确,我们深表歉意!
我遇到了与您完全相同的问题。我似乎已经通过从选项卡2和选项卡3的地图代码中删除了leaflet.extras
包函数addResetMapButton()
来解决了您的代码中的问题。您可以将其保留在选项卡1上。
我没有深入研究导致这种情况的幕后情况。如果您的代码中包含addResetMapButton()
,则第三个地图选项卡上将显示一个Uncaught Error: Set map center and zoom first.
的javascript错误。希望精通Java的人可以解释addResetMapButton()
和地图标签之间发生了什么,从而导致此错误。
答案 1 :(得分:1)
感谢其他论坛的支持,此处分享了一个解决方案:https://github.com/rstudio/leaflet/issues/623