r - How to display headers from a quarto with a for loop and plotly graphs? - Stack Overflow

I use tagList in order to render ploty graphs within a for loop. But the headers don't show !Is i

I use tagList in order to render ploty graphs within a for loop. But the headers don't show ! Is it possible to have plotly graphs AND headers within the for loop ? I want to show a table of content in the html file.

 library(plotly)
 library(htmltools)
   
 l <- htmltools::tagList()
   
 for(i in 1:3) {
     l[[i]] <- tagList(paste0("\n\n####Zone ", i, "\n"),
            plot_ly(x = rnorm(10), type="histogram"))
 }
   
   
 l

actual result

what I want

I use tagList in order to render ploty graphs within a for loop. But the headers don't show ! Is it possible to have plotly graphs AND headers within the for loop ? I want to show a table of content in the html file.

 library(plotly)
 library(htmltools)
   
 l <- htmltools::tagList()
   
 for(i in 1:3) {
     l[[i]] <- tagList(paste0("\n\n####Zone ", i, "\n"),
            plot_ly(x = rnorm(10), type="histogram"))
 }
   
   
 l

actual result

what I want

Share Improve this question edited Jan 31 at 10:05 DouxValkyn asked Jan 31 at 10:04 DouxValkynDouxValkyn 33 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

This gets bit tricky as you'd normally (i.e. in Shiny) use htmltools::h4(paste0("Zone ",i)) there, but then Quarto would not be able to pick up headings for styling and table of contents.

There might be less clunky / more robust options, but outputting both Markdown ("#### Zone ...") and htmlwidgets (plot_ly() output) in an asis block and manually handling widget dependencies should do.

Latter is needed as (in following example) no other Plotly object gets added by regular means, nothing triggers inclusion of Plotly js / css resources and plots in resulting HTML would not render.

Complete QMD document:
---
format:
  html:
    toc: true
    toc-depth: 4
---

# Foo  
Lorem ipsum dolor sit amet, tempor interdum felis a nostra in
```{r}
#| warning: false
library(plotly)
library(htmltools)

# create a dummy Plotly object to gather dependencies, 
# attach those by including in a tagList
findDependencies(plot_ly()) |> tagList()
```

```{r}
#| output: asis
for(i in 1:3) {
  paste0("\n\n#### Zone ", i, "\n") |> cat()
  plot_ly(x = rnorm(10), type="histogram") |> tagList() |> print()
}
```

# Bar  
Felis adipiscing vestibulum risus ipsum libero.  
Renders as:

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745270170a4619696.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信