R brilliant download of various image formats

I have a brilliant function piece where the user selects the type of upload image (.png, .tiff, etc.) and clicks a button to download it. But all parameters are loaded in .png format and I can't figure out what is wrong.

Please note that the preview is always png. The upload function creates different types of files when the button is clicked. Another note is the use of file.copy () in the loader and not in something like PNG (name) plot () dev.off () This is because my plotting function is complex and the .copy () file more practical. The code is below.

#ui.R ----------------------------------------------------------
shinyUI(fluidPage(

  titlePanel("Download test"),

  sidebarLayout(
    sidebarPanel(
      numericInput("fheight", "Height (cm)", min=2, max=15, step=1, value = 10),
      numericInput("fwidth", "Width (cm)", min=2, max=15, step=1, value = 10),
      selectInput("fres", "Res", choices=c("100","200","300"), selected = "100"),
      selectInput("fformat", "File type", choices=c("png","tiff","jpeg","pdf"), selected = "png", multiple = FALSE, selectize = TRUE),
      downloadButton('bn_download', 'Download Plot')
    ),

    # Show a plot of the generated distribution
    mainPanel(
      imageOutput("plotoutput")
    )
  )
))


# server.R ----------------------------------------------------------
shinyServer(function(input, output) {

  # store some values
  store <- reactiveValues(dname="AwesomeDownload")

  # data creation
  fn_data <- reactive({
    df <- data.frame(x=rnorm(50),y=rnorm(50))
  })

  # create filename
  fn_downloadname <- reactive({

    if(input$fformat=="png") filename <- paste0(store$dname,".png",sep="")
    if(input$fformat=="tiff") filename <- paste0(store$dname,".tif",sep="")
    if(input$fformat=="jpeg") filename <- paste0(store$dname,".jpg",sep="")
    if(input$fformat=="pdf") filename <- paste0(store$dname,".pdf",sep="")
    return(filename)
  })

  # render png preview
  output$plotoutput <- renderImage({

    df <- fn_data()
    fheight <- input$fheight
    fwidth <- input$fwidth
    fres <- as.numeric(input$fres)

    png(paste0(store$dname,".png",sep=""), height=fheight, width=fwidth, res=fres, units="cm")
    plot(df)
    dev.off()

    return(list(src = paste0(store$dname,".png",sep=""),
                contentType = "image/png",
                width = round((input$fwidth*as.numeric(input$fres))/2.54, 0),
                height = round((input$fheight*as.numeric(input$fres))/2.54, 0),
                alt = "plot"))
  },deleteFile=TRUE)

  # download function
  fn_download <- function()
    {

    df <- fn_data()
    fheight <- input$fheight
    fwidth <- input$fwidth
    fres <- as.numeric(input$fres)

    if(input$fformat=="pdf") fheight <- round(fheight*0.3937,2)
    if(input$fformat=="pdf") fwidth <- round(fwidth*0.3937,2)

    if(input$fformat=="png") png(fn_downloadname(), height=fheight, width=fwidth, res=fres, units="cm")
    if(input$fformat=="tiff") tiff(fn_downloadname(), height=fheight, width=fwidth, res=fres, units="cm",compression="lzw")
    if(input$fformat=="jpeg") jpeg(fn_downloadname(), height=fheight, width=fwidth, res=fres, units="cm",quality=100)
    if(input$fformat=="pdf") pdf(fn_downloadname(), height=fheight, width=fwidth)
    plot(df)
    dev.off()
  }

  # download handler
  output$bn_download <- downloadHandler(
    filename = fn_downloadname(),
    content = function(file) {
      fn_download()
      file.copy(fn_downloadname(), file, overwrite=T)
    }
  )
})

      

+3


source to share


1 answer


Removing the parentheses in the filename of the download resolves the problem Yes, don't even ask. I have no idea why this is so. But it works.

Joe Cheng's answer:

Change this line:



filename = fn_downloadname(),

      

:

filename = fn_downloadname,

      

+1


source







All Articles