How to display a list of chorddiag plot swhen using dynamically generated tabs?

可紊 提交于 2020-06-01 05:08:17

问题


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 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))
}
```

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')
}
```

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


回答1:


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')
}
```


来源:https://stackoverflow.com/questions/62016121/how-to-display-a-list-of-chorddiag-plot-swhen-using-dynamically-generated-tabs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!