From 361335bbebc750acb0854ec92f4891079cd46a73 Mon Sep 17 00:00:00 2001 From: Victor Ordu Date: Wed, 14 Jun 2023 01:56:11 +0100 Subject: [PATCH 1/2] Make R Studio project settings part of history The build configurations for this package have been customized, hence the decision to make this file available to collaborators. --- .gitignore | 1 - naijR.Rproj | 19 +++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 naijR.Rproj 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/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 From 7c98625161a8b14a0af84f1c0ed5446a89e44943 Mon Sep 17 00:00:00 2001 From: Victor Ordu Date: Wed, 14 Jun 2023 13:41:27 +0100 Subject: [PATCH 2/2] Stop using package-internal S3 generic/methods R CMD check for R-devel now raises a NOTE on improper registration of S3 methods. The ones under reference are meant to exist only internally and there is no value in exporting them. So, I have decided instead to use conditional flow of control in a single function. I also added checks for function pre- and post-conditions. --- DESCRIPTION | 2 +- R/map-helpers.R | 60 ++++++++++++++++++------------------------------- 2 files changed, 23 insertions(+), 39 deletions(-) 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 }