R shiny 下载不同的图像格式

R shiny download different image formats

我有一个闪亮功能的一部分,用户可以在其中选择下载图像类型(.png、.tiff 等)并单击按钮进行下载。但是所有选项都以 .png 格式下载,我似乎无法找出问题所在。

请注意,预览始终为 png。单击按钮后,下载功能会创建不同的文件类型。另一点需要注意的是在 downloadhandler 中使用 file.copy() 而不是类似 PNG(名称) 阴谋() dev.off() 这是因为我的作图函数比较复杂,file.copy()比较实用。 代码如下。

#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)
    }
  )
})

删除下载文件名中的括号解决了这个问题。是的,甚至不要问。我也不知道为什么会这样。但它有效。

乔诚的回答:

更改此行:

filename = fn_downloadname(),

对此:

filename = fn_downloadname,