1

I generate a list of highcharts objects and tabs. Then I'd like to render it into an html page.

I can't figure out how to do it in a simple loop.

If i do it one by one it works, but not in a for.

Here is an example :

---
output: 
  html_document
---

``` {r, echo=FALSE, results='asis'}
library(highcharter)

out<-list(gr1=highcharts_demo(),gr2=highcharts_demo())

cat("

Column {.tabset}
-----------------------------------------------------------------------

")

cat("

###A1

"
)
out[[1]]

cat("

###A2

"
)
out[[2]]

for (i in c(1,2) )
{
  cat(paste0("

###","B",i,"

"
))
  out[[i]]
}
```

I compile it in RStudio with knitr.

And only the first two tabs have graphs, not the last two...

I tried to put explicit print or show, to add a \n in the loop. No luck.

Any idea ? Thanks a lot for your help.

xloki
  • 13
  • 3
  • This question is answered in https://stackoverflow.com/questions/35567124/how-to-print-htmlwidgets-to-html-result-inside-a-function and https://stackoverflow.com/questions/30509866/for-loop-over-dygraph-does-not-work-in-r. In summary: you need to put the results in a "tagList". – user2554330 May 24 '17 at 00:06

1 Answers1

1

To expand on my comment: you can put each out[[i]] item in a tagList, and print it. Your loop would become

for (i in c(1,2) )
{
  cat(paste0("

###","B",i,"

"
))
  print(htmltools::tagList(out[[i]]))
}
user2554330
  • 37,248
  • 4
  • 43
  • 90
  • great works like a charm. I spent many hours research but couldn't link my problem to the references you mentionned. Thanks a lot. – xloki May 24 '17 at 13:20
  • There is a problem though : if I remove the "direct" first prints (out[[1]] and out[[2]] lines) and leave only the loop with the tagList, it doesn't work anymore. – xloki May 24 '17 at 15:20
  • Probably the first ones trigger inclusion of something, and the tagList doesn't. So do the first one manually, the rest in a loop. – user2554330 May 24 '17 at 18:03