From 75e424c122f7eeeaa7d6bf2fea6d7b3a0d9cf1b6 Mon Sep 17 00:00:00 2001 From: Uchida Mizuki Date: Sun, 26 May 2024 21:03:46 +0900 Subject: [PATCH] Update `grid_components()` and add tests --- R/move.R | 15 ++++++++------- man/grid_components.Rd | 5 +++-- tests/testthat/test-move.R | 16 ++++++++++++++++ 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/R/move.R b/R/move.R index e50b525..307a589 100644 --- a/R/move.R +++ b/R/move.R @@ -57,7 +57,7 @@ grid_neighborhood <- function(grid, neighbor <- tibble::tibble(grid = grid) |> vec_unique() |> tidyr::expand_grid(n_XY) - neighbor$grid_neighbor <- neighbor$grid |> + neighbor$grid_neighborhood <- neighbor$grid |> grid_move(n_X = neighbor$n_X, n_Y = neighbor$n_Y) neighbor <- neighbor |> @@ -68,7 +68,7 @@ grid_neighborhood <- function(grid, neighbor$neighbor <- neighbor$neighbor |> purrr::map(function(neighbor) { neighbor |> - purrr::chuck("grid_neighbor") + purrr::chuck("grid_neighborhood") }) } @@ -82,7 +82,8 @@ grid_neighborhood <- function(grid, #' #' @param grid A `grid` vector. #' @param n A numeric vector of degrees. By default, `0:1`. -#' @param moore Moore neighborhood (`TRUE`) or Von Neumann neighborhood +#' @param type A character vector of neighborhood types, `"von_neumann"` or +#' `"moore"`. By default, `"von_neumann"`. #' (`FALSE`, default). #' #' @return A integer vector of group IDs. @@ -90,11 +91,11 @@ grid_neighborhood <- function(grid, #' @export grid_components <- function(grid, n = 0:1, - moore = FALSE) { + type = NULL) { edges <- tibble::tibble(grid_from = grid, - grid_to = grid_neighbor(grid, - n = n, - moore = moore)) |> + grid_to = grid_neighborhood(grid, + n = n, + type = type)) |> tidyr::unnest("grid_to") |> dplyr::filter(.data$grid_to %in% grid) diff --git a/man/grid_components.Rd b/man/grid_components.Rd index b3fe565..9c08374 100644 --- a/man/grid_components.Rd +++ b/man/grid_components.Rd @@ -4,14 +4,15 @@ \alias{grid_components} \title{Connected components of grid square codes} \usage{ -grid_components(grid, n = 0:1, moore = FALSE) +grid_components(grid, n = 0:1, type = NULL) } \arguments{ \item{grid}{A \code{grid} vector.} \item{n}{A numeric vector of degrees. By default, \code{0:1}.} -\item{moore}{Moore neighborhood (\code{TRUE}) or Von Neumann neighborhood +\item{type}{A character vector of neighborhood types, \code{"von_neumann"} or +\code{"moore"}. By default, \code{"von_neumann"}. (\code{FALSE}, default).} } \value{ diff --git a/tests/testthat/test-move.R b/tests/testthat/test-move.R index fae33d2..996c1a1 100644 --- a/tests/testthat/test-move.R +++ b/tests/testthat/test-move.R @@ -20,3 +20,19 @@ test_that("neighborhood", { 523877, 523970, 523971), grid_size = "10km")))) }) + +test_that("components", { + grid_1km <- parse_grid(c(53394620, + 53394631, 53394632, + 53394507, 53394508, 53394509), + "1km") + + expect_equal(grid_components(grid_1km, n = 0:1, type = "von_neumann"), + c(3, 2, 2, 1, 1, 1)) + expect_equal(grid_components(grid_1km, n = 0:1, type = "moore"), + c(1, 1, 1, 2, 2, 2)) + expect_equal(grid_components(grid_1km, n = 0:2, type = "von_neumann"), + c(1, 1, 1, 2, 2, 2)) + expect_equal(grid_components(grid_1km, n = 0:2, type = "moore"), + c(1, 1, 1, 1, 1, 1)) +})