我正在尝试使用“ mapview”和“ purrr”在交互式地图上绘制空间数据。在.rmd中使用Rstudio时,这些地图可见,但在编织html时,这些地图不可见。 ggplot2支持这种在rmd中显示数据的方式,因此我认为以html格式导出地图会很有用。
样本数据:
library(mapview)
library(dplyr)
library(purrr)
df <- data.frame(lon = 1:9,
lat = 1:9,
id = c(rep(1,5), rep(2,4))) %>%
st_as_sf(coords = c("lon", "lat"), crs = 4326)
# split dataframe in multiple datasets (to produce multiple plots for each group of data)
df <- split(df, df$id)
当尝试使用“ purrr”和“ mapview”在html中绘图时:
df %>% map(mapview)
html中的输出:
答案 0 :(得分:2)
您可以使用htmltools :: tagList(),请参见:How to render leaflet-maps in loops in RMDs with knitr
如果要使用tidyverse:
---
title: "R Notebook"
output:
html_document
---
```{r}
library('tidyverse')
library('sf')
library('mapview')
library('htmltools')
# create data set
df = data.frame(lon = 1:9,
lat = 1:9,
id = c(rep(1,5), rep(2,4))) %>%
st_as_sf(coords = c("lon", "lat"), crs = 4326)
# split dataframe in multiple datasets (to produce multiple plots for each group of data)
df = split(df, df$id)
# create maps
df_maps = df %>%
purrr::set_names() %>%
map(.x = .,
.f = mapview) %>%
map(.x = ., slot, name = "map")
# add html headers
df_maps =
imap(.x = df_maps,
.f = function(x, y) {
list(h4(paste("Subset:", y)),
x)
}) %>%
flatten()
# for printing the maps
tagList(df_maps)
```