首页 > 解决方案 > 在闪亮时将 wordcloud2 输出下载为 png/jpg

问题描述

我正在尝试从闪亮的 wordcloud2 下载输出。我的代码如下:

 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)

如代码所示,我使用 downloadhandler 尝试了两种样式,但它们返回空结果。

任何关于他们下载处理程序为什么不起作用的见解或关于如何最好地影响下载功能的重定向将不胜感激。

标签: rshinywordcloud2

解决方案


我设法通过使用张贴在 LeafletMaps 上的下载处理程序函数示例使我的下载工作:为什么 webshot 不能使用 R 中的传单闪亮?

我更新的代码如下:

  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 上时它不起作用


推荐阅读