简体   繁体   English

在Shiny上将Wordcloud2输出下载为png / jpg

[英]Downloading wordcloud2 output as png/jpg on shiny

I am trying to download output from wordcloud2 on shiny. 我正在尝试从wordcloud2上下载闪亮的输出。 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. 我已经尝试使用downloadhandler两种样式,如代码所示,但是它们返回空结果。

Any insight on why they downloadhandler doesn't work or redirection on how best to effect the download function will be appreciated. 对于为什么他们的下载处理程序不起作用的任何见解,或关于如何最好地实现下载功能的重定向,将不胜感激。

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? 我通过使用在LeafletMaps上发布的下载处理程序功能的示例来使下载工作正常进行: 为什么Webshot无法在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 唯一的问题是,当将应用程序部署在Shiny.io上时,它不起作用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM