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]])
}
}