I am new in R-Markdown
and I have problem with rendering plotly
objects in HTML report. Problem is with for loop
. When I create graphs inside for loop
they are not visible in final report. I found solution for single loop: LINK
What about situation in double loop? I can’t convert the solution from a single loop into, for example, 2 nested loops.
To sum up:
- Single
FOR LOOP
-
plot invisible:
plots <- list() for(i in 1:3) { l[[i]] <- plot_ly(x = rnorm(10)) } l
-
plot visible:
l <- htmltools::tagList() for (i in 1:3) { l[[i]] <- plot_ly(x = rnorm(10)) } l
- Double
FOR LOOP
-
plot invisible:
for(j in 1:3){ # some text, tables depends of j # transform dataset based on `j` l <- htmltools::tagList() for (i in 1:3) { l[[i]] <- plot_ly(x = rnorm(10)) } l # <-plot on the end of each j loop, not on the end of whole loop like was in single for loop }
-
plot visible:
??
Any idea how to solve that problem? I will be very grateful for any help!
EDIT: example of code
#' ---
#' title: "Sample Document"
#' output:
#' html_document:
#' toc: true
#' theme: united
#' ---
#' R Markdown report of graphs generated with a double (nested) for loop
df <- data.frame(
j = numeric(),
desc = character()
)
for(j in c(1:3)){
cat(paste0("# below we present visualizations for iteration no.: ", j, "n"))
#' Random table
#+ echo=FALSE, results = 'asis'
df <- rbind(df, data.frame(j = j, desc = paste0("iteration no.: ", j)))
print(knitr::kable(df, row.names = F))
l <- htmltools::tagList()
for(i in (1:3)){
l[[i]] <- plot_ly(x = j*rnorm(i*10))
print(l[[i]])
}
}
8
Basically you could achieve this with the same approach by wrapping the outer loop in a second tagList
:
---
title: "Untitled"
output: html_document
date: "2023-04-12"
---
```{r echo=FALSE, message=FALSE}
library(plotly)
```
# Using `lapply`
```{r message=FALSE}
htmltools::tagList(
lapply(1:2, function(j) {
l <- htmltools::tagList()
for (i in 1:2) {
l[[i]] <- plot_ly(x = rnorm(10)) |>
layout(title = paste(j, i, sep = "."))
}
l
})
)
```
# Using a `for` loop
```{r message=FALSE}
ll <- htmltools::tagList()
for (j in 1:2) {
l <- htmltools::tagList()
for (i in 1:2) {
l[[i]] <- plot_ly(x = rnorm(10)) |>
layout(title = paste(j, i, sep = "."))
}
ll[[j]] <- l
}
ll
```
EDIT Adapting my answer on this post to your case you could achieve your desired result like so. The important step is to make sure that the JS dependencies are included. Afterwards you could print the plots created in the inner loop using print
.
---
title: "Untitled"
output:
html_document:
toc: true
theme: united
date: "2023-04-12"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(message = FALSE)
```
```{r echo=FALSE}
library(plotly)
```
```{r include=FALSE}
df <- data.frame(
j = numeric(),
desc = character()
)
```
```{r, include=FALSE}
# Init Step to make sure that the dependencies are loaded
htmltools::tagList(plot_ly(x = rnorm(10), type = "histogram"))
```
```{r results='asis', echo=FALSE}
for (j in 1:2) {
cat(paste0("# below we present visualizations for iteration no.: ", j, "n"))
df <- rbind(df, data.frame(j = j, desc = paste0("iteration no.: ", j)))
print(knitr::kable(df, row.names = F))
l <- htmltools::tagList()
for (i in 1:2) {
l[[i]] <- plot_ly(x = j * rnorm(i * 10))
}
print(l)
}
```
4