diff --git a/.gitignore b/.gitignore index e4e2036..bab4248 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ .Rproj.user .Rhistory inst/doc -naijR.Rproj diff --git a/DESCRIPTION b/DESCRIPTION index 2a5c1f2..b9ac204 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -2,7 +2,7 @@ Package: naijR Type: Package Title: Operations to Ease Data Analyses Specific to Nigeria Version: 0.5.2 -Date: 2023-06-13 +Date: 2023-06-14 Depends: R (>= 3.6), grDevices, diff --git a/R/map-helpers.R b/R/map-helpers.R index 570a1ed..e0f1982 100644 --- a/R/map-helpers.R +++ b/R/map-helpers.R @@ -689,53 +689,37 @@ new_shpfile_props <- function(dir, layer, namefield, spObj) ## Fetch the namefield -## This is a generic function. The methods we have are for distinguishing -## how namefields are retrieved, since this varies depending on the kind -## of region passed. The interesting case in point is when getting this -## field for LGA regions, since the spatial data also contains data on -## States. To avoid confusion, when iterating through the data frame, when -## a column with States is encountered it is skipped as can be seen in the -## `.fetch_namefield.lgas` method. -.fetch_namefield <- function(x, ...) - UseMethod(".fetch_namefield") - - - - -.fetch_namefield.lgas <- function(x, dt) { - nmfld <- NA +.fetch_namefield <- function(x, dt) { + getfield <- function(index) names(dt)[[index]] + stopifnot(is.data.frame(dt)) + nmfield <- NA for (i in seq_len(ncol(dt))) { + icolumn <- dt[[i]] + iregions <- x %in% icolumn - if (all(unique(dt[[i]]) %in% states())) # skip column with States - next - - if (any(x %in% dt[[i]])) { # just any LGAs will do, since some of them - nmfld <- colnames(dt)[i] # also share names with their State + if (inherits(x, "states") && all(iregions)) { + nmfield <- getfield(i) break } - } - - nmfld -} - - - - -.fetch_namefield.states <- function(x, dt) { - # dt[[1]][dt[[1]] == "Nasarawa"] <- "Nassarawa" # Fix for 'ng_admin' - nmfld <- NA - - for (i in seq_len(ncol(dt))) { - if (all(x %in% dt[[i]])) { # all MUST be states - nmfld <- colnames(dt)[i] - break + if (inherits(x, "lgas")) { + # skip datafrane column with States + if (all(unique(icolumn) %in% states())) + next + + # just any LGAs will do, as some are synonymous with States + if (any(iregions)) { + nmfield <- getfield(i) + break + } } - } - nmfld + if (is.null(nmfield) || is.na(nmfield)) + cli::cli_abort("Problem retrieving the namefield") + + nmfield } diff --git a/naijR.Rproj b/naijR.Rproj new file mode 100644 index 0000000..af3a057 --- /dev/null +++ b/naijR.Rproj @@ -0,0 +1,19 @@ +Version: 1.0 + +RestoreWorkspace: Default +SaveWorkspace: Default +AlwaysSaveHistory: Default + +EnableCodeIndexing: Yes +UseSpacesForTab: Yes +NumSpacesForTab: 2 +Encoding: UTF-8 + +RnwWeave: Sweave +LaTeX: pdfLaTeX + +BuildType: Package +PackageUseDevtools: Yes +PackageInstallArgs: --no-multiarch --with-keep.source +PackageBuildArgs: --resave-data +PackageCheckArgs: --no-tests