将shapefile从目录批量加载到R

将shapefile从目录批量加载到R,r,gis,batch-processing,shapefile,rgdal,R,Gis,Batch Processing,Shapefile,Rgdal,我想回答一些问题,无意中发现: 我发现如果措辞更恰当,这个问题可能会产生更大的影响 我认为用户的意思是询问一种从特定目录将shapefile批量加载到R中的方法 我在下面提供我的解决方案。我欢迎任何对如何增强它有任何建议的人,请在下面给出自己的答案。#——从这里下载shapefile: #--download shapefiles from here: #--https://www.census.gov/geo/maps-data/data/cbf/cbf_cds.html

我想回答一些问题,无意中发现:

我发现如果措辞更恰当,这个问题可能会产生更大的影响

我认为用户的意思是询问一种从特定目录将shapefile批量加载到R中的方法

我在下面提供我的解决方案。我欢迎任何对如何增强它有任何建议的人,请在下面给出自己的答案。

#——从这里下载shapefile:
    #--download shapefiles from here:
    #--https://www.census.gov/geo/maps-data/data/cbf/cbf_cds.html

    wd <- "/My Shapefiles"
    setwd(wd)

    #--store shapefile names as a list
    shp_files <- list.files(wd, pattern = "\\.shp$")

    #--inspect list
    print(shp_files)

    ####################################
    # Batch shapefile loading function #
    ####################################

    rgdal_batch_shp <- function(shp_list) {

      layer_name <- as.character(gsub(".shp","",shp_list))

      shp_spdf <-readOGR(dsn = wd, stringsAsFactors = FALSE, verbose = TRUE, 
                         useC = TRUE, dropNULLGeometries = TRUE, addCommentsToPolygons = TRUE,
                         layer = layer_name, require_geomType = NULL,
                         p4s = NULL, encoding = 'ESRI Shapefile')
    }

    #########################################
    # Pass batch function to shapefile list #
    #########################################

    library(rgdal) #--for readOGR function

    #--Use lapply to pass rgdal_batch_shp function to files list.

    batch_shp_list <- lapply(shp_files, rgdal_batch_shp)

    #--Extract each element in list into its own object

    for (i in seq(batch_shp_list))
      assign(paste("test_shp", i, sep = ""), batch_shp_list[[i]])
#--https://www.census.gov/geo/maps-data/data/cbf/cbf_cds.html
wd不是一个更好的发布解决方案的地方,而不是打开一个新的重复的帖子吗?@MauritsEvers我想了想。然而,我认为这将“奖励”另一篇文章的作者在其当前状态下提交问题的方法(没有清晰性,没有可复制的示例,没有起始代码)。我觉得在那里发布我的解决方案会鼓励那个人继续以这种方式提交问题。