首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >以png/jpg的形式下载wordcloud2输出

以png/jpg的形式下载wordcloud2输出
EN

Stack Overflow用户
提问于 2019-01-18 19:14:31
回答 1查看 873关注 0票数 1

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

代码语言:javascript
复制
 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)

我尝试了两种使用下载处理程序的样式,如代码所示,但它们返回空的结果。

任何关于为什么他们的下载处理程序不工作或重定向如何最好地实现下载功能的洞察力,我们将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-21 10:30:12

通过使用在LeafletMaps上发布的下载处理程序函数示例:Why is webshot not working with leaflets in R shiny?,我成功地完成了下载工作。

我的更新代码如下:

代码语言:javascript
复制
  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上时,它不能工作。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54260164

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档