在循环中创建的交互式ggiraph对象在Quarto HTML输出中不显示。

huangapple go评论69阅读模式
英文:

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: &quot;test&quot;
format: html
---

```{r}
library(dplyr)
library(ggplot2)
library(ggiraph)
set.seed(1)
tmp_names &lt;- diamonds %&gt;% select(where(is.double)) %&gt;% names() %&gt;% 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 &lt;- diamonds %&gt;%
    select(all_of(i)) %&gt;% 
    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 &lt;- diamonds %&gt;%
    select(all_of(i)) %&gt;% 
    ggplot(aes(x = .data[[i]],
               tooltip = &quot;I&#39;m interactive!&quot;))+
    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: &quot;test&quot;
format: html
---

```{r}
library(dplyr)
library(ggplot2)
library(ggiraph)
set.seed(1)
tmp_names &lt;- diamonds %&gt;% select(where(is.double)) %&gt;% names() %&gt;% sample(2)
```

```{r}
#| results: asis
#| echo: false
gg_list &lt;- list()
for(i in tmp_names){
  tmp_plot &lt;- diamonds %&gt;%
    select(all_of(i)) %&gt;% 
    ggplot(aes(x = .data[[i]],
               tooltip = &quot;I&#39;m interactive!&quot;))+
    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

huangapple
  • 本文由 发表于 2023年2月27日 07:14:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75575583.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定