在 R 中的 reveal.js 演示文稿中看不到传单地图图块?

Leaflet map tiles not visible in reveal.js presentation within R?

简介:

我正在尝试将传单地图嵌入到 RMarkdown 文件中的 revealjs 演示文稿中。我下面的示例非常接近,但它是 (1) 缺少图块,(2) 弹出窗口不显示,以及 (3) 图例和字体太大了!

我不太关心代码块目前的样子。我计划为我的最终产品使用 results = "hide" 幻灯片选项。

提前致谢!

可重现示例:

---
title: "My Presentation"
author: Me
date: 2017-06-23
output:
    revealjs::revealjs_presentation:
        theme: black

---

## Loading in necessary packages:
```{r}
library(dplyr)
library(sp)
library(rgdal)
library(rgeos)
library(RColorBrewer)
library(classInt)
library(leaflet)
library(htmlwidgets)
```

## Defining our data:
```{r}
lat <- c(45.51158000, 45.50431159, 45.496539)
lon <- c(-122.548056, -122.54775, -122.54788)
no2 <- c(17.37, 25.61, 24.69)

dta <- data.frame(lat, lon, no2)
colnames(dta) <- c("lat","lon","no2")
```

## Create layer of spatial points:
```{r}
points <- SpatialPointsDataFrame(data.frame(x=dta$lon, y=dta$lat), data = data.frame(dta$no2))

plotclr <- (brewer.pal(7, "RdYlGn"))
class <- classIntervals(dta$no2, n = 7, style = "fixed", fixedBreaks = c(0,5,10,15,20,25,30))
colcode <- findColours(class, rev(plotclr))
plot(points, col=colcode, pch=19)

pop1<-paste0("<b>NO2:</b> ", dta$no2, " ppb", 
         "<br /> <b>Lat:</b> ", dta$lat, 
         "<br /> <b>Lon:</b> ", dta$lon) 
```

## Creating the leaflet map:
```{r}
no2_map <-leaflet()%>%
  addTiles('http://{s}.tiles.wmflabs.org/bw-mapnik/{z}/{x}/{y}.png') %>%
  addCircleMarkers(data=points, color = "black", radius = dta$no2, fillColor = colcode, fillOpacity=0.7, weight=1, popup=pop1) %>%
  addLegend(position = "bottomright", colors = rev(plotclr), labels = rev(c("30","25","20","15","10","5","0")), opacity = 0.9, title = "NO2 (ppb)")
```

---
```{r}
no2_map

saveWidget(no2_map, file="map.html")

```

不幸的是,reveal.js 和 Leaflet 不能很好地协同工作,并且您的地图幻灯片可能缺少图层。这是因为 Leaflet 无法辨别用作地图容器的 DOM 元素的大小,因为 reveal.js 动态调整所有元素的大小。

最简单的解决方法是当您在带有 Leaflet 地图的幻灯片中时刷新页面。您还可以尝试延迟调用 map.invalidateSize()(通过在 Javascript 中使用 setTimeout()