Downloadhander (save plot) for basic plot in shiny

前端 未结 1 1288
春和景丽
春和景丽 2021-01-21 06:21

How to save plot using download button in shiny?
I know how to do it for ggplot, but I can\'t find how to do it for basic plot().

Example:

<         


        
相关标签:
1条回答
  • 2021-01-21 06:59

    You can write it on a png object using device graphics. Check the code.

    library(shiny)
    library(ggplot2)
    
    
    # ui
    ui <- fluidPage(
      downloadButton("save", "save")
    )
    
    
    # server
    server <- function(input, output){
    
      p <- reactive({ggplot(iris, aes(Sepal.Length, Sepal.Width)) + geom_point()})
      p2 <- reactive({dotchart(iris$Sepal.Length, iris$Species, iris$Species)})
    
      output$save <- downloadHandler(
        file = "save.png" , # variable with filename
        content = function(file) {
          #ggsave(p(), filename = file)
          png(file = file)
          p2()
          dev.off()
        })
    }
    
    
    # run
    shinyApp(ui, server)
    
    0 讨论(0)
提交回复
热议问题