Downloading wordcloud2 output as png/jpg on shiny

时间:2019-01-18 19:14:31

标签: r shiny wordcloud2

I am trying to download output from wordcloud2 on shiny. My code is as below:

 library(shiny)
 library(htmlwidgets)
 library(webshot)
      ui <- shinyUI(fluidPage(mainPanel(
            wordcloud2Output("wordcl"),
            downloadButton(outputId = "savecloud"),
            downloadButton(outputId = "savecloud2")
      )))

  server <- shinyServer(function(input, output, session) {
          wordcl <- reactive ({
           wordcloud2(demoFreq, color = "random-light", backgroundColor = "grey")    
    })

        output$wordcl <- renderWordcloud2({  wordcl() })

 ##### SOLUTION 1 #########
   output$savecloud <- downloadHandler(
          filename = "word.png",
          content = function(cloud) {
          file.copy(wordcl(), cloud)
           })
##### SOLUTION 2 ##########
  output$savecloud2 <- downloadHandler(
        saveWidget(wordcl(), file="temp.html", selfcontained = F),
         webshot("temp.html", file = "word2.png",
      cliprect = "viewport")
      )
      })

shinyApp(ui = ui, server = server)

I have tried two styles using downloadhandler as shown in the code but they return empty results.

Any insight on why they downloadhandler doesn't work or redirection on how best to effect the download function will be appreciated.

1 个答案:

答案 0 :(得分:0)

我通过使用发布在LeafletMaps上的下载处理程序函数的示例来设法使下载正常进行:Why is webshot not working with leaflets in R shiny?

我更新的代码如下:

  library(shiny)
  library(htmlwidgets)
  library(webshot)
  library(wordcloud2)
 #webshot::install_phantomjs()


  ui <- shinyUI(fluidPage(mainPanel(
       wordcloud2Output("wordcl"),
       downloadButton(outputId = "savecloud")
        )))

 server <- shinyServer(function(input, output, session) {
          wordcl <- reactive ({
         wordcloud2(demoFreq, color = "random-light", backgroundColor = "grey")
                    })
        output$wordcl <- renderWordcloud2({
                         wordcl()
                             })   
    output$savecloud <- downloadHandler(
               filename = paste("wordcloud", '.png', sep=''),
               content = function(file) {
               owd <- setwd(tempdir())
               on.exit(setwd(owd))
              saveWidget(wordcl(), "temp.html", selfcontained = FALSE)
              webshot("temp.html", delay =15, file = file, cliprect = "viewport")
                    }) 
          })

shinyApp(ui = ui, server = server)

链接上给出的解决方案似乎结合了我在原始帖子中尝试实现的解决方案。

唯一的问题是,当将应用程序部署在Shiny.io上时,该功能不起作用