Download an object in tab1 of shiny app, when the 'download' button is in tab2?

I'm using shinyscreenshot to download a plot that exists in tab1 of my shiny app. When I try to use the exact same method but place the download button in tab2 (still wanting plot from tab1), this fails. How can I make this work?

Simple, self-contained, minimal reprex :

library(shiny)
library(shiny.semantic)
library(semantic.dashboard)
library(shinyscreenshot)
library(ggplot2)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(side = "left",
                   sidebarMenu(
                     menuItem(tabName = "tab1", text = "tab1", icon = icon("home")),
                     menuItem(tabName = "tab2", text = "tab2", icon=icon("home")))),
  dashboardBody(
    tabItems(
      tabItem(
        tabName = "tab1",
        fluidRow(
          box(title = "tab1", 
              column(5,
              plotOutput("myplot")),
              actionButton("download_plot_test1", "download_plot_test1") #Screenshot the plot
              ))),
      
      tabItem(
        tabName = "tab2",
        fluidRow(
          box(title = "tab2", 
              width = 7,
              column(5,
                     actionButton("download_plot_test2", "download_plot_test2"))))))))
      
      

server <- shinyServer(function(input, output) {
  
  #Render simple ggplot
  output$myplot <- renderPlot({ggplot(mtcars, aes(x=cyl, y=mpg)) + geom_point()}) 
  
  #Test1 (it works)
  observeEvent(input$download_plot_test1, {
    screenshot(id="myplot", scale=2)
  })
  
  #Test2 (does not work?)
  observeEvent(input$download_plot_test2, {
    screenshot(id="myplot", scale=2)
  })
  
}) #End shinyServer

#Run the app
shinyApp(ui, server)

Hi,

As far as I'm aware, shinyscreenshot can only be used for taking a screenshot of the currently visible screen. If you want to download a plot in a different tab you're best off using some combination of downloadHandler() and ggsave() - see here.

Hope that helps.

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.