使用反应对象通过 *zip 创建 shapefile 文件

Create shapefile files with *zip using reactive object

我想在 Shiny 中创建 shapefile 文件。 A 创建在 tempdir() 中更正的文件,包括最终的 *zip 文件。但是输出错误是Error in h: error evaluating argument 'x' in method selection for function 'unique': 'cannot find function "selectedvariable0"'[No stack trace available] 好的,使用 reactive 对象需要小心,但尽管我尝试使用 selectedvariable0()$ID_UNIQUEselectedvariable0()output$selectedvariable0。没有什么对我有用。在我的例子中:

# 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",style = "width:500px")    
    ),
    mainPanel(
      textOutput("idSaida")
    )
  ))

server <- function(input, output, session){
  
  output$download1 <- downloadHandler(
    
    
    filename = function() {
      stands_sel_choose<-unique(input$selectedvariable0)
      paste0(iconv(stands_sel_choose, from = '', to = 'ASCII//TRANSLIT'),"-",Sys.Date(),".zip",sep="")
    },
    content = function(file) {
      stands_sel_choose<-unique(input$selectedvariable0)
      data = stands_ds[stands_ds$ID_UNIQUE %in% stands_sel_choose,] # I assume this is a reactive object
      pts.class = SpatialPoints(data[,1:2], proj4string=crs("+proj=longlat +ellps=GRS80 +towgs84=0,0,0 +no_defs"))
      coor.df <- SpatialPointsDataFrame(pts.class, data.frame(id=1:length(pts.class)))
      # create a temp folder for shp files
      temp_shp <- tempdir()
      # write shp files
      writeOGR(coor.df, dsn=paste0(temp_shp,"/",iconv(stands_sel_choose, from = '', to = 'ASCII//TRANSLIT'),"-",Sys.Date(),".shp",sep=""),layer=paste0(iconv(stands_sel_choose, from = '', to = 'ASCII//TRANSLIT'),"-",Sys.Date(),sep=""), driver="ESRI Shapefile", 
               overwrite = TRUE)
      # zip all the shp files
      zip_file <- file.path(temp_shp, paste0(temp_shp,"/",iconv(stands_sel_choose, from = '', to = 'ASCII//TRANSLIT'),"-",Sys.Date(),".zip",sep=""))
      shp_files <- list.files(temp_shp,
                              paste0(iconv(stands_sel_choose, from = '', to = 'ASCII//TRANSLIT'),"-",Sys.Date(),sep=""),
                              full.names = TRUE)
      # the following zip method works for me in W10
      tar(paste0(temp_shp,"/",iconv(stands_sel_choose, from = '', to = 'ASCII//TRANSLIT'),"-",Sys.Date(),".zip",sep=""), files=shp_files, tar="tar") 
      # copy the zip file to the file argument
      fileZip <- list.files(temp_shp, pattern="\.zip$")
      file.copy(fileZip[1], file)
      # remove all the files created
      do.call(file.remove, list(list.files(temp_shp, full.names = TRUE)))
    }
  )    
}
shinyApp(ui, server)
##

拜托,对我正确上传 *zip 文件中的 shapefile 有什么帮助吗?

现在添加 zip 库并删除不创建“/”以不创建路径混淆:

# Packages
library(rgdal)
library(raster)
library(shiny)
library(shinythemes)
library(lubridate)
library(dplyr)
library(zip)
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",style = "width:500px")    
    ),
    mainPanel(
      textOutput("idSaida")
    )
  ))

server <- function(input, output, session){
  
  output$download1 <- downloadHandler(
    
    
    filename = function() {
      stands_sel_choose<-unique(input$selectedvariable0)
      paste0(gsub("-*_","",iconv(stands_sel_choose, from = '', to = 'ASCII//TRANSLIT'),Sys.Date()),".zip",sep="")
    },
    content = function(file) {
      stands_sel_choose<-unique(input$selectedvariable0)
      data = stands_ds[stands_ds$ID_UNIQUE %in% stands_sel_choose,] # I assume this is a reactive object
      pts.class = SpatialPoints(data[,1:2], proj4string=crs("+proj=longlat +ellps=GRS80 +towgs84=0,0,0 +no_defs"))
      coor.df <- SpatialPointsDataFrame(pts.class, data.frame(id=1:length(pts.class)))
      # create a temp folder for shp files
      setwd(tempdir())
      temp_shp <-tempdir()
      # write shp files
      writeOGR(coor.df, dsn=paste0(gsub("-*_","",iconv(stands_sel_choose, from = '', to = 'ASCII//TRANSLIT'),Sys.Date()),".shp",sep=""),layer=paste0(gsub("-*_","",iconv(stands_sel_choose, from = '', to = 'ASCII//TRANSLIT'),Sys.Date()),sep=""), driver="ESRI Shapefile", 
               overwrite = TRUE)
      # zip all the shp files
      #zip_file <- file.path(paste0(gsub("-*_","",iconv(stands_sel_choose, from = '', to = 'ASCII//TRANSLIT'),Sys.Date()),".zip",sep=""))
      shp_files <- list.files(temp_shp,paste0(gsub("-*_","",iconv(stands_sel_choose, from = '', to = 'ASCII//TRANSLIT'),Sys.Date()),sep=""),
                              full.names = TRUE)
      # the following zip method works for me in W10
      #tar(paste0(gsub("-*_","",iconv(stands_sel_choose, from = '', to = 'ASCII//TRANSLIT'),Sys.Date()),".zip",sep=""), files=shp_files, tar="tar") 
      zip::zipr(zipfile = paste0(gsub("-*_","",iconv(stands_sel_choose, from = '', to = 'ASCII//TRANSLIT'),Sys.Date()),".zip",sep=""), files = shp_files)
      req(file.copy(paste0(gsub("-*_","",iconv(stands_sel_choose, from = '', to = 'ASCII//TRANSLIT'),Sys.Date()),".zip",sep=""), file))
      #remove all the files created
      #do.call(file.remove, list(list.files(temp_shp, full.names = TRUE)))
    }
  )    
}
shinyApp(ui, server)
##