Skip to content

Commit

Permalink
clean up pumpCase()
Browse files Browse the repository at this point in the history
  • Loading branch information
lindbrook committed Aug 15, 2018
1 parent 983ff51 commit 16db1c9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 27 deletions.
38 changes: 13 additions & 25 deletions R/pumpCase.R
@@ -1,6 +1,6 @@
#' Extract numeric case IDs by pump neighborhood.
#'
#' @param obj An object created by \code{neighborhoodEuclidean()}, \code{neighborhoodVoronoi()} or \code{neighborhoodWalking()}.
#' @param x An object created by \code{neighborhoodEuclidean()}, \code{neighborhoodVoronoi()} or \code{neighborhoodWalking()}.
#' @seealso \code{\link{neighborhoodVoronoi}}, \code{\link{neighborhoodVoronoi}}, \code{\link{neighborhoodEuclidean}},
#' @return An R list of numeric ID of cases by pump neighborhoods.
#' @export
Expand All @@ -12,52 +12,40 @@
#' pumpCase(neighborhoodWalking())
#' }

pumpCase <- function(obj) UseMethod("pumpCase", obj)
pumpCase <- function(x) UseMethod("pumpCase", x)

pumpCase.default <- function(obj) NULL
pumpCase.default <- function(x) NULL

#' @export
pumpCase.euclidean <- function(obj) {
if (class(obj) != "euclidean") {
stop('obj\'s class needs to be "euclidean".')
}

pumps <- sort(unique(obj$nearest.pump))
pumpCase.euclidean <- function(x) {
pumps <- sort(unique(x$nearest.pump))
out <- lapply(pumps, function(p) {
obj$anchors[obj$nearest.pump == p]
x$anchors[x$nearest.pump == p]
})

stats::setNames(out, paste0("p", pumps))
}

#' @export
pumpCase.voronoi <- function(obj) {
if (class(obj) != "voronoi") {
stop('obj\'s class needs to be "voronoi".')
}

output <- obj$statistic.data
pumpCase.voronoi <- function(x) {
output <- x$statistic.data
out <- lapply(output, function(x) {
cholera::fatalities.address$anchor.case[x == 1]
})

if (is.null(obj$pump.select)) {
if (obj$vestry == TRUE) {
if (is.null(x$pump.select)) {
if (x$vestry == TRUE) {
stats::setNames(out, paste0("p", seq_len(nrow(cholera::pumps.vestry))))
} else {
stats::setNames(out, paste0("p", seq_len(nrow(cholera::pumps))))
}
} else {
stats::setNames(out, paste0("p", obj$pump.id))
stats::setNames(out, paste0("p", x$pump.id))
}
}

#' @export
pumpCase.walking <- function(obj) {
if (class(obj) != "walking") {
stop('obj\'s class needs to be "walking".')
}

output <- obj$cases
pumpCase.walking <- function(x) {
output <- x$cases
stats::setNames(output, paste0("p", names(output)))
}
4 changes: 2 additions & 2 deletions man/pumpCase.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 16db1c9

Please sign in to comment.