r - 在闪亮时将 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 尝试了两种样式,但它们返回空结果。
任何关于他们下载处理程序为什么不起作用的见解或关于如何最好地影响下载功能的重定向将不胜感激。
解决方案
我设法通过使用张贴在 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 上时它不起作用
推荐阅读
- python - Python strptime 在数据框中给出 int 而不是 datetime 对象
- swift - 在 iOS13 中,是否允许在收到 VoIP Push 时报告 CXCallUpdate 或 CXEndCallAction?
- c# - 将 WPF 应用程序移植到 NetCore 3 时缺少对 MSTSCLib 的引用
- ruby-on-rails - 哎呀!!找不到资源项的方法 index.en
- google-cloud-platform - 是否可以查看具有特定权限的所有资源的列表?
- java - 使用 EL 表达式访问 JSP 中的 JSONObject 属性
- python - 如何在 python 中使用多处理?
- python - Python Pandas Dataframe 在单元格中搜索文本
- reactjs - 将事件传递给另一个 js 文件中的函数
- c++ - 无法在 Visual Studio 2019 中打印整数