1

I'm attempting to adapt a function written by a coworker, but I've hit a point where neither of us know how to solve the issue. Generally, the function reads in several different shapefiles kept in different directories, then merges them all with another necessary .csv dataset (facilitycrosswalk). I haven't really worked with spatial data in R before, so some of this is totally foreign to me--apologies for anything confusing as this is not my code. Here's a general recreation:

#working directory
working <- "C:/my/main/directory/"

#lists feeding into function
county <- c("County1", "County2", "County3", "County4", "County5")
longsitenames <- c("Site Name 1", "Site Name 2", "Site Name 3")
shortsitenames <- c("SN1", "SN2", "SN3")

#creating function to read and merge all shapefiles
readshapefiles <- function(county){
  map2(.x = longsitenames,
       .y = shortsitenames,
       .f =  ~ rgdal::readOGR(
         glue("{working}Shared_Data/Spatial Data/Site Data/{county} County/{.x}/Final Shapefile/{.y}.shp" )) %>%
         sp::merge(x = .,
                   y = facilitycrosswalk %>% 
                     mutate(Name = glue("{`Facility Site Name`} Service Area")),
                   by.x = "Name", 
                   by.y = "Name",
                   all.x = T, 
                   all.y = F)  %>% 
         st_as_sf(as(., "Spatial")) %>% 
         filter(method=="censusblock areal interpolation")) %>%
    setNames(c(sitenameslong))
}

#running function and formatting output
shp_all <- readshapefiles(county) %>% 
  reduce(., rbind.sf) %>% 
  st_as_sf(as(., "Spatial")) %>% 
  st_transform(st_crs(wgs84)) 

This gives the error "Error in (function (cond) : error in evaluating the argument 'x' in selecting a method for function 'merge': length(dsn) == 1L is not TRUE"

When I attempt to run the function on one specific county it seems to work okay, which makes me think it's having issues iterating over the list of counties, but I'm not sure why that would be. Any help is greatly appreciated!

kbill
  • 11
  • 3
  • If it works on a single county, does it work if you pass the vector of counties to the function 'one at a time', e.g. `shp_all <- purrr::map(county, readshapefiles) %>% reduce (etc)`? – jared_mamrot Oct 17 '22 at 23:11
  • That seems to have worked for the error listed above...now it's giving me a "cannot open data source" error. Directory organization and file naming shouldn't (?) be an issue since my coworker runs this function daily on the same set of files – kbill Oct 17 '22 at 23:29
  • Is `facilitycrosswalk` available in the current environment? – jared_mamrot Oct 18 '22 at 00:00
  • Yes it is, it gets used earlier in the script to create `longsitenames` and `shortsitenames` – kbill Oct 18 '22 at 17:12
  • Hmmm... Sorry @kbill, not sure what the issue could be. You could edit your question to include more details, or it would probably be better to create a new question specific to this 'new' problem (to get more attention from other SO users) – jared_mamrot Oct 19 '22 at 01:38

0 Answers0