From 7859e02f0a91aecb010a0f26f52853da7addd06d Mon Sep 17 00:00:00 2001 From: AAoritz Date: Wed, 7 Feb 2024 22:17:14 +0100 Subject: [PATCH] bundle cli messages in list in convert_nuts_level --- R/convert_nuts_level.R | 76 +++++++++++----------- docs/pkgdown.yml | 2 +- docs/reference/classify_nuts.html | 26 ++++---- docs/reference/convert_nuts_level.html | 33 +++++----- docs/reference/convert_nuts_version.html | 59 ++++++++--------- man/classify_nuts.Rd | 2 +- man/convert_nuts_level.Rd | 2 +- man/convert_nuts_version.Rd | 2 +- tests/testthat/test-classify_nuts.R | 2 - tests/testthat/test-convert_nuts_level.R | 1 - tests/testthat/test-convert_nuts_version.R | 2 - 11 files changed, 104 insertions(+), 103 deletions(-) diff --git a/R/convert_nuts_level.R b/R/convert_nuts_level.R index 63bcd04..4b9a850 100644 --- a/R/convert_nuts_level.R +++ b/R/convert_nuts_level.R @@ -119,21 +119,20 @@ convert_nuts_level <- # CONVERSION POSSIBLE #---------------------- - # Welcome information - cli_text("{.blue Converting level of NUTS codes}") - cli_text("{.blue ------------------------------}") # CONVERSION BETWEEN DIFFERENT NUTS LEVELS - cli_alert_info("{.blue => Aggregate from NUTS regional level {.red {data$from_level[1]}} to {.red {to_level}}}.") + aggregate_from_level <- data$from_level[1] + message_conversion_levels <- c("i" = "{.blue Aggregate from NUTS regional level {.red {aggregate_from_level}} to {.red {to_level}}}.") # Check which NUTS codes can be converted nr_nuts_codes_recognized <- length(data$from_code[check_nuts_codes]) nr_nuts_codes <- length(data$from_code) + dropped_codes <- unique(data$from_code[!check_nuts_codes]) if (nr_nuts_codes_recognized == nr_nuts_codes) { - cli_alert_info("{.blue => All NUTS codes can be converted.}") + message_can_be_converted <- c("v" = "{.blue All NUTS codes can be converted.}") } else if (nr_nuts_codes_recognized < nr_nuts_codes && nr_nuts_codes_recognized > 0) { - cli_alert_warning("{.blue => These NUTS codes cannot be converted and {.red are dropped} from the dataset: {.red {unique(data$from_code[!check_nuts_codes])}}.}") + message_can_be_converted <- c("x" = "{.blue These NUTS codes cannot be converted and {.red are dropped}: {.red {dropped_codes}}.}") data <- data[check_nuts_codes, ] } @@ -149,38 +148,34 @@ convert_nuts_level <- nrow() # Use data_versions which is sorted for most frequent version within group - if (multi_versions_A > multi_versions_B) { - cli_alert_info("{.blue => Within {.red groups} defined by {.red {group_vars}}}") - cli_alert_warning("{.blue ==> {.red Multiple} NUTS code versions.}") + if (multi_versions_A > multi_versions_B && multiple_versions == "break") { - if (multiple_versions == "break") { - cli_abort( - c( - "Mixed NUTS versions within groups!", - "Please make sure the data contains only one version per group. Alternatively, keep only the codes belonging to the 'most_frequent' version using the argument {.arg multiple_versions}." - ) + cli_abort( + c( + "Mixed NUTS versions within groups!" + , + "Please make sure the data contains only one version per group. Alternatively, keep only the codes belonging to the 'most_frequent' version using the argument 'multiple_versions'." ) + ) - } else if (multiple_versions == "most_frequent") { - data_versions <- data_versions %>% - group_by_at(vars(any_of(c(group_vars)))) %>% - slice(1) %>% - ungroup() + } else if (multi_versions_A > multi_versions_B && multiple_versions == "most_frequent") { + data_versions <- data_versions %>% + group_by_at(vars(any_of(c(group_vars)))) %>% + slice(1) %>% + ungroup() - data_multi_versions <- - anti_join(data, data_versions, by = c("from_version", group_vars)) - data <- - inner_join(data, data_versions, by = c("from_version", group_vars)) + data_multi_versions <- + anti_join(data, data_versions, by = c("from_version", group_vars)) + data <- + inner_join(data, data_versions, by = c("from_version", group_vars)) - cli_alert_info("{.blue Choosing most frequent version within group and {.red dropping} {nrow(data_multi_versions)} row(s).}") - } - paste_grouping = F + n_rows_dropped <- nrow(data_multi_versions) + message_multiple_versions <- c("!" = "{.blue Choosing most frequent version within group and {.red dropping} {n_rows_dropped} row{?s}.}") } else { - paste_grouping = T + message_multiple_versions <- c("v" = "{.blue Version is {.red unique}.}") } # - Done - # Prepare join with regional indicator stocks such that missing NUTS codes within groups are kept # - Create group structure group_structure <- data %>% @@ -221,15 +216,12 @@ convert_nuts_level <- select(all_of(names(variables))) %>% filter(if_any(names(variables), ~ is.na(.))) - if (paste_grouping) { - cli_alert_info("{.blue => Within {.red groups} defined by {.red {group_vars}}.}") - } - + # - Alert missing if (nrow(missing) > 0) { - cli_alert_warning("{.blue => {.red Missing} NUTS codes in data.}") - cli_alert_warning("{.blue ==> No values are calculated for regions associated with missing NUTS codes. Ensure that the input data is complete.}") + message_missing_codes <- c("x" = "{.blue {.red Missing} NUTS codes in data. No values are calculated for regions associated with missing NUTS codes. Ensure that the input data is complete.}") + } else if (nrow(missing) == 0) { - cli_alert_info("{.blue => No missing NUTS codes.}") + message_missing_codes <- c("v" = "{.blue No {.red missing} NUTS codes.}") } rm(missing) @@ -272,7 +264,17 @@ convert_nuts_level <- full_join(rel_data, by = c("to_code", group_vars)) # - Done - cat("\n") + # Console Message + #----------------- + cli_h1("Converting version of NUTS codes") + cli_bullets( + c("{.blue Within {.red groups} defined by {.red {group_vars}}:}", + message_conversion_levels, + message_can_be_converted, + message_multiple_versions, + message_missing_codes + ) + ) return(data) } diff --git a/docs/pkgdown.yml b/docs/pkgdown.yml index f850d57..7a106be 100644 --- a/docs/pkgdown.yml +++ b/docs/pkgdown.yml @@ -3,7 +3,7 @@ pkgdown: 2.0.7 pkgdown_sha: ~ articles: nuts: nuts.html -last_built: 2024-02-07T09:28Z +last_built: 2024-02-07T21:11Z urls: reference: https://aaoritz.github.io/nuts/reference article: https://aaoritz.github.io/nuts/articles diff --git a/docs/reference/classify_nuts.html b/docs/reference/classify_nuts.html index e9eb1fe..23fffa2 100644 --- a/docs/reference/classify_nuts.html +++ b/docs/reference/classify_nuts.html @@ -85,7 +85,7 @@

