我正在尝试从zip存档列表中读入shapefile列表,而不实际解压缩存档。是的,我知道存档将在后台解压缩,但我想避免的是在Windows资源管理器中看到解压缩的文件。
这个例子可以完全重现,你需要从this Github repository下载所有的文件,并将你的工作目录设置为你下载它们的文件夹。
我还想用tidyverse-style,使用管道,不保存中间对象。
我现在要运行的代码是这样的:
library(tidyverse)
library(magrittr)
library(sf)
list.files() %>%
map(unzip, list = T) %>%
map(filter, grepl(".shp$", Name)) %>%
map(~ .x %$% Name) %>%
map2(.x = ., .y = list.files(), .f = ~st_read(unzip(zipfile = .y, files = .x)))
但是,这行不通。为什么?
EDIT:为了使示例更简洁,我想您也可以从上面的存储库中只下载两个文件。
2条答案
按热度按时间kyks70gy1#
/vsizip
GDAL虚拟文件系统驱动程序非常方便:创建于2023年2月5日,使用reprex v2.0.2
lawou6xi2#
您可以定义一个小函数来下载zip文件,解压缩它,将shape文件读入内存,删除临时文件,然后返回
sf
对象。下面的函数可以完成所有这些操作:
所以,现在没有任何mucking约在文件资源管理器,我们可以做:
这似乎是阿拉斯加城市边界的形状文件,为了完整起见,让我们将其绘制出来:
创建于2023年2月5日,使用reprex v2.0.2