Lchiffon / wordcloud2

R interface to wordcloud for data visualization.
396 stars 107 forks source link

Self contained widget save does not work #20

Closed nebuzoltanszuhai closed 7 years ago

nebuzoltanszuhai commented 7 years ago

I tried to save the widget with the self-contained option, that does not work. There is nothing fancy in the frequency table I use, normal English words. Would be great if you could fix it. (Where I would like to use the widget, there is no option for using the non self-contained form of it.)

Lchiffon commented 7 years ago

Could you please leave your codes here?

Just using a small data like demoFreq

nebuzoltanszuhai commented 7 years ago
library(wordcloud2) 
library(htmlwidgets)

m <- wordcloud2::wordcloud2(wordcloud2::demoFreq)
htmlwidgets::saveWidget(m, file = "wordcloud2.html", selfcontained = TRUE)

The html itself saved, opening the html in a browser gives a "blank" page, viewing it with source view, you see this error:

TypeError: canvas.getBoundingClientRect is not a function. (In 'canvas.getBoundingClientRect()', 'canvas.getBoundingClientRect' is undefined)

htmlwidgets: 0.8 wordcloud2: 0.2.0 Tried with Safari and Chrome in MacOs, same behavior. In RStudio this looks ok: wordcloud2::wordcloud2(wordcloud2::demoFreq)

Lchiffon commented 7 years ago

Try this simple function to fix it, it is a problem caused by strange DOM, not because of wordcloud2, but pandoc...

simpleFix = function(inputFile, outputFile){
  a = readLines(inputFile)
  output = paste(a, collapse = "\n")
  output = gsub(">\n\n</div>","></div>",output)
  writeLines(output, outputFile)
  invisible(NULL)
}

library(wordcloud2) 
library(htmlwidgets)

m <- wordcloud2::wordcloud2(wordcloud2::demoFreq)
htmlwidgets::saveWidget(m, file = "wordcloud2.html", selfcontained = TRUE)
simpleFix("wordcloud2.html", "wordcloud2.html")
nebuzoltanszuhai commented 7 years ago

Works perfectly, thanks!