有没有办法使用 {capture} 包将 R Shiny 应用程序的屏幕截图保存到根目录而不是下载?

Is there a way to save screenshots of a R Shiny app to the root directory with {capture} package instead of downloading?

我有一个闪亮的应用程序,它使用包 wordcloud2 生成词云。我正在使用 capture 包截取 wordcloud 的屏幕截图。我也尝试过其他类似的包,例如 shinyscreenshotsnapper。它们都提供了一种截取应用程序的一部分或整个应用程序的屏幕截图并下载屏幕截图的方法。但在我的用例中,我需要在单击按钮时将图像保存到应用程序的根目录,而不是下载它。有办法吗?

这是一个可重现的例子:

library(shiny)
library(shinydashboard)
library(wordcloud2)
library(capture)

ui <- dashboardPage(
  title = "wordcloud",
  header = dashboardHeader(
    title = "Wordcloud"
  ),
  sidebar = dashboardSidebar(),
  body = dashboardBody(
    fluidRow(
      column(width = 3),
      column(
        width = 6,
        box(
          title = "wordcloud", solidHeader = TRUE, status = "primary", width = 12,
          wordcloud2Output("wordcloud")
        ),
        id = "cloud"
      ),
      column(width = 3)
    ),
    fluidRow(
      div(
        capture(
          selector = "#cloud",
          filename = "cloud",
          icon("camera"), "Downlaod wordcloud",
          format = "png"
        ),
        style = "text-align: center;"
      )
    )
  )
)

server <- function(input, output){
  output$wordcloud <- renderWordcloud2(
    wordcloud2(demoFreq, size=1, color='random-dark')
  )
}

shinyApp(ui, server)

提前致谢。

最新版本的shinscreenshoot (0.1.0) 确实只让你下载png文件。有一个用于将文件保存到服务器目录的新选项。让我们安装新版本:

devtools::install_github("daattali/shinyscreenshot@a4d374d")

单击此应用程序中的按钮将触发在服务器磁盘 R 会话的当前工作目录中创建文件 screenshoot.png

library(shiny)
library(shinydashboard)
library(wordcloud2)
library(shinyscreenshot)

ui <- dashboardPage(
  title = "wordcloud",
  header = dashboardHeader(
    title = "Wordcloud"
  ),
  sidebar = dashboardSidebar(),
  body = dashboardBody(
    fluidRow(
      column(width = 3),
      column(
        width = 6,
        box(
          title = "wordcloud", solidHeader = TRUE,
          status = "primary", width = 12,
          wordcloud2Output("wordcloud")
        ),
        id = "cloud"
      ),
      column(width = 3)
    ),
    fluidRow(
      actionButton("save_screenshoot", "Save screenshoot")
    )
  )
)

server <- function(input, output){
  output$wordcloud <- renderWordcloud2(
    wordcloud2(demoFreq, size=1, color='random-dark')
  )
  
  observeEvent(input$save_screenshoot, {
    screenshot(
      selector = "#wordcloud",
      download = FALSE,
      server_dir = ".",
      filename = "screenshoot",
    )
  }
  )
}

shinyApp(ui, server)