英文:
Interactive ggiraph objects created in a loop does not show in quarto HTML output
问题
我正在尝试创建一个循环,使用ggiraph包生成+100个交互式图表。
当我以交互方式运行循环时,图表显示如预期。然而,当我渲染.qmd文档时,交互式图表没有显示。
下面是一个可重现的示例,包括YAML标题。
你能让最后一个循环中的交互式图表显示在渲染的输出中吗?
谢谢。
---
title: "测试"
format: html
---
```{r}
library(dplyr)
library(ggplot2)
library(ggiraph)
set.seed(1)
tmp_names <- diamonds %>% select(where(is.double)) %>% names() %>% sample(2)
这些非交互式图表会显示:
当我在会话中运行代码块时,以及渲染.qmd文档时
for(i in tmp_names){
tmp_plot <- diamonds %>%
select(all_of(i)) %>%
ggplot(aes(x = .data[[i]]))+
geom_density()
print(tmp_plot)
}
这些交互式图表仅在我在会话中运行循环时才会显示,
在渲染.qmd文件时不会显示。
for(i in tmp_names){
tmp_plot <- diamonds %>%
select(all_of(i)) %>%
ggplot(aes(x = .data[[i]],
tooltip = "我是交互式的!"))+
geom_density_interactive()
print(girafe(ggobj = tmp_plot))
}
英文:
I am trying to create a loop that generates +100 interactive graphs using the ggiraph package.
The plots appear as expected when I run the loop interactively. However, when I render the .qmd document, the interactive graphs are not showing.
A reproducible example, including the YAML header, is below.
Can you make the interactive graphs in the last loop appear in the rendered output?
Thanks.
---
title: "test"
format: html
---
```{r}
library(dplyr)
library(ggplot2)
library(ggiraph)
set.seed(1)
tmp_names <- diamonds %>% select(where(is.double)) %>% names() %>% sample(2)
```
## These not-interactive plots appear:
## When I run the chunk in the session and when the .qmd document is rendered
```{r}
for(i in tmp_names){
tmp_plot <- diamonds %>%
select(all_of(i)) %>%
ggplot(aes(x = .data[[i]]))+
geom_density()
print(tmp_plot)
}
```
## These interactive plots only appear when I run the loop in a session,
## Not when the .qmd file is rendered.
```{r}
for(i in tmp_names){
tmp_plot <- diamonds %>%
select(all_of(i)) %>%
ggplot(aes(x = .data[[i]],
tooltip = "I'm interactive!"))+
geom_density_interactive()
print(girafe(ggobj = tmp_plot))
}
```
答案1
得分: 3
在Quarto/R Markdown中,遍历HTML小部件的方式与静态图不同。一种解决方法是将你的ggiraph对象存储在一个列表中,然后使用htmltools::tagList()
:
---
title: "test"
format: html
---
```{r}
library(dplyr)
library(ggplot2)
library(ggiraph)
set.seed(1)
tmp_names <- diamonds %>% select(where(is.double)) %>% names() %>% sample(2)
#| results: asis
#| echo: false
gg_list <- list()
for(i in tmp_names){
tmp_plot <- diamonds %>%
select(all_of(i)) %>%
ggplot(aes(x = .data[[i]],
tooltip = "I'm interactive!"))+
geom_density_interactive()
gg_list[[i]] = girafe(ggobj = tmp_plot)
}
htmltools::tagList(gg_list)
我建议查看与此主题相关的GitHub问题链接中的解决方案:https://github.com/davidgohel/ggiraph/issues/55
英文:
Looping over html widgets in Quarto/R Markdown doesn't work in the standard way as static plots do. One solution is to store your ggiraph objects in a list and use htmltools::tagList()
:
---
title: "test"
format: html
---
```{r}
library(dplyr)
library(ggplot2)
library(ggiraph)
set.seed(1)
tmp_names <- diamonds %>% select(where(is.double)) %>% names() %>% sample(2)
```
```{r}
#| results: asis
#| echo: false
gg_list <- list()
for(i in tmp_names){
tmp_plot <- diamonds %>%
select(all_of(i)) %>%
ggplot(aes(x = .data[[i]],
tooltip = "I'm interactive!"))+
geom_density_interactive()
gg_list[[i]] = girafe(ggobj = tmp_plot)
}
```
```{r}
htmltools::tagList(gg_list)
```
I would suggest checking out the solutions linked from the GitHub issue on this topic: https://github.com/davidgohel/ggiraph/issues/55
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论