首页 > 解决方案 > If 语句导致闪亮应用程序中的 downloadhandler() 出现问题

问题描述

我在下面有一个闪亮的应用程序,它可以下载 shapefile。该应用程序运行良好,直到我添加if else条件并收到unexpected token ','通知。为什么会这样?如果我删除,我得到:

Error in shinysession$registerDownload: argument "content" is missing, with no default

如果我离开它,该应用程序根本无法运行。

require(shiny)
require(sp)
require(rgdal)
Sys.setenv("R_ZIPCMD" = "C:/Rtools/bin/zip.exe")

runApp(
  list(
    ui = bootstrapPage(
      fileInput('inputdata', 'Input file',accept=c('.csv')),
      selectInput("select", label = "Choose a dataset", 
                  choices = c("Tree" , "Crowns"), 
                  selected = "Tree"),
      downloadButton('downloadShp', 'DownloadSHP')
    ),
    server = function(input, output) {

      createShp <- reactive({
        myXY <- input$inputdata
        if (is.null(myXY)){
          return(NULL)      
        } else {
          xyPoints <- read.table(myXY$datapath, sep=",", header=T)

          SHP <- SpatialPointsDataFrame(coords= cbind(xyPoints[,1:2]), data =  xyPoints)
          proj4string(SHP) <- CRS("+init=epsg:4326")
          return(SHP)
        }
      })

      output$downloadShp <- downloadHandler(
        if(input$select=="Tree"){
        filename = function() { paste0("shpExport.zip") }, #paste('shpExport.zip',
        content = function(file) {
          if (length(Sys.glob("shpExport.*"))>0){
            file.remove(Sys.glob("shpExport.*"))
          }
          writeOGR(createShp(), dsn="shpExport.shp", layer="shpExport", driver="ESRI Shapefile")
          zip(zipfile='shpExport.zip', files=Sys.glob("shpExport.*"),zip = Sys.getenv("R_ZIPCMD", "zip"))
          file.copy("shpExport.zip", file)
          if (length(Sys.glob("shpExport.*"))>0){
            file.remove(Sys.glob("shpExport.*"))
          }
        }
        }
        else{
          filename = function() { paste0("shpExport2.zip") }, #paste('shpExport.zip',
          content = function(file) {
            if (length(Sys.glob("shpExport2.*"))>0){
              file.remove(Sys.glob("shpExport2.*"))
            }
            writeOGR(createShp(), dsn="shpExport2.shp", layer="shpExport2", driver="ESRI Shapefile")
            zip(zipfile='shpExport2.zip', files=Sys.glob("shpExport2.*"),zip = Sys.getenv("R_ZIPCMD", "zip"))
            file.copy("shpExport2.zip", file)
            if (length(Sys.glob("shpExport2.*"))>0){
              file.remove(Sys.glob("shpExport2.*"))
            }
          } 
        }
      )

    }) 
)

标签: rif-statementshinydownload

解决方案


downloadHandler是一个函数,所以它的参数需要是实际参数。你不能做

downloadHandler(
  if (condexpr) {
    filename = ...,
    content = ...
  } else {
    filename = ...,
    content = ...
  }
)

这不是合法的 R 语法。

相反,像

downloadHandler(
  filename = if (condexpr) func1 else func2,
  content = if (condexpr) ... else ...
)

或者

downloadHandler(
  filename = function() if (condexpr) expr1 else expr2,
  content = function(file) if (condexpr) expr1 else expr2
)

而且由于?downloadHandler状态

可以从此函数中使用反应值和函数

对于filename=content=参数,您仍然可以(input$select=="Tree")用作条件。


推荐阅读