Search code examples
rr-markdownchord-diagram

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


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


Solution

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