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

1

0

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

Hann answered 26/5, 2020 at 6:43 Comment(0)
C
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')
}
```
Cerium answered 26/5, 2020 at 16:52 Comment(3)
@stephen, yes that what I tried to do. it works when I have only one chorddiag plot. However when I have a list of plots ( I updated my example with the structure I have), it does not helpHann
Hej @Hann or as I call you from now: yulia-tabset-loop-uu. (; First. Your code still missed the "init" step. Second. As you already converted to a tagList in the first loop you don't need to tagList in the second loop. Just print the list elements. See my edit.Cerium
Thanks a megaton! I finally understood the meaning of init step:) I am strongly considering updating my user name based on your suggestion ;)Hann

© 2022 - 2024 — McMap. All rights reserved.