I am trying to make a Shiny-based web application that can accept multiple binary files as input from the user, and create a GeoTiff raster file of each input file for the user to download. However, I am unable to download the multiple output GeoTiff files as a zip folder using DownloadHandler of R shiny. The code which I have tried is given below.
library(shiny)
library(raster)
library(rgdal)
library(zip)
library(stringr)
ui = fluidPage(
titlePanel(title = "Convert Binary raster data to GeoTiff"),
sidebarLayout(position = "right",
sidebarPanel(
fileInput(inputId = "layer", label = "Upload your binary raster data", multiple = TRUE)
),
mainPanel(tableOutput("files"),
tableOutput("path"),
downloadButton(outputId = "downloadTiff", label = "Download GoeTiff file")
)
)
)
bin_raster = function(x){
rf.file = file(x, 'rb')
bin_data = readBin(rf.file, what = 'double', n = 120705, size = 4, endian = "big")
rf_matrix = matrix(data = bin_data, nrow = 301, ncol = 401, byrow = T)
rf_rast1 = flip(raster(rf_matrix, xmn = 70, xmx = 110, ymn = 5, ymx = 35,
crs = "+proj=longlat +datum=WGS84"), direction = 2)
return(rf_rast1)
}
server = function(input, output){
inFile = reactive({
if(is.null(input$layer$datapath)){return()}
else{
for (i in input$layer$datapath) {
wb = bin_raster(i)
return(wb)
}
}
})
output$files = renderTable(input$layer)
output$path = renderTable(input$layer$datapath)
output$downloadTiff = downloadHandler(
filename = "GeoTiff.zip",
content = function(file){
#go to a temp dir to avoid permission issues
owd <- setwd(tempdir())
on.exit(setwd(owd))
files <- NULL;
for (i in 1:input$layer$datapath){
fileName <- paste(input$layer$name,i,".tif",sep = "")
writeRaster(inFile()$wb[i],fileName, format = "GTiff", overwrite = TRUE)
files <- c(fileName,files)
}
#create the zip file
zip(file,files)
}
)
}
shinyApp(ui = ui, server = server)
It is showing me the following error:
Warning in 1:input$layer$datapath :
numerical expression has 2 elements: only the first used
Warning in download$func(tmpdata) : NAs introduced by coercion
Warning: Error in :: NA/NaN argument
[No stack trace available]
Please help me.