Hi Everyone!!
In my script, the zip
is not created in shinyapps.io and I don't know why, because with the same code in my local machine all works fine and the zip
file is created. Please, any help for me to upload my *zip
file correctly? In my example:
# 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)
##