Skip to content

Commit

Permalink
Merge pull request #216 from antaldaniel/daniel-regional
Browse files Browse the repository at this point in the history
Replacing regional data vignette
  • Loading branch information
antaldaniel authored Jun 19, 2021
2 parents 7a246b2 + d8637b2 commit 264f137
Show file tree
Hide file tree
Showing 15 changed files with 430 additions and 425 deletions.
6 changes: 4 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ Imports:
stringr,
tibble,
tidyr,
regions
regions,
rlang
Suggests:
covr,
Cairo,
Expand All @@ -84,7 +85,8 @@ Suggests:
rvest,
testthat,
tmap,
usethis
usethis,
here
LazyData: true
URL: https://ropengov.github.io/eurostat/
BugReports: https://github.com/ropengov/eurostat/issues
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ importFrom(readr,read_tsv)
importFrom(region,validate_nuts_regions)
importFrom(regions,recode_nuts)
importFrom(regions,validate_nuts_regions)
importFrom(rlang,.data)
importFrom(sf,st_buffer)
importFrom(sf,st_read)
importFrom(stringi,stri_extract_first_regex)
Expand Down
5 changes: 5 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# eurostat 3.7.5

* Deprecated add_nuts_level, harmonize_geo_code, recode_to_nuts_2016 and recode_to_nuts_2013; these functions were moved to the
new package regions. The problem of sub-national geo codes is explained in the new vignette "Mapping Regional Data, Mapping Metadata Problems", which replaces the "Regional data examples for the eurostat R package" vignette. This is a shared vignette, but the new regions package has more articles on how to work with sub-national data.

# eurostat 3.7.2

* Non-intersecting sf-geometries in get_eurostat_geospatial (PR by @retostauffer)
Expand Down
60 changes: 0 additions & 60 deletions R/add_nuts_level.R

This file was deleted.

67 changes: 67 additions & 0 deletions R/deprecated_regions_functions.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,73 @@
#' DEPRECATED FUNCTIONS FOR BACKWARD COMPATIBILITY
#' FUNCTIONS GIVE WARNING AND CALL APPROPRIATE regions FUNCTIONS

#' @title Add the statistical aggregation level to data frame
#' @description Eurostat regional statistics contain country, and various
#' regional level information. In many cases, for example, when mapping,
#' it is useful to filter out national level data from NUTS2 level regional data,
#' for example.
#'
#' This function will be deprecated. Use the more comprehensive
#' \code{regions::\link[regions]{validate_nuts_regions}} instead.
#'
#' @param dat A data frame or tibble returned by \code{\link{get_eurostat}}.
#' @param geo_labels A geographical label, defaults to \code{geo}.
#' @author Daniel Antal
#' @return a new numeric variable nuts_level with the numeric value of
#' NUTS level 0 (country), 1 (greater region),
#' 2 (region), 3 (small region).
#' @examples
#' {
#' dat = data.frame (
#' geo = c("FR", "IE04", "DEB1C"),
#' values = c(1000, 23, 12)
#' )
#'
#' add_nuts_level(dat)
#' }
#'
#' @importFrom dplyr mutate case_when
#' @importFrom rlang .data
#' @importFrom magrittr %>%
#'
#' @export

add_nuts_level <- function (dat, geo_labels = "geo") {

message ("This function will be deprecated. Use regions::validate_nuts_regions() instead.")

if ( any(c("character", "factor") %in% class(dat)) ) {
input <- 'label'
geo <- dat
dat <- data.frame( geo = as.character(geo),
stringsAsFactors = FALSE)
} else {
input <- "not_label"
}


if ( "data.frame" %in% class(dat) ) {
if ( ! geo_labels %in% names(dat) ) {
stop ( "Regional labelling variable '",
geo_labels,
"' is not found in the data frame.")
}

dat <- dat %>%
dplyr::mutate ( nuts_level = dplyr::case_when (
nchar(as.character(.data$geo)) == 2 ~ 0,
nchar(as.character(.data$geo)) == 3 ~ 1,
nchar(as.character(.data$geo)) == 4 ~ 2,
nchar(as.character(.data$geo)) == 5 ~ 3,
TRUE ~ NA_real_
))
}

if ( input == "label" ) {
as.numeric(dat$nuts_level)
} else dat

}

#' @title Harmonize NUTS region codes that changed with the \code{NUTS2016} definition
#' @description Eurostat mixes \code{NUTS2013} and \code{NUTS2016} geographic
Expand Down
11 changes: 9 additions & 2 deletions man/add_nuts_level.Rd

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

4 changes: 0 additions & 4 deletions man/harmonize_geo_code.Rd

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

29 changes: 0 additions & 29 deletions man/nuts_correspondence.Rd

This file was deleted.

28 changes: 0 additions & 28 deletions man/regional_changes_2016.Rd

This file was deleted.

3 changes: 0 additions & 3 deletions tests/testthat/test-add_nuts_level.R

This file was deleted.

16 changes: 14 additions & 2 deletions tests/testthat/test_regional.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,25 @@ test_regional_codes <- data.frame (
"Recoded in NUTS2016"
))



dat = data.frame (
geo = c("FR", "IE04", "DEB1C"),
values = c(1000, 23, 12)
)

test_that("deprecation warning works", {
expect_warning (recode_to_nuts_2013(test_regional_codes))
expect_warning (recode_to_nuts_2016(test_regional_codes))
expect_message(add_nuts_level(dat))
})

suppressMessages(
test_that("add_nuts_level works", {
expect_equal(add_nuts_level(dat)$nuts_level, c(0,2,3))
})
)


test_that("Recoding gives correct results",{

Expand All @@ -33,8 +47,6 @@ test_that("Recoding gives correct results",{

suppressWarnings(try_recode_2013 <- recode_to_nuts_2013(test_harmonized))

suppressWarnings(try_recode_2016 <- recode_to_nuts_2016(test_harmonized))

lookup_code16 <- test_harmonized %>%
filter ( geo == "FR243") %>%
select ( code16 ) %>% unlist() %>% as.character()
Expand Down
Binary file added vignettes/fig/indicator_with_map.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added vignettes/fig/recoded_indicator_with_map.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 264f137

Please sign in to comment.