I'm developing a package that primarily consists of spatial data files for various geographies in New York City (boroughs, community districts, census tracts, etc.) as well as some helper functions to retrieve and filter these objects. I have each of the geographies saved as .rda files in the /data folder of the package so I can use the objects by referring to their name, like tracts_sf.
The main function in the package lets a user specify a geography (like borough, cd, tract) and returns an sf object with those boundaries. For example nyc_boundaries(geography = "borough") will return an sf object of the borough boundaries.
I'm wondering the best way to reference the appropriate data file to return (in this case boros_sf) in my function. Currently, I'm creating strings of the data files based on the input argument and then using get() to return the correct file, but this doesn't feel like the best way to do things. Here is a simplified version of my function:
nyc_boundaries <- function(geography = c("borough", "cd", "tract"),
resolution = c("low", "high")) {
geography <- match.arg(geography)
resolution <- match.arg(resolution)
if (geography == "borough") {
.geo <- "boros"
} else if (geography == "cd") {
.geo <- "cds"
} else {
.geo <- "tracts"
}
if (resolution == "low") {
.shp_call <- paste0(.geo, "_sf_simple")
} else {
.shp_call <- paste0(.geo, "_sf")
}
shp <- get(.shp_call)
return(shp)