admin管理员组文章数量:1417549
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 badges1 Answer
Reset to default 0This 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:
本文标签: rHow to display headers from a quarto with a for loop and plotly graphsStack Overflow
版权声明:本文标题:r - How to display headers from a quarto with a for loop and plotly graphs? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745270169a2650841.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论