【问题标题】:Downloading wordcloud2 output as png/jpg on shiny在闪亮时将 wordcloud2 输出下载为 png/jpg
【发布时间】:2019-01-18 19:14:31
【问题描述】:

我正在尝试从闪亮的 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 的样式,如代码所示,但它们返回空结果。

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

【问题讨论】:

    标签: r shiny wordcloud2


    【解决方案1】:

    我设法通过使用张贴在 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上时它不起作用

    【讨论】:

    • 这在我的本地机器上不起作用——下载的是一个 html 文件,而不是 png
    猜你喜欢
    • 1970-01-01
    • 2013-05-10
    • 2020-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多