Search code examples
rimageshinydownloadlattice

Shiny application can't download lattice plot


First of all i'm beginner at Shiny. I'm trying to save locally the plot (using button) i made in my application with lattice package. However, I am unable to create a corresponding function in the server.

Please help

download button and plot in my UI:

downloadButton('downloadPlot2', 'Save lattice plot locally')
.
.
.
tabPanel(names(panel_choices)[2], value = panel_choices[[2]], plotOutput("plot2"))

Server:

output$plot2 <- renderPlot({
    print(plotInput2())
  })
  
  plotInput2 <- reactive({
    md <- mydata()
    md.long <- melt(md, id = "ID", measure = colnames(md)[-1])
    xyplot(value~md.long[,1],data = md.long,main="visualization of data from csv file",ylim = c(input$value[1],input$value[2]),
           xlab = "ID",ylab = "values of variables")
  })

Solution

  • You can do this with downloadHandler, just paste this inside your server:

    output$downloadPlot2 <- downloadHandler(
        filename = '',
        content = function(file) {
          png(file)
          print(plotInput2())
          dev.off()
        })