I am working on an R Markdown ( *.Rmd) document in RStudio with HTML widgets. I have a map created by functions in a flyer package , including a legend created by a function addLegend(). This is followed by a heat map created by the function d3heatmap()of the d3heatmap package . When I run the code using Run All Chunks, the output that appears in the Viewer panel looks great. When I run the code using Knit HTML, there is no legend in the output that appears in the popup.
What do I need to do to keep the legend displayed on the map, even when I show the heat map?
Below is an example that you can try yourself. It is based on http://rstudio.imtqy.com/leaflet/legends.html and requires that you download countries.geojson data from http://data.okfn.org/data/datasets/geo-boundaries-world-110m .
Using Knitting HTML in this example, if I turn on the heatmap using {r, eval=TRUE}, there is no legend on the flyer card . If I exclude the heat map using {r, eval=FALSE}, there is a legend on the map booklet map.
I am using R version 3.2.3 (2015-12-10), platform: x86_64-w64-mingw32 / x64 (64-bit), running under: Windows 7 x64 (build 7601) with Service Pack 1 (SP1), with RStudio Version 0.99.491 and packages d3heatmap_0.6.1, rgdal_1.1-3, sp_1.2-1 and leaflet_1.0.0.
---
title: "Testing"
output:
html_document: default
---
Some text.
```{r, message=FALSE}
library(leaflet)
library(rgdal)
countries <- readOGR("json/countries.geojson", "OGRGeoJSON",
verbose=FALSE)
map <- leaflet(countries) %>% addTiles()
pal <- colorNumeric(
palette = "YlGnBu",
domain = countries$gdp_md_est)
map %>%
addPolygons(stroke = FALSE, smoothFactor = 0.2, fillOpacity = 1,
color = ~pal(gdp_md_est)) %>%
addLegend("bottomright", pal = pal, values = ~gdp_md_est,
title = "Est. GDP (2010)",
labFormat = labelFormat(prefix = "$"),
opacity = 1)
```
Some text.
```{r, eval=TRUE}
library(d3heatmap)
d3heatmap(mtcars)
```