Search code examples
rshinyshinydashboard

Is there any way to create a screenshot or PDF?


We have a shiny app and I'm looking for a way to create/download a screenshot (PNG, JPG) or export/download a PDF. The optimum would be if this file only contains the visuals (green), but I'd also be quite happy if there is only a way to do this with full frame (yellow or full page).

enter image description here

Is there an easy way to achieve this? Like clicking on a button and the download starts.


MWE

library(shiny)
library(shinydashboard)
library(data.table)
library(DT)
library(bslib)

################################################################################
################################ S E R V E R ###################################
################################################################################

server = shinyServer(function(input,output){
  
  output$histogram = renderPlot(
    hist(faithful$eruptions, breaks=input$days_plot)
  )
  
  output$histogram2 = renderPlot(
    hist(faithful$eruptions, breaks=input$days_plot)
  )
  
  output$active_cases = DT::renderDataTable(
    mtcars, selection = 'single', options=list(scrollX=TRUE))
  
})

################################################################################
#################################### U I #######################################
################################################################################

ui = shinyUI(
  dashboardPage(
    dashboardHeader(
      title="just a test"
    ),
    dashboardSidebar(
      #h3("Downstream", style="text-align:center; 
      #                        color:white;
      #                        background-color:red'"
      #   ),
      sidebarMenu(id="tabs",
                  menuItem("Tab1", tabName="active_cases", icon = icon("magnifying-glass-location")),
                  menuItem("Tab2", tabName="archive", icon = icon("box-archive")),
                  menuItem("Configuration", sliderInput("days_plot", "Days into past", 1, 60, 30))
      )
    ),
    dashboardBody(
      tabItems(
        tabItem(tabName="active_cases", shiny::h2("Active Cases"),
                fluidRow(
                  box(title="Table", status="primary", solidHeader=TRUE, div(DT::dataTableOutput("active_cases")))
                ),
                fluidRow(
                  box(title="Visual1", status="primary", solidHeader=TRUE, plotOutput("histogram")),
                  box(title="Visual2", status="primary", solidHeader=TRUE, plotOutput("histogram2"))
                )
        ),
        tabItem(tabName="archive", shiny::h2("Archive"))
      )
    )
  )
)

################################################################################
################################### R U N ######################################
################################################################################

shinyApp(ui, server)

Solution

  • I solved it as suggested (thank you very much, Stephane) with shinyscreenshot.

    library(shiny)
    library(shinydashboard)
    library(shinyscreenshot)
    library(data.table)
    library(DT)
    library(bslib)
    library(plotly)
    
    ################################################################################
    ################################ S E R V E R ###################################
    ################################################################################
    
    server = shinyServer(function(input,output){
      
      output$histogram = renderPlotly(
        ggplotly(ggplot(mtcars, aes(x=disp, y=hp, color=gear)) + geom_point())
      )
      
      
      output$active_cases = DT::renderDataTable(
        mtcars)
      
      ######################### SCREENSHOT REACTIVE FUNCTION #########################  
      observeEvent(input$go,{
        screenshot(id = "to_plot") # plot only ID "to_plot"
      })
      ######################### SCREENSHOT REACTIVE FUNCTION #########################
      
    })
    
    
    ################################################################################
    #################################### U I #######################################
    ################################################################################
    
    ui = shinyUI(
      dashboardPage(
        dashboardHeader(
          title="just a test"
        ),
        dashboardSidebar(
          sidebarMenu(id="tabs",
                      menuItem("Tab1", tabName="active_cases", icon = icon("magnifying-glass-location"))
          )
        ),
        dashboardBody(
          tabItems(
            tabItem(tabName="active_cases", shiny::h2("Active Cases"),
              fluidRow(actionButton("go", "go")),
                  fluidRow(
                    box(title="Table", status="primary", solidHeader=TRUE, div(DT::dataTableOutput("active_cases")))
                  ),
              div(id="to_plot",
                  fluidRow(
                    box(title="Visual1", status="primary", solidHeader=TRUE, plotlyOutput("histogram"))
                  )
              )
    )))))
    
    
    ################################################################################
    ################################### R U N ######################################
    ################################################################################
    
    shinyApp(ui, server)