我有一个 shapefile 存储在 Dropbox 文件夹中,我想在 Shiny 应用程序中显示它。我可以从 Dropbox 下载 URL 下载 .zip 文件,解压缩该文件,然后使用
st_read()
包中的 sf
读取 .shp 文件。当我在 R 中本地运行 Shiny 应用程序时,此方法有效,但当应用程序部署在闪亮的.myorganization.org 上时,此方法无效。
我尝试通过 Dropbox 共享 URL 将 shapefile 直接从 Dropbox 读取到
st_read()
,但收到错误,Cannot open "URL"; The file doesn't seem to exist.
在尝试读取 .zip 文件夹(解压文件夹)的 Dropbox 共享 URL 时收到相同的错误,和直接 .shp 文件。
我的下面的代码在 R 中工作但在部署的 Shiny 应用程序上工作是否存在问题,因为需要下载并解压缩文件?有办法绕过这个吗?我在闪亮的一面缺少什么吗?也许缺少软件包或权限?
我想将 .shp 文件存储在 Dropbox 中,而不是存储在存储 Shiny 应用程序的 GitHub 存储库中。我以为我在 this post 中找到了潜在的解决方案,但它使用的
rdrop2
包不再受支持。
谢谢你。
# load libraries
library(sf)
library(leaflet)
library(shiny)
# load data
# download folder from dropbox that contains the shapefile
aFile<- "https://www.dropbox.com/myshapefiletodownload/key=1"
download.file(url = aFile, destfile = "myshapefiletodownload.dir", mode = "wb")
# unzip the folder to see the files
unzip("myshapefiletodownload.dir")
# read in the shapefile
df<- st_read(dsn = "myshapefile.shp", layer = "df")
# define UI
ui<- fluidPage(
mainPanel(leafletOutput("map"))
)
# Define server logic
server<- function(input, output) {
output$map<- renderLeaflet({
leaflet(df) %>%
addTiles() %>%
addPolygons() %>%
})
}
# Run the app
shinyApp(ui, server)
sf
使用 GDAL
读取数据,这意味着您可以将 GDAL 虚拟文件系统 与 sf
一起使用——假设您有一个 Shapefile 存档的有效直接下载链接,请在其前面加上 "/vsizip//vsicurl/"
让 GDAL 处理下载和提取:
url_ <- "https://raw.githubusercontent.com/OSGeo/gdal/master/autotest/ogr/data/shp/poly.zip"
sf::read_sf(paste0("/vsizip//vsicurl/", url_))
#> Simple feature collection with 10 features and 3 fields
#> Geometry type: POLYGON
#> Dimension: XY
#> Bounding box: xmin: 478315.5 ymin: 4762880 xmax: 481645.3 ymax: 4765610
#> Projected CRS: OSGB36 / British National Grid
#> # A tibble: 10 × 4
#> AREA EAS_ID PRFEDEA geometry
#> <dbl> <dbl> <chr> <POLYGON [m]>
#> 1 215229. 168 35043411 ((479819.8 4765181, 479690.2 4765260, 479647 476537…
#> 2 247328. 179 35043423 ((480035.3 4765559, 480039 4765540, 479730.4 476540…
#> 3 261753. 171 35043414 ((479819.8 4765181, 479859.9 4765270, 479909.9 4765…
#> 4 547597. 173 35043416 ((479014.9 4765148, 479029.7 4765111, 479117.8 4764…
#> 5 15776. 172 35043415 ((479029.7 4765111, 479046.5 4765117, 479123.3 4765…
#> 6 101430. 169 35043412 ((480083 4765050, 480080.3 4764980, 480134 4764857,…
#> 7 268598. 166 35043409 ((480389.7 4764950, 480537.2 4765014, 480568 476491…
#> 8 1634833. 158 35043369 ((480701.1 4764738, 480761.5 4764778, 480825 476482…
#> 9 596610. 165 35043408 ((479750.7 4764702, 479968.5 4764788, 479985.1 4764…
#> 10 5269. 170 35043413 ((479750.7 4764702, 479658.6 4764670, 479640.1 4764…
创建于 2024-03-12,使用 reprex v2.1.0