Skip to content

Commit

Permalink
Merge pull request #3 from adamhsparks/example_data
Browse files Browse the repository at this point in the history
  • Loading branch information
adamhsparks authored Nov 30, 2024
2 parents c8f4477 + 0ec3e24 commit 3d1a5fa
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 60 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Imports:
tidync,
withr
Suggests:
httptest2,
knitr,
pander,
rmarkdown,
Expand Down
20 changes: 8 additions & 12 deletions R/clear_cache.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

#' Remove Files in Users' Cache Directory
#'
#' Removes all files in the \pkg{read.abares} cache if they exist.
#' Removes all files in the \pkg{read.abares} cache if any exist.
#'
#' @examples
#' # not run because cached files shouldn't exist on CRAN or testing envs
Expand All @@ -13,18 +13,14 @@
#' @export

clear_cache <- function() {
f <- list.files(.find_user_cache(),
recursive = TRUE,
full.names = TRUE)
f <- .find_user_cache()

if (length(f > 0)) {
unlink(f, recursive = TRUE, force = TRUE)
} else {
cli::cli_inform(
c(
"There do not appear to be any files cached for {.pkg {{read.abares}}}
if (unlink(f, recursive = TRUE, force = FALSE) == 0)
return(invisible(TRUE))
cli::cli_inform(
c(
"There do not appear to be any files cached for {.pkg {{read.abares}}}
that need to be cleared at this time."
)
)
}
)
}
67 changes: 41 additions & 26 deletions R/get_aagis_regions.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@

#' Get AAGIS Region Mapping Files
#'
#' @param cache `Boolean` Cache the \acronym{AAGIS} regions shape files after
#' download using `tools::R_user_dir()` to identify the proper directory for
#' storing user data in a cache for this package. Defaults to `TRUE`, caching
#' the files locally as a native \R object. If `FALSE`, this function uses
#' `tempdir()` and the files are deleted upon closing of the \R session.
#' Download, cache and import the Australian Agricultural and Grazing
#' Industries Survey (\acronym{AAGIS} regions geospatial shapefile. Upon
#' import, the geometries are automatically corrected to fix invalid
#' geometries that are present in the original shapefile.
#'
#' @param cache `Boolean` Cache the \acronym{AAGIS} regions' geospatial file
#' after downloading using `tools::R_user_dir("read.abares", "cache")` to
#' identify the proper directory for storing user data in a cache for this
#' package. Defaults to `TRUE`, caching the files locally as a Geopackage. If
#' `FALSE`, this function uses `tempdir()` and the files are deleted upon
#' closing of the \R session.
#'
#' @examplesIf interactive()
#' aagis <- get_aagis_regions()
Expand All @@ -16,15 +23,16 @@
#' @family AGFD
#'
#' @references <https://www.agriculture.gov.au/abares/research-topics/surveys/farm-definitions-methods#regions>
#' @source <https://www.agriculture.gov.au/sites/default/files/documents/aagis_asgs16v1_g5a.shp_.zip>
#' @autoglobal
#' @export

get_aagis_regions <- function(cache = TRUE) {
aagis_sf <- .check_existing_aagis(cache)
if (is.null(aagis_sf)) {
aagis_sf <- .download_aagis_shp(cache)
aagis <- .check_existing_aagis(cache)
if (is.null(aagis)) {
aagis <- .download_aagis_shp(cache)
} else {
return(aagis_sf)
return(aagis)
}
}

Expand All @@ -35,40 +43,43 @@ get_aagis_regions <- function(cache = TRUE) {
#' cache, but is in `tempdir()`, it is saved to the cache before being returned
#' in the current session.
#'
#'
#' @return An \cranpkg{sf} object of AAGIS regions
#' @noRd
#' @autoglobal
#' @keywords Internal

.check_existing_aagis <- function(cache) {
aagis_rds <- file.path(.find_user_cache(), "aagis_regions_dir/aagis.rds")
aagis_gpkg <- file.path(.find_user_cache(), "aagis_regions_dir/aagis.gpkg")
tmp_shp <- file.path(tempdir(), "aagis_asgs16v1_g5a.shp")

if (file.exists(aagis_rds)) {
return(readRDS(aagis_rds))
if (file.exists(aagis_gpkg)) {
return(readRDS(aagis_gpkg))
} else if (file.exists(tmp_shp)) {
aagis_sf <- sf::st_read(tmp_shp)
aagis_sf <- sf::st_read(tmp_shp, quiet = TRUE)
# From checking the unzipped file, some geometries are invalid, this corrects
aagis_sf <- sf::st_make_valid(aagis_sf)
if (cache) {
dir.create(dirname(aagis_rds), recursive = TRUE)
saveRDS(aagis_sf, file = aagis_rds)
dir.create(dirname(aagis_gpkg), recursive = TRUE)
sf::st_write(aagis_sf, file = aagis_gpkg, quiet = TRUE)
}
return(aagis_sf)
} else {
return(NULL)
return(invisible(NULL))
}
}

#' Download the AAGIS Regions Shapefile
#'
#' Handles downloading and caching (if requested) of AAGIS regions geospatial
#' data.
#' Handles downloading, caching (if requested) and importing of AAGIS regions
#' geospatial data. The geometries are corrected for validity before returning
#' to the user.
#'
#' @param cache `Boolean` Cache the \acronym{AAGIS} regions shape files after
#' download using `tools::R_user_dir()` to identify the proper directory for
#' storing user data in a cache for this package. Defaults to `TRUE`, caching
#' the files locally as a native \R object. If `FALSE`, this function uses
#' `tempdir()` and the files are deleted upon closing of the \R session.
#' download using `tools::R_user_dir("read.abares")` to identify the proper
#' directory for storing user data in a cache for this package. Defaults to
#' `TRUE`, caching the files locally as a native \R object. If `FALSE`, this
#' function uses `tempdir()` and the files are deleted upon closing of the \R
#' session.
#'
#' @return An \cranpkg{sf} object of AAGIS regions
#' @noRd
Expand All @@ -81,7 +92,7 @@ get_aagis_regions <- function(cache = TRUE) {
tmp_zip <- file.path(file.path(tempdir(), "aagis.zip"))
aagis_zip <- data.table::fifelse(cache, cached_zip, tmp_zip)
aagis_regions_dir <- dirname(aagis_zip)
aagis_rds <- file.path(aagis_regions_dir, "aagis.rds")
aagis_gpkg <- file.path(aagis_regions_dir, "aagis.gpkg")

# the user-cache may not exist if caching is enabled for the 1st time
if (cache && !dir.exists(aagis_regions_dir)) {
Expand All @@ -95,10 +106,14 @@ get_aagis_regions <- function(cache = TRUE) {
utils::unzip(aagis_zip, exdir = aagis_regions_dir))

aagis_sf <- sf::read_sf(dsn = file.path(aagis_regions_dir,
"aagis_asgs16v1_g5a.shp"))
"aagis_asgs16v1_g5a.shp"),
quiet = TRUE)

# From checking the unzipped file, some geometries are invalid, this corrects
aagis_sf <- sf::st_make_valid(aagis_sf)

if (cache) {
saveRDS(aagis_sf, file = aagis_rds)
sf::st_write(obj = aagis_sf, dsn = aagis_gpkg, quiet = TRUE)
unlink(c(
aagis_zip,
file.path(aagis_regions_dir, "aagis_asgs16v1_g5a.*")
Expand Down
14 changes: 13 additions & 1 deletion codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@
}
],
"softwareSuggestions": [
{
"@type": "SoftwareApplication",
"identifier": "httptest2",
"name": "httptest2",
"provider": {
"@id": "https://cran.r-project.org",
"@type": "Organization",
"name": "Comprehensive R Archive Network (CRAN)",
"url": "https://cran.r-project.org"
},
"sameAs": "https://CRAN.R-project.org/package=httptest2"
},
{
"@type": "SoftwareApplication",
"identifier": "knitr",
Expand Down Expand Up @@ -273,7 +285,7 @@
},
"SystemRequirements": null
},
"fileSize": "343.019KB",
"fileSize": "344.301KB",
"citation": [
{
"@type": "SoftwareSourceCode",
Expand Down
2 changes: 1 addition & 1 deletion man/clear_cache.Rd

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

19 changes: 13 additions & 6 deletions man/get_aagis_regions.Rd

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

1 change: 1 addition & 0 deletions tests/testthat/setup.R
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
library(httptest2)
33 changes: 19 additions & 14 deletions tests/testthat/test-get_aagis_regions.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,25 @@
withr::local_envvar(R_USER_CACHE_DIR = file.path(tempdir(), "abares.cache.1"))

# without caching ----

test_that("get_aagis_regions doesn't cache", {
skip_if_offline()
skip_on_ci()
x <- get_aagis_regions(cache = FALSE)
expect_s3_class(x, "sf")
expect_false(file.exists(
file.path(.find_user_cache(), "aagis_regions_dir/aagis.rds")
))
with_mock_dir("test-get_aagis_regions", {
test_that("get_aagis_regions doesn't cache", {
skip_if_offline()
skip_on_ci()
x <- get_aagis_regions(cache = FALSE)
expect_s3_class(x, "sf")
expect_false(file.exists(
file.path(.find_user_cache(), "aagis_regions_dir/aagis.rds")
))
})
})

test_that("get_aagis_regions skips downloading if still in tempdir()", {
skip_if_offline()
skip_on_ci()
x <- .check_existing_aagis(cache = FALSE)
expect_s3_class(x, "sf")
with_mock_dir("test-get_aagis_regions", {
test_that("get_aagis_regions skips downloading if still in tempdir()", {
skip_if_offline()
skip_on_ci()
x <- .check_existing_aagis(cache = FALSE)
expect_s3_class(x, "sf")
})
})

# with caching ----
Expand Down Expand Up @@ -49,6 +52,7 @@ test_that("get_aagis_regions skips downloading if cache is available", {
# sets up a custom cache environment in `tempdir()` just for testing
withr::local_envvar(R_USER_CACHE_DIR = file.path(tempdir(), "abares.cache.2"))


test_that("get_aagis_regions does cache", {
skip_if_offline()
skip_on_ci()
Expand All @@ -71,5 +75,6 @@ test_that("get_aagis_regions skips downloading if still in tempdir()", {
x <- .check_existing_aagis(cache = TRUE)
expect_s3_class(x, "sf")
})
})

withr::deferred_run()

0 comments on commit 3d1a5fa

Please sign in to comment.