4

我想创建一个函数来保存 downloadHandler 函数并动态提供它的详细信息,但我不断收到消息

“找不到模式‘函数’的对象‘plotFunction’”

我的下载功能如下:

downloadPlots <- function(fileName,plotFunction,fileFormat,fileContentType){
if(fileFormat=="pdf"){ #because it doesn't require specification of contentType. Others do
  downloadHandler(
    filename = fileName,
    content = function(file) {          
      pdf(file, pointsize = 12, bg = "white", res = NA)
      FUN <- match.fun(plotFunction) 
      FUN()
      dev.off()
    }
  )
}else{
  downloadHandler(
    filename = fileName,
    content = function(file) {
      if(fileFormat=="png")
        png(file, pointsize = 12, bg = "white", res = NA)
      FUN <- match.fun(plotFunction,descend = TRUE) 
      FUN()
      dev.off()
    },
    contentType = fileContentType
  )
}

}

这就是函数的调用方式

output$histPng <- downloadPlots("histogram.png",histogram(),"png","image/png")

在ui.R中,下载plot的代码如下:

downloadButton('histPng','PNG')
4

1 回答 1

4

我认为问题(没有任何测试)可能是您downloadPlots()错误地传递了您的绘图功能。histogram从函数调用中删除括号:

output$histPng <- downloadPlots("histogram.png",histogram,"png","image/png")
于 2013-06-07T08:44:05.313 回答