R下载不同的图像格式

R下载不同的图像格式,r,shiny,R,Shiny,我有一个闪亮功能的一部分,用户可以选择下载图像类型(.png、.tiff等),然后单击按钮下载。但所有选项都是以.png格式下载的,我似乎无法找出问题所在 请注意,预览始终为png。单击按钮后,下载功能将创建不同的文件类型。另一点需要注意的是在downloadhandler中使用file.copy(),而不是类似的用法 巴布亚新几内亚(名称) 绘图() 发展主任() 这是因为我的绘图函数很复杂,file.copy()更实用。 代码如下 #ui.R ------------------------

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

请注意,预览始终为png。单击按钮后,下载功能将创建不同的文件类型。另一点需要注意的是在downloadhandler中使用file.copy(),而不是类似的用法 巴布亚新几内亚(名称) 绘图() 发展主任() 这是因为我的绘图函数很复杂,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)
    }
  )
})
#ui.R----------------------------------------------------------
shinyUI(fluidPage)(
titlePanel(“下载测试”),
侧边栏布局(
侧栏面板(
数值输入(“FHight”,“Height(cm)”,最小值=2,最大值=15,步长=1,值=10),
数值输入(“fwidth”,“宽度(cm)”,最小值=2,最大值=15,步长=1,值=10),
选择输入(“fres”、“Res”,choices=c(“100”、“200”、“300”),selected=“100”),
选择输入(“格式”、“文件类型”、选项=c(“png”、“tiff”、“jpeg”、“pdf”)、选择的=png、多个=FALSE、选择的=TRUE),
downloadButton('bn_download','download Plot')
),
#显示生成的分布图
主面板(
图像输出(“打印输出”)
)
)
))
#服务器.R----------------------------------------------------------
shinyServer(功能(输入、输出){
#存储一些值

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

郑若骅的回答是:

更改此行:

filename = fn_downloadname(),
为此:

filename = fn_downloadname,

请看这里,参数filename需要一个函数或字符串。但是reactive()对象不只是返回字符串,因此您需要传递reactive函数本身,这是通过不使用括号来完成的。不知道确切的细节,但这是此行为背后的一般想法,相当令人困惑。