Arguments @@ -136,12 +136,12 @@

Examples select(-indic_ag, -time) %>% # Data varies at the NUTS code level classify_nuts(nuts_code = 'geo') -#> Classifying version of NUTS codes -#> ----------------------------------- -#> => These NUTS codes cannot be identified or classified: DEZZ. -#> => Within groups defined by country. -#> ! ==> Missing NUTS codes detected. See the tibble 'missing_data' in the output. -#> +#> +#> ── Classifying version of NUTS codes ─────────────────────────────────────────── +#> Within groups defined by country: +#> ! These NUTS codes cannot be identified or classified: DEZZ. +#> Unique NUTS version classified. +#> Missing NUTS codes detected. See the tibble 'missing_data' in the output. #> $data #> # A tibble: 36 × 5 #> from_code from_version from_level country values @@ -193,12 +193,12 @@

Examples # Data varies at the year x country x NUTS code level. The country grouping # is always used by default. classify_nuts(nuts_code = 'geo', group_vars = 'time') -#> Classifying version of NUTS codes -#> ----------------------------------- -#> => These NUTS codes cannot be identified or classified: ME000 and NOZZZ. -#> => Within groups defined by country and time. -#> ! ==> Missing NUTS codes detected. See the tibble 'missing_data' in the output. -#> +#> +#> ── Classifying version of NUTS codes ─────────────────────────────────────────── +#> Within groups defined by country and time: +#> ! These NUTS codes cannot be identified or classified: ME000 and NOZZZ. +#> Unique NUTS version classified. +#> Missing NUTS codes detected. See the tibble 'missing_data' in the output. #> $data #> # A tibble: 1,902 × 6 #> from_code from_version from_level country time values diff --git a/docs/reference/convert_nuts_level.html b/docs/reference/convert_nuts_level.html index 15c64d2..5bd5192 100644 --- a/docs/reference/convert_nuts_level.html +++ b/docs/reference/convert_nuts_level.html @@ -94,7 +94,7 @@

Arguments @@ -132,20 +132,23 @@

