Skip to content

Commit

Permalink
First working version of boundaries_select()
Browse files Browse the repository at this point in the history
TODO: improve texts and add proper testing
  • Loading branch information
ccamara committed Jul 22, 2024
1 parent 4939b22 commit 88f4695
Show file tree
Hide file tree
Showing 6 changed files with 243 additions and 1 deletion.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ Suggests:
tidyr
VignetteBuilder: knitr
Imports:
sf
sf,
utils
Depends:
R (>= 2.10)
Config/testthat/edition: 3
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Generated by roxygen2: do not edit by hand

export(boundaries_get)
export(boundaries_select)
78 changes: 78 additions & 0 deletions R/boundaries_select.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#' Interactively Retrieve Geographical Boundaries
#'
#' Constructs a valid combination of geometry type, year and detail level and
#' downloads the selected boundary.
#'
#' @return a `sf` object with the selected boundaries.
#'
#'
#' @export
#'
boundaries_select <- function() {
# Boundary types ----------------------------------------------------------
boundary_types <- levels(ons_boundaries$boundary_type)

# Use the menu function to prompt the user to choose an option
choice_boundary_type_n <- utils::menu(
boundary_types,
title = "Please choose the boundary type:"
)

choice_boundary_type <- boundary_types[choice_boundary_type_n]

cat("You chose:", choice_boundary_type, "\n")


# Boundary ----------------------------------------------------------------
boundaries <- levels(droplevels(
ons_boundaries[ons_boundaries$boundary_type == choice_boundary_type, "boundary"]
))

choice_boundary_n <- utils::menu(boundaries,
title = paste(choice_boundary_type, "has the following boundaries. Please choose the boundary name you'd like to download:")
)

choice_boundary <- boundaries[choice_boundary_n]

cat("You chose:", choice_boundary, "\n")

# Year --------------------------------------------------------------------

years <- levels(as.factor(
ons_boundaries[ons_boundaries$boundary == choice_boundary, "year"]
))

choice_years_n <- utils::menu(
years,
title = paste(choice_boundary, "were updated in the following years. Please choose the year name you'd like to download:")
)

choice_year <- as.numeric(years[choice_years_n])

print(choice_year)

# Detail level -----------------------------------------------------------
detail_levels <- levels(droplevels(
ons_boundaries[ons_boundaries$boundary == choice_boundary & ons_boundaries$year == choice_year, "detail_level"]
))

choice_detail_levels_n <- utils::menu(
detail_levels,
title = "Choose the detail level you'd like to download"
)

choice_detail_levels <- detail_levels[choice_detail_levels_n]

# Download ---------------------------------------------------------------
#return(paste(choice_boundary, choice_year, choice_detail_levels, sep = "_"))
return(boundaries_get(choice_boundary, choice_year, choice_detail_levels))

# Check if the user made a valid choice
# if (choice_boundary_type> 0) {
# cat("You chose:", choice_boundary_type, "\n")
# return(choice_boundary_type)
# } else {
# cat("No valid option was chosen.\n")
# return(NULL)
# }
}
15 changes: 15 additions & 0 deletions man/boundaries_select.Rd

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

144 changes: 144 additions & 0 deletions man/figures/hex-logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions tests/testthat/test-boundaries_select.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test_that("multiplication works", {
expect_equal(2 * 2, 4)
})

0 comments on commit 88f4695

Please sign in to comment.