使用动态生成的选项卡时如何显示和弦图的列表? [英] How to display a list of chorddiag plot swhen using dynamically generated tabs?

查看:64
本文介绍了使用动态生成的选项卡时如何显示和弦图的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想动态生成标签,每个标签绘制corddiag plot

I want to dynamically generate tabs, and each tab plot the corddiag plot

library(igraph)
library(tidygraph)
library(chorddiag)

m <- matrix(c(11975,  5871, 8916, 2868,
               1951, 10048, 2060, 6171,
               8010, 16145, 8090, 8045,
               1013,   990,  940, 6907),
               byrow = TRUE,
               nrow = 4, ncol = 4)
groupnames <- c("black", "blonde", "brown", "red")
row.names(m) <- groupnames
colnames(m) <- groupnames

m 只是为了简单起见我使用的一些 corddiag 图.基于不同的数据集,我在每个页面上都有不同的图.所以我在下一步中创建了一个 corddiag 图列表:

m is just some corddiag plot I use for simplicity. I have different plots on each page based on a different dataset. So I created a list of corddiag plots in my next step:

```{r, include=FALSE}
graphList_biling.lan <- list()  
for ( i in 1:3) {
 graphList_biling.lan[[i]] <- htmltools::tagList(chorddiag(m))
}
```

为了简单起见,我只是在每个循环中使用了相同的 m,但在我的实际情节中,它们会有所不同,这就是我需要创建一个列表的原因.然后我执行以下操作:

I just used the same m in each loop for simplicity, but in my actual plot those will be different, that is why I need to create a list. Then I do the following :

## First learned languages {.tabset .tabset-fade .tabset-pills}

```{r echo=FALSE, fig.height=6, fig.width=6, warning=FALSE, results='asis'}
for (i in 1:3) {
  cat("###", paste("Tab ",i), '{-}',  '\n\n')
  print(htmltools::tagList( graphList_biling.lan[[i]]))
  cat( '\n\n')
}
```

在 Rmarkdown 窗口中一切正常,但是当我打结时,我无法生成图.它确实创建了一个空间,但没有显示图

Everything works pretty well in Rmarkdown window, but when I knot I can't generate plots. It does create a space but no plots show up

推荐答案

试试这个.解决方案与您之前的问题相同.(; 决定性的一步是在循环之外添加一个虚拟"情节.根据这个 post 需要这个虚拟图来确保库(在本例中为 d3)包含在呈现的 HTML 中:

Try this. Solution is the same as in case of your former question. (; The decisive step is to add a "dummy" plot outside of the loop. According to this post this dummy plot is needed to ensure that the libs (in this case d3) are included in the rendered HTML:

---
title: "test_tab_loop"
date: "26 5 2020"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```


```{r}
library(igraph)
library(tidygraph)
library(chorddiag)

m <- matrix(c(11975,  5871, 8916, 2868,
               1951, 10048, 2060, 6171,
               8010, 16145, 8090, 8045,
               1013,   990,  940, 6907),
               byrow = TRUE,
               nrow = 4, ncol = 4)
groupnames <- c("black", "blonde", "brown", "red")
row.names(m) <- groupnames
colnames(m) <- groupnames
```

## First learned languages {.tabset .tabset-fade .tabset-pills} 

```{r, include=FALSE}
## init step: takes care that the libs are included
htmltools::tagList(chorddiag(m))
```

```{r, include=FALSE}
graphList_biling.lan <- list()  
for (i in 1:3) {
  graphList_biling.lan[[i]] <- htmltools::tagList(chorddiag(m))
}
```

## First learned languages {.tabset .tabset-fade .tabset-pills}

```{r echo=FALSE, fig.height=6, fig.width=6, warning=FALSE, results='asis'}
for (i in 1:3) {
  cat("###", paste("Tab ",i), '{-}',  '\n\n')
  ## You already applied htmltools::taglist. So just print
  print(graphList_biling.lan[[i]])
  cat( '\n\n')
}
```

这篇关于使用动态生成的选项卡时如何显示和弦图的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