【发布时间】:2021-06-24 07:10:34
【问题描述】:
我正在尝试制作一个基于 Shiny 的 Web 应用程序,它可以接受多个二进制文件作为用户的输入,并为每个输入文件创建一个 GeoTiff 光栅文件供用户下载。但是,我无法使用 R Shiny 的 DownloadHandler 将多个输出 GeoTiff 文件下载为 zip 文件夹。我尝试过的代码如下。
#Import required packages
library(shiny)
library(raster)
library(rgdal)
library(zip)
library(stringr)
#User Interface
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")
)
)
)
#Function to convert binary file to GeoTIFF raster data
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
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)
它向我显示以下错误: 1:input$layer$datapath 中的警告: 数值表达式有 2 个元素:仅使用第一个元素 download$func(tmpdata) 中的警告:强制引入的 NA 警告: :: NA/NaN 参数中的错误 [没有可用的堆栈跟踪]
请帮帮我。
【问题讨论】:
标签: r shiny download zip raster