rshinywordcloud2

R Shiny - Wordcloud2 breaking downloadButton


I want to create several wordclouds, using wordcloud and wordcloud2 packages. I can create a download for the wordcloud created by the wordcloud package, but as soon as I render a wordcloud2-wordcloud, the download button breaks (only allows to download .html instead of .png).

I've added an example to reproduce it:

library("shiny")
library("wordcloud")
library("wordcloud2")
library("tm")

ui <- fluidPage(plotOutput("plot1"), downloadButton('plot1download'))

server <- function(input, output, session) {
  output$plot1 <- renderPlot({
    wordcloud(data(crude))
  })
  output$plot2 <- renderWordcloud2({
    wordcloud2(demoFreq)
  })
  output$plot1download <- downloadHandler(
    filename = function() {
      paste('wordcloud', '.png', sep='')
    },
    content = function(file) {
      device <- function(..., width, height) grDevices::png(...)
      ggsave(file, plot = wordcloud(data(crude)), device = device)
    }
  )
}

shinyApp(ui, server)

The code above works, but if I add another plot (plotOutput("plot2")), it breaks:

library("shiny")
library("wordcloud")
library("wordcloud2")
library("tm")

ui <- fluidPage(plotOutput("plot1"), downloadButton('plot1download'), wordcloud2Output("plot2"))

server <- function(input, output, session) {
  output$plot1 <- renderPlot({
    wordcloud(data(crude))
  })
  output$plot2 <- renderWordcloud2({
    wordcloud2(demoFreq)
  })
  output$plot1download <- downloadHandler(
    filename = function() {
      paste('wordcloud', '.png', sep='')
    },
    content = function(file) {
      device <- function(..., width, height) grDevices::png(...)
      ggsave(file, plot = wordcloud(data(crude)), device = device)
    }
  )
}

shinyApp(ui, server)

Anyone has an idea why this happens and how to resolve the issue?


Solution

  • This seems to be a bug in the CRAN version of wordcloud2. Install the development version from GitHub with remotes::install_github("lchiffon/wordcloud2") for a fix.