Home > Software design >  R shiny- add download button to non-ggplot plots
R shiny- add download button to non-ggplot plots

Time:12-13

I was wondering if there is a short solution (with a package?) to add a download button to non-ggplots in shiny, since my app produces many plots. Thank you.

CodePudding user response:

shinyscreenshot is a great package for screenshot shiny apps. You can create an image of the entire page of just specific elements like plots. The screenshotButton() function will create a button to save any element in your app.

CodePudding user response:

A possible workaround is to create a function to make the plot or plots and call it inside a downloadHandler.

App

library(shiny)

mk_plot <- function(file, plots = NULL) {
  jpeg(file) # open
  par(mfrow=c(2,2))
  plot(rnorm(30))
  plot(rnorm(30))
  # plot code
  dev.off() # close
}

shinyApp(ui, server)

ui <- fluidPage(
  downloadLink("downloadData", "Download")
)

server <- function(input, output) {
  data <- mtcars

  output$downloadData <- downloadHandler(
    filename = function() {
      paste("data-", Sys.Date(), ".jpeg", sep = "")
    },
    content = function(file) {
      mk_plot(file)
    }
  )
}


shinyApp(ui, server)
  • Related