Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leverage on giscoR #264

Merged
merged 8 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Suggests:
VignetteBuilder:
knitr
Config/Needs/website: ggplot2, tmap, styler, sessioninfo,
ropengov/rogtemplate
ropengov/rogtemplate, ragg
Config/testthat/edition: 3
Config/testthat/parallel: false
Encoding: UTF-8
Expand Down
22 changes: 15 additions & 7 deletions R/get_eurostat_geospatial.R
Original file line number Diff line number Diff line change
Expand Up @@ -207,22 +207,33 @@ get_eurostat_geospatial <- function(output_class = "sf",
# Check if package "giscoR" is installed
# nocov start
if (!requireNamespace("giscoR")) {
message("'giscoR' package is required for geospatial functionalities")
return(invisible())
}

if (!requireNamespace("sf")) {
message("'sf' package is required for geospatial functionalities")
return(invisible())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even though this PR was already merged I noticed a logical problem that originated here:

!requireNamespace("sf") check is not run if use_local == TRUE
This causes problems later on in rows 265:267 (see another comment)

}
# nocov end

message("Extracting data using giscoR package")
message(paste0(
"Extracting data using giscoR package, please report issues",
" on https://github.com/rOpenGov/giscoR/issues"
))

# Manage cache: Priority is eurostat cache (if set)
# If not make use of giscoR default options
detect_eurostat_cache <- eur_helper_detect_cache_dir()
if (!is.null(cache_dir)) {
# Already set by the user, no need message
cache_dir <- eur_helper_cachedir(cache_dir)
} else if (identical(detect_eurostat_cache, file.path(tempdir(), "eurostat"))) {
} else if (identical(
detect_eurostat_cache,
file.path(tempdir(), "eurostat")
)) {
# eurostat not set, using default giscoR cache management
message("Cache management as per giscoR. see 'giscoR::gisco_get_nuts()")
message("Cache management as per giscoR. see 'giscoR::gisco_get_nuts()'")
} else {
cache_dir <- eur_helper_cachedir(cache_dir)
}
Expand All @@ -241,20 +252,17 @@ get_eurostat_geospatial <- function(output_class = "sf",

# Just to capture potential NULL outputs from giscoR - this happen
# on some errors
# nocov start
if (is.null(shp)) {
return(NULL)
}
# nocov end

# Post-data treatments

# Adding a `geo` column for easier joins with dplyr
shp$geo <- shp$NUTS_ID
# to df
if (output_class == "df") {
# Convert yl df
sf_col <- attr(shp, "sf_column")
# Remove geometry
shp <- sf::st_drop_geometry(shp)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even though this PR was already merged I noticed a logical problem that originated here:

!requireNamespace("sf") check is not run if use_local == TRUE
If sf is not installed then sf::st_drop_geometry(shp) can't be run. Something like shp$geometry <- NULL could do the same in this situation, I guess?


Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/_snaps/03_get_eurostat_geospatial.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
Code
a <- get_eurostat_geospatial(nuts_id = "LU", nuts_level = 0)
Message <simpleMessage>
Extracting data using giscoR package
Extracting data using giscoR package, please report issues on https://github.com/rOpenGov/giscoR/issues

---

Code
a <- get_eurostat_geospatial(nuts_id = "LU", nuts_level = 0, cache_dir = another_temp)
Message <simpleMessage>
Extracting data using giscoR package
Extracting data using giscoR package, please report issues on https://github.com/rOpenGov/giscoR/issues

---

Code
a <- get_eurostat_geospatial(nuts_id = "LU", nuts_level = 0)
Message <simpleMessage>
Extracting data using giscoR package
Cache management as per giscoR. see 'giscoR::gisco_get_nuts()
Extracting data using giscoR package, please report issues on https://github.com/rOpenGov/giscoR/issues
Cache management as per giscoR. see 'giscoR::gisco_get_nuts()'

20 changes: 20 additions & 0 deletions tests/testthat/test_03_get_eurostat_geospatial.R
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,23 @@ test_that("get_eurostat_geospatial cache_dir", {
suppressMessages(set_eurostat_cache_dir(init_cache))
expect_identical(init_cache, eur_helper_detect_cache_dir())
})


test_that("giscoR returns NULL", {
skip_if_not_installed(pkg = "giscoR")
skip_on_cran()
skip_if_offline()
skip_if(!giscoR::gisco_check_access(), "No access to GISCO")
skip_if(packageVersion("giscoR") < "0.3.5", "Use latest giscoR release")

options(giscoR_test_offline = TRUE)
expect_message(
n <- get_eurostat_geospatial(
country = "AT", nuts_level = "0",
update_cache = TRUE
),
"not reachable"
)
expect_null(n)
options(giscoR_test_offline = FALSE)
})