I've created a R markdown report where the sections and tabsets are dynamically created.
I have an issue where the Leaflet maps are not being generated in the output, but rather a blank white space. They do however render in R studio.
Here is an image showing how the output currently looks, and how it should look.
Here is the code i've tried:
{r, echo = FALSE, results='asis', warning=FALSE, message=FALSE}
for (estates in filtered_list){
map_data <- x_estates %>% filter(Street_Postcode == estates)
cat("###", estates, "{.tabset .tabset-fade .tabset-pills}", "\n")
cat("\n\n\n")
cat("This is where the map will go ")
# generate leaflet plot (doesn't even show white space if not stored in tagList)
page <- tagList(
leaflet() %>% addTiles() %>%
addMarkers(map_data$Longitude, map_data$Latitude)
)
### options i've tried for getting the Leaflet map to plot correctly
page
print(page)
print(page[[1]])
page[[1]]
print(
tagList(
page[[1]]
))
print(
tagList(
page
))
### end
cat("\n\n\n")
for (major in Major$Titles) {
cat("####", major, "\n")
cat("\n\n\n")
#####
rest of code
#####
}
}
It has something to do with html rendering I presume, as static plots i've generated using ggmap work; however I want my maps to be interactive.
N.B. The maps render fine in R studio, just not in the knitr html output.
EDIT
As suggested by Susan Switzer, I have made some changes to the code. The leaflet plots now show (with interactivity too). Can alter the code to dynamically name and load the correct HTML file.
m <- leaflet() %>% addTiles() %>%
addMarkers(map_data$Longitude, map_data$Latitude)
library(mapview)
m <- mapshot(m, url = paste0(getwd(), "/map.html"))
# m <- htmltools::includeHTML("map.html") produces an odd output where the entire document becomes a laggy leaflet map
# print(m)
m <- htmltools::tags$iframe(title = "Map", src = "map.html")
print(m)
EDIT 2
Waldi has solved the issue when using points. However the same problem occurs when using clusters or heatmaps. e.g.
library(tidyverse)
library(leaflet)
library(leaflet.extras)
leaflet()
Minimal code to show examples with 3 sets of coordinates
long <- as.numeric(c("0.005638", "0.005648", "0.005658"))
lat <- as.numeric(c("51.62879", "51.62889", "51.62879"))
data1 <- data.frame(long, lat)
filtered_list <- 1:3
cat("## Tabs {.tabset .tabset-fade .tabset-pills}", "\n")
for (estates in filtered_list){
cat("###", estates, "\n")
cat("\n\n\n")
cat("This is where the map will go ")
cat("1 ")
# generate leaflet plot (doesn't even show white space if not stored in tagList)
page <- htmltools::tagList(
leaflet() %>%
addTiles() %>% # Add default OpenStreetMap map tiles
addMarkers(lng=data1$long, lat=data1$lat, popup="The birthplace of R")
)
cat(as.character(page))
cat("2 ")
page <- htmltools::tagList(
leaflet() %>%
addTiles() %>% # Add default OpenStreetMap map tiles
addMarkers(lng=data1$long, lat=data1$lat, popup="The birthplace of R", clusterOptions = markerClusterOptions())
)
cat(as.character(page))
cat("3 ")
page <- htmltools::tagList(
leaflet() %>%
addTiles() %>% # Add default OpenStreetMap map tiles
addMarkers(lng=data1$long, lat=data1$lat, popup="The birthplace of R") %>%
addHeatmap(
lng = data1$lat, lat = data1$long,
blur = 20, max = 5, radius = 40
)
)
cat(as.character(page))
cat("4 ")
page <- htmltools::tagList(
leaflet() %>%
addTiles() %>% # Add default OpenStreetMap map tiles
addMarkers(lng=data1$long, lat=data1$lat, popup="The birthplace of R") %>%
addHeatmap(
lng = data1$lat, lat = data1$long,
blur = 20, max = 5, radius = 40
)
)
cat(as.character(page))
}