Skip to content

Commit

Permalink
refactor prep_geonames code
Browse files Browse the repository at this point in the history
  • Loading branch information
truenomad committed Apr 18, 2024
1 parent 0e04bb2 commit 52665cb
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 16 deletions.
13 changes: 6 additions & 7 deletions R/prep_geonames.R
Original file line number Diff line number Diff line change
Expand Up @@ -188,15 +188,15 @@ calculate_match_stats <- function(data, lookup_data,
#' # list(x = "Skip", y = "Save"), "Your choice:")
#'
#' @keywords internal
display_custom_menu <- function(title, main_header, options,
display_custom_menu <- function(title, main_header, choices_input,
special_actions, prompt) {
cli::cli_h1(main_header)

# display the title using cli
cli::cli_h2(title)

# display the replacement options with cli styling
options_nums <- seq_along(options)
options_nums <- seq_along(choices_input)
for (i in options_nums) {
cli::cli_text(glue::glue("{i}: {options[i]}"))
}
Expand All @@ -221,7 +221,7 @@ display_custom_menu <- function(title, main_header, options,
cli::cli_alert_warning("Invalid choice, please try again.")
}

return(choice)
choice
}

#' Calculate String Distances Between Admin Names
Expand Down Expand Up @@ -265,7 +265,7 @@ calculate_string_distance <- function(
results <- list()

# iterate over each row (admin name to be cleaned)
for (i in 1:nrow(scores)) {
for (i in seq_len(nrow(scores))) {
# sort distances and get indices ot top ones
sorted_indices <- order(scores[i, ], decreasing = FALSE)[1:n_matches]

Expand Down Expand Up @@ -565,10 +565,10 @@ handle_user_interaction <- function(input_data, adm_level,
#' #)
#' # result <- construct_geo_names(data, "country", "state", "city")
construct_geo_names <- function(data, adm0, adm1, adm2) {
data <- data |>
data |>
dplyr::rowwise() |>
dplyr::mutate(long_geo = {
non_null_adms <- c()
non_null_adms <- NULL
if (!is.null(adm0) && !is.na(get(adm0))) {
non_null_adms <- c(non_null_adms, get(adm0))
}
Expand All @@ -581,7 +581,6 @@ construct_geo_names <- function(data, adm0, adm1, adm2) {
paste(non_null_adms, collapse = "_")
}) |>
dplyr::ungroup()
return(data)
}

#' Interactive Admin Name Cleaning and Matching
Expand Down
2 changes: 1 addition & 1 deletion R/save.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ save <- function(data, file_path, ...) {

if (file_ext %in% supported_formats_rio) {
rio::export(data, file_path, ...)
} else if (file_ext %in% c("shp")) { # shp shapefiles
} else if (file_ext %in% "shp") { # shp shapefiles
sf::write_sf(data, file_path, ...)
} else if (tolower(file_ext) %in% c("json", "geojson")) { # json shapefiles
sf::write_sf(data, file_path, driver = "GeoJSON", ...)
Expand Down
6 changes: 3 additions & 3 deletions man/display_custom_menu.Rd

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

7 changes: 2 additions & 5 deletions tests/testthat/test-prep_geonames.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
library(testthat)
library(stringdist)

# Example test for calculate_string_distance function
test_that("calculate_string_distance works correctly", {
testthat::test_that("calculate_string_distance works correctly", {

# get function output
result <- calculate_string_distance(
Expand All @@ -20,7 +17,7 @@ test_that("calculate_string_distance works correctly", {
)

# Test if the output matches expected data frame
expect_equal(result, expected)
testthat::expect_equal(result, expected)
})


Expand Down

0 comments on commit 52665cb

Please sign in to comment.