wordcloud2 icon indicating copy to clipboard operation
wordcloud2 copied to clipboard

Wordcloud failing in Rmarkdown tabsets

Open QuentinB02 opened this issue 5 years ago • 10 comments

Hello,

I am facing what seems like a bug while using the worcloud2 package with Rmarkdown : displaying one or multiple workclouds on the same page works fine, as long as I do not make use of tabs (namely tabsetPanel from Shiny). Specifically any wordcloud that's not on the first tab will not be shown.

Here is a reproducible Rmd example (without header formatting, but I also attached the Rmd version to this issue) :


title: "Wordcloud tabset error in Rmd" output: html_document

library(wordcloud2)
library(shiny)
wordcloud = wordcloud2::wordcloud2(data.frame(word = c("foo", 
                                                       "bar", 
                                                       "foobar"),
                                              n = c(15, 
                                                    35, 
                                                    50)))
tabsetPanel(
  tabPanel("test", wordcloud),
  tabPanel("test2", wordcloud)
)

wordcloud_tabset_error.zip

Upon knitting, the first wordcloud is showing, but not the second one... Any idea why that might be? Thanks in advance for any help on the matter, and thanks for all the amazing work on this package.

PS : I installed the latest package version from Github, not the one on CRAN

QuentinB02 avatar May 23 '19 13:05 QuentinB02

Happens the same here.

If I use two wordclouds in an rmd knitted to HTML, only the first one is shown.

samuelhuerga avatar Jun 07 '19 10:06 samuelhuerga

Happens the same here.

If I use two wordclouds in an rmd knitted to HTML, only the first one is shown.

Same here.

gaospecial avatar Nov 20 '19 10:11 gaospecial

here I have a alternative solution.

Use the following function.

#' Webshot a widget and place it in place with markdown syntax
#'
#' @param widget a html widget, i.e. generated by wordcloud2 pacakge
#' @param path figure webshot storage place
#'
#' @return
#' @export
#'
#' @examples
markdown_widget <- function(widget,path=getwd(),filename="file.png"){
  require(htmlwidgets)
  require(webshot)
  saveWidget(widget,"tmp.html",selfcontained = F)
  file <- paste(path,filename,sep = "/")
  webshot("tmp.html",file,delay = 5,vwidth = 1024,vheight = 768)
  file.remove("tmp.html")
  paste0("![",file,"](",file,")\n\n") %>% cat()
}

when in markdown file, insert a chunk like this:

```{r results="asis"} widget <- wordcloud2(...) markdown_widget(widget,path=".",filename="wordcloud.png") ```

then in your markdown file, the figure will finally show up.

gaospecial avatar Nov 21 '19 07:11 gaospecial

Hi @samuelhuerga,

I found a way to slove my problem.

Just install my fork by devtools::install_github("gaospecial/wordcloud2"), and then it works.

The following test code

library(wordcloud2)
wordcloud2(data = head(demoFreq,3))
wordcloud2(head(demoFreq,3), color = "random-light", backgroundColor = "grey")
wordcloud2(head(demoFreq,6), color = "random-light", backgroundColor = "white")

will give the result:

image

I don't know why it works, as I just update the wordcloud2.js library to the latest version.

May this can be helpful.

gaospecial avatar Feb 04 '20 15:02 gaospecial

Thanks Gao! I found that your solution (using your fork) worked for me in a 'standard' RMarkdown document, but not when used with tabset within flexdashboard. Here's a repo I quickly created producing the example:

https://github.com/martinctc/wordcloud2-test

Any idea if this is more an issue with flexdashboard perhaps?

martinctc avatar Feb 13 '20 15:02 martinctc

I just used @gaospecial 's version and it rendered both my wordcloud2s while using flexdashboard. Belows my session information

> sessionInfo()
R version 3.6.3 (2020-02-29)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.4 LTS

Matrix products: default
BLAS:   /opt/R/3.6.3/lib/R/lib/libRblas.so
LAPACK: /opt/R/3.6.3/lib/R/lib/libRlapack.so

locale:
 [1] LC_CTYPE=C.UTF-8       LC_NUMERIC=C           LC_TIME=C.UTF-8        LC_COLLATE=C.UTF-8     LC_MONETARY=C.UTF-8    LC_MESSAGES=C.UTF-8   
 [7] LC_PAPER=C.UTF-8       LC_NAME=C              LC_ADDRESS=C           LC_TELEPHONE=C         LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C   

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] dplyr_1.0.2       data.table_1.13.0 dtplyr_1.0.1      magrittr_1.5      lubridate_1.7.9  

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.5          here_0.1            tidyr_1.1.0         prettyunits_1.1.1   ps_1.3.4            assertthat_0.2.1    rprojroot_1.3-2    
 [8] digest_0.6.25       packrat_0.5.0       R6_2.4.1            backports_1.1.8     evaluate_0.14       httr_1.4.1          ggplot2_3.3.2      
[15] pillar_1.4.6        rlang_0.4.7         lazyeval_0.2.2      curl_4.3            rstudioapi_0.11     callr_3.4.3         rmarkdown_2.3      
[22] desc_1.2.0          devtools_2.3.1      flexdashboard_0.5.2 readr_1.3.1         stringr_1.4.0       htmlwidgets_1.5.1   munsell_0.5.0      
[29] compiler_3.6.3      xfun_0.16           pkgconfig_2.0.3     pkgbuild_1.1.0      htmltools_0.5.0     tidyselect_1.1.0    tibble_3.0.3       
[36] fansi_0.4.1         viridisLite_0.3.0   crayon_1.3.4        withr_2.2.0         grid_3.6.3          jsonlite_1.7.0      formattable_0.2.0.1
[43] gtable_0.3.0        lifecycle_0.2.0     scales_1.1.1        cli_2.0.2           stringi_1.4.6       fs_1.5.0            remotes_2.2.0      
[50] testthat_2.3.2      ellipsis_0.3.1      generics_0.0.2      vctrs_0.3.2         forcats_0.5.0       tools_3.6.3         glue_1.4.1         
[57] purrr_0.3.4         hms_0.5.3           crosstalk_1.1.0.1   processx_3.4.3      pkgload_1.1.0       yaml_2.2.1          colorspace_1.4-1   
[64] sessioninfo_1.1.1   memoise_1.1.0       plotly_4.9.2.1      wordcloud2_0.2.3    knitr_1.29          usethis_1.6.1

daczarne avatar Aug 21 '20 07:08 daczarne

thank you @gaospecial !

I wonder what is causing the issue. Outdated package installing via "wordcloud2" directly?

davidika avatar Sep 02 '20 04:09 davidika

Thanks, @gaospecial I have had this problem for years!

Does anyone know why lettercloud() doesn't work?

eimennin avatar Sep 02 '21 15:09 eimennin

thank you @gaospecial !

I wonder what is causing the issue. Outdated package installing via "wordcloud2" directly?

May be the backend wordcloud javascript has bug, as its version is very low.

gaospecial avatar Sep 03 '21 00:09 gaospecial

Thanks Gao! I found that your solution (using your fork) worked for me in a 'standard' RMarkdown document, but not when used with tabset within flexdashboard. Here's a repo I quickly created producing the example:

https://github.com/martinctc/wordcloud2-test

Any idea if this is more an issue with flexdashboard perhaps?

A bit old, but for @martinctc: something that got all of the wordclouds to show up for me was appending {.active} after each sub-header (e.g., ### Tab 1 {.active}.

Tab 1 Tab 2

jrtran avatar Sep 24 '21 03:09 jrtran