You need to enable JavaScript to run this app. Shiny使用reactive对象创建zip格式shapefile报错如何解决
优惠活动
大模型
产品
解决方案
定价
更多

Shiny使用reactive对象创建zip格式shapefile报错如何解决

Shiny导出ZIP格式Shapefile修复方案

错误原因

  • 核心报错cannot find function "selectedvariable0"由错误的取值写法导致:Shiny界面组件的取值规则为input$组件ID,selectedvariable0是selectInput的ID,属于普通值对象不是reactive函数,不需要加()后缀,你之前尝试的selectedvariable0()、output$selectedvariable0都是无效写法。
  • 原有代码还存在shapefile写入路径错误、压缩格式不匹配、临时文件清理逻辑不合理的问题,会导致导出的压缩包无法正常识别。

修复后的完整代码

# Packages
library(rgdal)
library(raster)
library(shiny)
library(shinythemes)
library(lubridate)
library(dplyr)
require(gdalUtils)

# get AOI
download.file(
  "https://github.com/Leprechault/trash/raw/main/stands_example.zip",
  zip_path <- tempfile(fileext = ".zip")
)
unzip(zip_path, exdir = tempdir())

# Open the files
setwd(tempdir())
stands_extent <- readOGR(".", "stands_target") # Border
stands_ds <- read.csv("pred_target_stands.csv", sep=";") # Data set
stands_ds <- stands_ds %>%
  mutate(DATA_S2 = ymd(DATA_S2))

# Create the shiny dash
ui <- fluidPage(
  theme = shinytheme("cosmo"),
  titlePanel(title="My Map Dashboard"),  
  sidebarLayout(
    sidebarPanel(
      selectInput(inputId = "selectedvariable0", "ID-Unique", choices = c(unique(stands_ds$ID_UNIQUE))),
      downloadButton("download1", "Salvar em *shp",style = "margin-top: 10px; margin-bottom: 10px; width:500px")    
    ),
    mainPanel(
      textOutput("idSaida")
    )
  ))

server <- function(input, output, session){
  
  output$download1 <- downloadHandler(
    filename = function() {
      stands_sel_choose <- input$selectedvariable0
      paste0(iconv(stands_sel_choose, from = '', to = 'ASCII//TRANSLIT'),"-",Sys.Date(),".zip")
    },
    content = function(file) {
      stands_sel_choose <- input$selectedvariable0
      data = stands_ds[stands_ds$ID_UNIQUE %in% stands_sel_choose,]
      pts.class = SpatialPoints(data[,1:2], proj4string=crs("+proj=longlat +ellps=GRS80 +towgs84=0,0,0 +no_defs"))
      coor.df <- SpatialPointsDataFrame(pts.class, data)
      
      # 单独创建临时子目录存储当前导出的shp文件,避免污染全局临时目录
      temp_shp <- tempfile()
      dir.create(temp_shp, showWarnings = FALSE)
      file_prefix <- paste0(iconv(stands_sel_choose, from = '', to = 'ASCII//TRANSLIT'),"-",Sys.Date())
      
      # 正确写入shapefile:dsn为目录路径,layer为文件名前缀
      writeOGR(coor.df, dsn = temp_shp, layer = file_prefix, driver="ESRI Shapefile", overwrite = TRUE)
      
      # 用zip函数生成标准zip压缩包,-j参数去掉文件路径,直接把shp相关文件放在压缩包根目录
      zip_path <- file.path(temp_shp, paste0(file_prefix, ".zip"))
      shp_files <- list.files(temp_shp, pattern = paste0("^", file_prefix), full.names = TRUE)
      zip(zipfile = zip_path, files = shp_files, flags = "-j")
      
      # 复制压缩包到下载路径
      file.copy(zip_path, file)
      # 清理临时子目录
      unlink(temp_shp, recursive = TRUE)
    }
  )    
}
shinyApp(ui, server)

内容的提问来源于stack exchange,提问作者Leprechault

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.17 01:54:08