Search code examples
rshinyshinyappswordcloud2shinyscreenshot

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


I have a shiny app that generates a wordcloud using the package wordcloud2. I am using the capture package to take a screenshot of the wordcloud. I have also tried other similar packages such as shinyscreenshot and snapper. They all provide a way to take screenshots of a portion of the app or the whole app and download the screenshot. But in my use case, I need to save the image to the root directory of the app when I click the button instead of downloading it. Is there a way to do so?

Here is a reproducible example:

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)

Thanks in advance.


Solution

  • The latest release of shinyscreenshoot (0.1.0) indeed only lets you download the png file. There is a new option for saving the file to a server directory. Let's install the new version:

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

    Clicking on the button at this app will trigger the creation of a file screenshoot.png in the current working directory in the R session at the server's disk:

    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)