Leaflet not rendering in dynamically generated R markdown html knitr
This is a similar problem as described here with Highcharter
Try:
---
title: "Test Leaflet Tabs"
output: html_document
---
`r knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE, cache = F)`
```{r setup, include=FALSE}
library(leaflet)
leaflet()
```
```{r,results='asis'}
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 ")
# 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=174.768, lat=-36.852, popup="The birthplace of R")
)
cat(as.character(page))
}
```
Recommended way to initialize JS renderer in 'asis' R Markdown chunk
The answer to my question has been given by @cderv :
https://github.com/rstudio/rmarkdown/issues/1877#issuecomment-679864674
The results = 'asis' is now more documented in https://bookdown.org/yihui/rmarkdown-cookbook/results-asis.html#results-asis . It is aimed at generating raw mardown content from a R chunk. Anything must result in a text output, and implicitly that means no knitr magic really happens for any R object in those chunk, because knitr does no adjustment when knit_printing the content (as it is juts text)
I think I would not use result = 'asis' to cat() a complex R object like an htmlwidget. You found a workaround but you may encounter other issues.
As this answer has been liked by @yihui, it gives a hint that cat + asis
on htmlwidget
should be used at one's own risk.
However, I'll personnaly continue to use the workarounds mentioned in the question, because as long as it works I find it very practical.
Thanks @atusi & @cderv for their valuable input.
Dynamically loop through htmlwidgets and add knitr formatting for RMarkdown
I'll copy my response to the Github issue below.
Good question, and I think others will be helped by this discussion. It might be easiest to start by building something like what you propose from scratch without the aid of rmarkdown
.
manually build
# https://github.com/ramnathv/htmlwidgets/pull/110#issuecomment-216562703
library(plotly)
library(htmltools)
library(markdown)
library(shiny)
browsable(
attachDependencies(
tagList(
tags$div(
class="tabs",
tags$ul(
class="nav nav-tabs",
role="tablist",
tags$li(
tags$a(
"data-toggle"="tab",
href="#tab-1",
"Iris"
)
),
tags$li(
tags$a(
"data-toggle"="tab",
href="#tab-2",
"Cars"
)
)
),
tags$div(
class="tab-content",
tags$div(
class="tab-pane active",
id="tab-1",
as.widget(
plot_ly(
iris,
x = iris[["Sepal.Length"]],
y = iris[["Sepal.Width"]],
mode = "markers"
)
)
),
tags$div(
class="tab-pane",
id="tab-2",
as.widget(
plot_ly(
cars,
x = speed,
y = dist,
mode = "markers"
)
)
)
)
)
),
# attach dependencies
# see https://github.com/rstudio/rmarkdown/blob/master/R/html_document.R#L235
list(
rmarkdown::html_dependency_jquery(),
shiny::bootstrapLib()
)
)
)
in rmarkdown
There is probably a better way to make this work, but until someone sets me straight, we can take the approach from above and use it in rmarkdown
. Unfortunately, this is still very manual. For more reference, here is the code that RStudio uses to build tabsets
.
---
title: "tabs and htmlwidgets"
author: "Kent Russell"
date: "May 3, 2016"
output: html_document
---
```{r echo=FALSE, message=FALSE, warning=FALSE}
library(plotly)
library(htmltools)
library(magrittr)
# make a named list of plots for demonstration
# the names will be the titles for the tabs
plots <- list(
"iris" = plot_ly(
iris,
x = iris[["Sepal.Length"]],
y = iris[["Sepal.Width"]],
mode = "markers"
),
"cars" = plot_ly(
cars,
x = speed,
y = dist,
mode = "markers"
)
)
# create our top-level div for our tabs
tags$div(
# create the tabs with titles as a ul with li/a
tags$ul(
class="nav nav-tabs",
role="tablist",
lapply(
names(plots),
function(p){
tags$li(
tags$a(
"data-toggle"="tab",
href=paste0("#tab-",p),
p
)
)
}
)
),
# fill the tabs with our plotly plots
tags$div(
class="tab-content",
lapply(
names(plots),
function(p){
tags$div(
# make the first tabpane active
class=ifelse(p==names(plots)[1],"tab-pane active","tab-pane"),
# id will need to match the id provided to the a href above
id=paste0("tab-",p),
as.widget(plots[[p]])
)
}
)
)
) %>%
# attach the necessary dependencies
# since we are manually doing what rmarkdown magically does for us
attachDependencies(
list(
rmarkdown::html_dependency_jquery(),
shiny::bootstrapLib()
)
)
```
Related Topics
Convert Latitude/Longitude to State Plane Coordinates
Reconstruct Symmetric Matrix from Values in Long-Form
R - Stuck with Plot() - Colouring Shapefile Polygons Based Upon a Slot Value
Adding Values in Two Data.Tables
R Applying a Function to a Subset of a Data Frame
Group/Bin/Bucket Data in R and Get Count Per Bucket and Sum of Values Per Bucket
How to Efficiently Retrieve Top K-Similar Vectors by Cosine Similarity Using R
Finding Which Element of a Vector Is Between Two Values in R
Grouped Bar Chart on R Using Ggplot2
Using Sample() with Sample Space Size = 1
Getting The Name of a Dataframe from Loading a .Rda File in R
Make a Boxplot Without Whiskers
Verify Object Existence Inside a Function in R
Using Read.Csv.Sql to Select Multiple Values from a Single Column