Downloading wordcloud2 output as png/jpg on shiny

后端 未结 1 994
生来不讨喜
生来不讨喜 2021-01-24 13:52

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

 library(shiny)
 library(htmlwidgets)
 library(webshot)
      ui <- shinyUI(flu         


        
相关标签:
1条回答
  • 2021-01-24 14:33

    I managed to make my download work by using an example of download handler function posted on LeafletMaps here: Why is webshot not working with leaflets in R shiny?

    My updated code is as below:

      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)
    

    The solution given on the link seems to combine the solutions I was trying to implement in my original post.

    The only issue is that it does not work when the app is deployed on shiny.io

    0 讨论(0)
提交回复
热议问题