0

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?

1 Answer 1

1

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()
3
  • Hi, thanks a lot for help :) but how to display a list of objects without showing those pesky [[indices]] in R Markdown?
    – tomsu
    Commented Apr 25, 2023 at 12:45
  • I found work(l, plot)
    – tomsu
    Commented Apr 25, 2023 at 12:59
  • 1
    Another possibility is purrr::walk(l, print). The walk() function will iterate over each element of the list and pass it to the print() function separately, which ommits the printing of indices.
    – gl-eb
    Commented Apr 25, 2023 at 13:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.