Examples convert_nuts_level(to_level = 2, variables = c('values'= 'absolute'), weight = 'pop18') -#> Classifying version of NUTS codes -#> ----------------------------------- -#> => These NUTS codes cannot be identified or classified: ME000, ME000, ME000, ME000, ME000, ME000, ME000, ME000, NOZZZ, NOZZZ, NOZZZ, NOZZZ, SIZZZ, and NOZZZ. -#> => Within groups defined by country, indic_ag, and time. -#> ! ==> Missing NUTS codes detected. See the tibble 'missing_data' in the output. -#> -#> Converting level of NUTS codes -#> ------------------------------ -#> => Aggregate from NUTS regional level 3 to 2. -#> ! => These NUTS codes cannot be converted and are dropped from the dataset: ME000, NOZZZ, and SIZZZ. -#> => Within groups defined by country, indic_ag, and time. -#> ! => Missing NUTS codes in data. -#> ! ==> No values are calculated for regions associated with missing NUTS codes. Ensure that the input data is complete. -#> +#> +#> ── Classifying version of NUTS codes ─────────────────────────────────────────── +#> Within groups defined by country, indic_ag, and time: +#> ! These NUTS codes cannot be identified or classified: ME000, ME000, ME000, +#> ME000, ME000, ME000, ME000, ME000, NOZZZ, NOZZZ, NOZZZ, NOZZZ, SIZZZ, and +#> NOZZZ. +#> Unique NUTS version classified. +#> Missing NUTS codes detected. See the tibble 'missing_data' in the output. +#> +#> ── Converting version of NUTS codes ──────────────────────────────────────────── +#> Within groups defined by country, indic_ag, and time: +#> Aggregate from NUTS regional level 3 to 2. +#> These NUTS codes cannot be converted and are dropped: ME000, NOZZZ, and +#> SIZZZ. +#> Version is unique. +#> Missing NUTS codes in data. No values are calculated for regions associated +#> with missing NUTS codes. Ensure that the input data is complete. #> # A tibble: 4,814 × 5 #> to_code country indic_ag time values #> <chr> <chr> <chr> <dbl> <dbl> diff --git a/docs/reference/convert_nuts_version.html b/docs/reference/convert_nuts_version.html index 0d6bf8a..dbd8919 100644 --- a/docs/reference/convert_nuts_version.html +++ b/docs/reference/convert_nuts_version.html @@ -94,7 +94,7 @@

Arguments @@ -135,20 +135,20 @@

Examples convert_nuts_version(to_version = '2021', weight = 'pop18', variables = c('values' = 'absolute')) -#> Classifying version of NUTS codes -#> ----------------------------------- -#> => These NUTS codes cannot be identified or classified: DEZZ. -#> => Within groups defined by country. -#> ! ==> Missing NUTS codes detected. See the tibble 'missing_data' in the output. -#> -#> Converting versions of NUTS codes -#> --------------------------------- -#> => Converting NUTS codes in version(s) 2006 to version 2021. -#> ! => These NUTS codes cannot be converted and are dropped from the dataaset: DEZZ. -#> => Within groups defined by country. -#> ! => Missing NUTS codes in data. -#> ! ==> No values are calculated for regions associated with missing NUTS codes. Ensure that the input data is complete. -#> +#> +#> ── Classifying version of NUTS codes ─────────────────────────────────────────── +#> Within groups defined by country: +#> ! These NUTS codes cannot be identified or classified: DEZZ. +#> Unique NUTS version classified. +#> Missing NUTS codes detected. See the tibble 'missing_data' in the output. +#> +#> ── Converting version of NUTS codes ──────────────────────────────────────────── +#> Within groups defined by country: +#> Converting NUTS codes in 1 version 2006 to version 2021. +#> These NUTS codes cannot be converted and are dropped: DEZZ. +#> Version is unique. +#> Missing NUTS codes in data. No values are calculated for regions associated +#> with missing NUTS codes. Ensure that the input data is complete. #> # A tibble: 38 × 4 #> to_code to_version country values #> <chr> <chr> <chr> <dbl> @@ -175,20 +175,21 @@

