rr-markdownvenn-diagramtaglisthtmltools

ggVennDiagram in lagList in R MarkDown


I am trying to embed Venn Diagrams to my report made in R MarkDown. I have dinamic number of graphs so I generate them by for loop. I know that R MarkDown has "problem" with loops but I find the way how to solve it when I want to plot plotly objects. But when I used a similar solution for Ven Diagrams from library ggVennDiagram I got error:

Error in as.vector(x, "character") : cannot coerce type 'environment' to vector of type 'character'

Code:

listToPlot <- list(
  x = unique(round(100*runif(100))),
  y = unique(round(100*runif(100))),
  z = unique(round(100*runif(100))),
  q = unique(round(100*runif(100)))
)

l <- htmltools::tagList()
if(length(listToPlot) > 3){
  for(i in c(2:length(listToPlot))){
    l[[i-1]] <- ggVennDiagram::ggVennDiagram(listToPlot[c(1,i)])
  }
}else{
  if(length(listToPlot) > 1){
    l[[1]] <- ggVennDiagram::ggVennDiagram(listToPlot)
  }
}
l

does anyone know how to solve this error?


Solution

  • Looks like you based your code on this answer. Using htmltools::tagList() seems to be a workaround for working with lists of plotly plots specifically. Since ggVennDiagram uses ggplot2, there is no need to use the plotly workaround as far as I can tell.

    Replace this line:

    l <- htmltools::tagList()
    

    With this:

    l <- list()