Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
814 views
in Technique[技术] by (71.8m points)

download - Downloading png from Shiny (R)

I am pretty new to Shiny (and R) and struggling with exporting the plot I make in Shiny to a png-file.

I looked at these two threads but could not figure it out:

Save plots made in a shiny app Shiny downloadHandler doesn't save PNG files

I manage to create the download button in the ui and the server seems to be doing everything I want it to do, too. When I hit the download button in the preview window, a pop up window asks me to specify the file location and name but no file is saved. When I do the same in a browser window, a png file is created but it is empty.

Any insight is much appreciated!

ui.R

library(shiny)

shinyUI(fluidPage(
  titlePanel("This is a scatterplot"),

  sidebarLayout(
    sidebarPanel(

      fileInput('datafile', 'Choose CSV file',
                accept=c('text/csv', 'text/comma-separated-values,text/plain')),

      uiOutput("varselect1"),

      uiOutput("varselect2"),

      downloadButton('downloadPlot', 'Download Plot')

      ),

    mainPanel(          
          h4("Here is your scatterplot"),
          plotOutput("plot1")
                  )
      ))
)

server.R

library(foreign)

shinyServer(function(session,input, output) {

    DataInput <- reactive({
      infile <- input$datafile
      if (is.null(infile)) {

        return(NULL)
      }
      read.csv(infile$datapath)
    })


    output$varselect1 <- renderUI({

      if (identical(DataInput(), '') || identical(DataInput(),data.frame())) return(NULL)

      cols <- names(DataInput())
      selectInput("var1", "Select a variable:",choices=c("---",cols[3:length(cols)]), selected=("---"))

    })

    output$varselect2 <- renderUI({

      if (identical(DataInput(), '') || identical(DataInput(),data.frame())) return(NULL)

      cols <- names(DataInput())
      selectInput("var2", "Select a variable:",choices=c("---",cols[3:length(cols)]), selected=("---"))

    })



    plotInput <- reactive({

      a <- which(names(DataInput())==input$var1)
      x_lab <- as.numeric(DataInput()[,a])


      b <- which(names(DataInput())==input$var2)
      y_lab <- as.numeric(DataInput()[,b])      

      main.text <- paste("Scatterplot of the variables",colnames(DataInput())[a],"and", colnames(DataInput())[b],sep = " ", collapse = NULL)

      plot(x_lab, y_lab, main=main.text, xlab=colnames(DataInput())[a], ylab=colnames(DataInput())[b], xlim=c(min(x_lab),max(x_lab)*1.05), ylim=c(min(y_lab), max(y_lab)*1.05))

      observations <- DataInput()[,1]

      text(x_lab, y_lab, labels=observations, pos=3)


    })

    output$plot1 <- renderPlot({
          print(plotInput())
    })


    output$downloadPlot <- downloadHandler(
      filename = "Shinyplot.png",
      content = function(file) {
        png(file)
        print(plotInput())
        dev.off()
      })    

  })  
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

A workaround for this strange scenario was discussed on the shiny-discuss google group. What you can do is simply change your reactive plotInput statement into a normal function. Not sure why downloadHandler doesn't play nice with reactive objects.

# change
plotInput <- reactive({...})

# into this
plotInput <- function(){...}

You can also remove the print statement in the downloadHandler call:

output$downloadPlot <- downloadHandler(
      filename = "Shinyplot.png",
      content = function(file) {
        png(file)
        plotInput()
        dev.off()
      })    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...