Examples convert_nuts_version(to_version = '2021', weight = 'pop18', variables = c('values' = 'absolute')) -#> Classifying version of NUTS codes -#> ----------------------------------- -#> => These NUTS codes cannot be identified or classified: ME000 and NOZZZ. -#> => Within groups defined by country and time. -#> ! ==> Missing NUTS codes detected. See the tibble 'missing_data' in the output. -#> -#> Converting versions of NUTS codes -#> --------------------------------- -#> => Converting NUTS codes in version(s) 2021, 2016, 2010, 2006, and 2013 to version 2021. -#> ! => These NUTS codes cannot be converted and are dropped from the dataaset: ME000 and NOZZZ. -#> => Within groups defined by country and time. -#> ! => Missing NUTS codes in data. -#> ! ==> No values are calculated for regions associated with missing NUTS codes. Ensure that the input data is complete. -#> +#> +#> ── Classifying version of NUTS codes ─────────────────────────────────────────── +#> Within groups defined by country and time: +#> ! These NUTS codes cannot be identified or classified: ME000 and NOZZZ. +#> Unique NUTS version classified. +#> Missing NUTS codes detected. See the tibble 'missing_data' in the output. +#> +#> ── Converting version of NUTS codes ──────────────────────────────────────────── +#> Within groups defined by country and time: +#> Converting NUTS codes in 5 versions 2021, 2016, 2010, 2006, and 2013 to +#> version 2021. +#> These NUTS codes cannot be converted and are dropped: ME000 and NOZZZ. +#> Version is unique. +#> Missing NUTS codes in data. No values are calculated for regions associated +#> with missing NUTS codes. Ensure that the input data is complete. #> # A tibble: 2,296 × 5 #> to_code to_version country time values #> <chr> <chr> <chr> <dbl> <dbl> diff --git a/man/classify_nuts.Rd b/man/classify_nuts.Rd index 31b45eb..2ee0343 100644 --- a/man/classify_nuts.Rd +++ b/man/classify_nuts.Rd @@ -24,7 +24,7 @@ duplicate NUTS codes within groups allowed.} \item{ties}{Picks \code{'most_recent'} or \code{'oldest'} version when overlap is identical across multiple NUTS versions. \code{'most_recent'} by default.} -\item{quiet}{Suppress messages and warnings. \code{TRUE} by default.} +\item{quiet}{Suppress messages and warnings. \code{FALSE} by default.} } \value{ A list of three tibbles. The first tibble contains the original data with the classified NUTS version, level, and country. diff --git a/man/convert_nuts_level.Rd b/man/convert_nuts_level.Rd index 97fbd35..9c50795 100644 --- a/man/convert_nuts_level.Rd +++ b/man/convert_nuts_level.Rd @@ -29,7 +29,7 @@ population in 2011 \code{'pop11'} or 2018 \code{'pop18'}, or artificial surfaces \item{multiple_versions}{By default equal to \code{'break'}, throwing an error when providing multiple NUTS versions within groups. If set to \code{'most_frequent'} data is converted using the best-matching NUTS version.} -\item{quiet}{Suppress messages and warnings. \code{TRUE} by default.} +\item{quiet}{Suppress messages and warnings. \code{FALSE} by default.} } \value{ A tibble containing NUTS codes, aggregated variable values, and possibly grouping variables. diff --git a/man/convert_nuts_version.Rd b/man/convert_nuts_version.Rd index f9367e3..55fe461 100644 --- a/man/convert_nuts_version.Rd +++ b/man/convert_nuts_version.Rd @@ -29,7 +29,7 @@ population in 2011 \code{'pop11'} or 2018 \code{'pop18'}, or artificial surfaces \item{multiple_versions}{By default equal to \code{'break'}, throwing an error when providing multiple NUTS versions within groups. If set to \code{'most_frequent'} data is converted using the best-matching NUTS version.} -\item{quiet}{Suppress messages and warnings. \code{TRUE} by default.} +\item{quiet}{Suppress messages and warnings. \code{FALSE} by default.} } \value{ A tibble containing NUTS codes, converted variable values, and possibly grouping variables. diff --git a/tests/testthat/test-classify_nuts.R b/tests/testthat/test-classify_nuts.R index 09ca64c..55a76b7 100644 --- a/tests/testthat/test-classify_nuts.R +++ b/tests/testthat/test-classify_nuts.R @@ -1,5 +1,3 @@ -devtools::load_all() - # Run error tests test_that("data not valid", { expect_error( diff --git a/tests/testthat/test-convert_nuts_level.R b/tests/testthat/test-convert_nuts_level.R index ff70de1..7d8df19 100644 --- a/tests/testthat/test-convert_nuts_level.R +++ b/tests/testthat/test-convert_nuts_level.R @@ -280,4 +280,3 @@ test_that("Feeding multiple NUTS versions within groups. Option most frequent.", c(52, 4) ) }) - diff --git a/tests/testthat/test-convert_nuts_version.R b/tests/testthat/test-convert_nuts_version.R index 514a28d..812f831 100644 --- a/tests/testthat/test-convert_nuts_version.R +++ b/tests/testthat/test-convert_nuts_version.R @@ -1,5 +1,3 @@ - - # Run error tests #--------------------- test_that("data input not valid", {