diff --git a/NAMESPACE b/NAMESPACE index 929aec9b1..7bdb5f0ff 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -8,7 +8,7 @@ export(aka) export(as_label) export(as_name) export(emissions_profile) -export(emissions_profile_any_add_values_to_categorize) +export(emissions_profile_any_add_profile_ranking) export(emissions_profile_upstream) export(enquo) export(enquos) diff --git a/NEWS.md b/NEWS.md index 924af0173..2b7fb4689 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,16 +2,16 @@ # tiltIndicator 0.0.0.9098 -* `emissions_profile_any_add_values_to_categorize()` now relocates the new +* `emissions_profile_any_add_profile_ranking()` now relocates the new columns to the left. # tiltIndicator 0.0.0.9097 -* `emissions_profile*()` uses `co2$values_to_categorize` if present (#605). +* `emissions_profile*()` uses `co2$profile_ranking` if present (#605). # tiltIndicator 0.0.0.9096 -* New pre-processing helper `emissions_profile_any_add_values_to_categorize()` +* New pre-processing helper `emissions_profile_any_add_profile_ranking()` (#602). # tiltIndicator 0.0.0.9095 diff --git a/R/emissions_profile_any_add_values_to_categorize.R b/R/emissions_profile_any_add_values_to_categorize.R index cc5ddc480..2017a2144 100644 --- a/R/emissions_profile_any_add_values_to_categorize.R +++ b/R/emissions_profile_any_add_values_to_categorize.R @@ -6,7 +6,7 @@ #' @family pre-processing helpers #' #' @return The input data frame with the additional columns `grouped_by` and -#' `values_to_categorize` and one row per benchmark per company. +#' `profile_ranking` and one row per benchmark per company. #' #' @export #' @@ -18,25 +18,25 @@ #' companies <- read_csv(toy_emissions_profile_any_companies()) #' #' products <- read_csv(toy_emissions_profile_products()) -#' products |> emissions_profile_any_add_values_to_categorize() +#' products |> emissions_profile_any_add_profile_ranking() #' #' inputs <- read_csv(toy_emissions_profile_upstream_products()) -#' inputs |> emissions_profile_any_add_values_to_categorize() -emissions_profile_any_add_values_to_categorize <- function(data) { - check_emissions_profile_any_add_values_to_categorize(data) +#' inputs |> emissions_profile_any_add_profile_ranking() +emissions_profile_any_add_profile_ranking <- function(data) { + check_emissions_profile_any_add_profile_ranking(data) - if (hasName(data, "values_to_categorize")) { + if (hasName(data, "profile_ranking")) { out <- check_crucial_names(data, "grouped_by") } else { benchmarks <- set_names(epa_benchmarks(data), flat_benchmarks(data)) out <- map_df(benchmarks, ~ add_rank(data, .x), .id = "grouped_by") } - related_cols <- c("grouped_by", "values_to_categorize") + related_cols <- c("grouped_by", "profile_ranking") relocate(out, all_of(related_cols)) } -check_emissions_profile_any_add_values_to_categorize <- function(data) { +check_emissions_profile_any_add_profile_ranking <- function(data) { crucial <- c(aka("tsector"), aka("xunit"), aka("isic"), aka("co2footprint")) walk(crucial, \(pattern) check_matches_name(data, pattern)) } @@ -64,7 +64,7 @@ add_rank <- function(data, .by) { if (identical(.by, "all")) .by <- NULL mutate( data, - values_to_categorize = rank_proportion(.data[[extract_name(data, aka("co2footprint"))]]), + profile_ranking = rank_proportion(.data[[extract_name(data, aka("co2footprint"))]]), .by = all_of(.by) ) } diff --git a/R/emissions_profile_any_at_product_level.R b/R/emissions_profile_any_at_product_level.R index 3b41a6810..515a5ef84 100644 --- a/R/emissions_profile_any_at_product_level.R +++ b/R/emissions_profile_any_at_product_level.R @@ -10,7 +10,7 @@ emissions_profile_any_at_product_level <- function(companies, .co2 <- prepare_co2(co2, low_threshold, high_threshold) .co2 |> - emissions_profile_any_add_values_to_categorize() |> + emissions_profile_any_add_profile_ranking() |> add_risk_category(low_threshold, high_threshold) |> join_companies(.companies) |> epa_select_cols_at_product_level() |> diff --git a/R/sector_profile_at_product_level.R b/R/sector_profile_at_product_level.R index 6d62d95ec..ae80f525e 100644 --- a/R/sector_profile_at_product_level.R +++ b/R/sector_profile_at_product_level.R @@ -9,7 +9,7 @@ sector_profile_at_product_level <- function(companies, .scenarios <- prepare_scenarios(scenarios, low_threshold, high_threshold) .companies |> - spa_add_values_to_categorize(.scenarios) |> + spa_add_profile_ranking(.scenarios) |> add_risk_category(low_threshold, high_threshold, .default = NA) |> spa_polish_output_at_product_level() |> sp_select_cols_at_product_level() |> diff --git a/R/sector_profile_upstream_at_product_level.R b/R/sector_profile_upstream_at_product_level.R index bd7f812d2..1049a4982 100644 --- a/R/sector_profile_upstream_at_product_level.R +++ b/R/sector_profile_upstream_at_product_level.R @@ -11,7 +11,7 @@ sector_profile_upstream_at_product_level <- function(companies, .inputs <- prepare_inputs(inputs) .inputs |> - spa_add_values_to_categorize(.scenarios) |> + spa_add_profile_ranking(.scenarios) |> add_risk_category(low_threshold, high_threshold, .default = NA) |> join_companies(remove_col_scenario(.companies)) |> spa_polish_output_at_product_level() |> diff --git a/R/spa.R b/R/spa.R index e503b0971..a7852331d 100644 --- a/R/spa.R +++ b/R/spa.R @@ -39,10 +39,10 @@ prepare_scenarios <- function(data, low_threshold, high_threshold) { data |> mutate(low_threshold = low_threshold, high_threshold = high_threshold) |> distinct() |> - rename(values_to_categorize = aka("co2reduce")) + rename(profile_ranking = aka("co2reduce")) } -spa_add_values_to_categorize <- function(data, scenarios) { +spa_add_profile_ranking <- function(data, scenarios) { left_join( data, scenarios, by = c(aka("scenario_type"), aka("xsector"), aka("xsubsector")), diff --git a/R/utils.R b/R/utils.R index bac484142..32719de27 100644 --- a/R/utils.R +++ b/R/utils.R @@ -105,7 +105,7 @@ abort_missing_names <- function(missing_names) { add_risk_category <- function(data, low_threshold, high_threshold, ...) { mutate(data, risk_category = categorize_risk( - .data$values_to_categorize, .data$low_threshold, .data$high_threshold, ... + .data$profile_ranking, .data$low_threshold, .data$high_threshold, ... )) } diff --git a/man/emissions_profile_any_add_values_to_categorize.Rd b/man/emissions_profile_any_add_values_to_categorize.Rd index fe05c766d..a6b8075ef 100644 --- a/man/emissions_profile_any_add_values_to_categorize.Rd +++ b/man/emissions_profile_any_add_values_to_categorize.Rd @@ -1,11 +1,11 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in -% R/emissions_profile_any_add_values_to_categorize.R -\name{emissions_profile_any_add_values_to_categorize} -\alias{emissions_profile_any_add_values_to_categorize} +% R/emissions_profile_any_add_profile_ranking.R +\name{emissions_profile_any_add_profile_ranking} +\alias{emissions_profile_any_add_profile_ranking} \title{Add values to categorize} \usage{ -emissions_profile_any_add_values_to_categorize(data) +emissions_profile_any_add_profile_ranking(data) } \arguments{ \item{data}{A "co2-like" data frame -- i.e. containing products or @@ -13,7 +13,7 @@ upstream-products (a.k.a. inputs).} } \value{ The input data frame with the additional columns \code{grouped_by} and -\code{values_to_categorize} and one row per benchmark per company. +\code{profile_ranking} and one row per benchmark per company. } \description{ Add values to categorize @@ -26,10 +26,10 @@ options(readr.show_col_types = FALSE) companies <- read_csv(toy_emissions_profile_any_companies()) products <- read_csv(toy_emissions_profile_products()) -products |> emissions_profile_any_add_values_to_categorize() +products |> emissions_profile_any_add_profile_ranking() inputs <- read_csv(toy_emissions_profile_upstream_products()) -inputs |> emissions_profile_any_add_values_to_categorize() +inputs |> emissions_profile_any_add_profile_ranking() } \seealso{ Other pre-processing helpers: diff --git a/man/sector_profile_any_pivot_type_sector_subsector.Rd b/man/sector_profile_any_pivot_type_sector_subsector.Rd index 7bf4cbccd..4ad0da534 100644 --- a/man/sector_profile_any_pivot_type_sector_subsector.Rd +++ b/man/sector_profile_any_pivot_type_sector_subsector.Rd @@ -34,7 +34,7 @@ companies } \seealso{ Other pre-processing helpers: -\code{\link{emissions_profile_any_add_values_to_categorize}()}, +\code{\link{emissions_profile_any_add_profile_ranking}()}, \code{\link{sector_profile_any_prepare_scenario}()}, \code{\link{sector_profile_any_prune_companies}()} } diff --git a/man/sector_profile_any_prepare_scenario.Rd b/man/sector_profile_any_prepare_scenario.Rd index 060967634..13224a890 100644 --- a/man/sector_profile_any_prepare_scenario.Rd +++ b/man/sector_profile_any_prepare_scenario.Rd @@ -28,7 +28,7 @@ sector_profile_any_prepare_scenario(raw_scenarios) } \seealso{ Other pre-processing helpers: -\code{\link{emissions_profile_any_add_values_to_categorize}()}, +\code{\link{emissions_profile_any_add_profile_ranking}()}, \code{\link{sector_profile_any_pivot_type_sector_subsector}()}, \code{\link{sector_profile_any_prune_companies}()} } diff --git a/man/sector_profile_any_prune_companies.Rd b/man/sector_profile_any_prune_companies.Rd index 56b10ad44..ab9ed0b64 100644 --- a/man/sector_profile_any_prune_companies.Rd +++ b/man/sector_profile_any_prune_companies.Rd @@ -39,7 +39,7 @@ sector_profile_any_prune_companies(companies) } \seealso{ Other pre-processing helpers: -\code{\link{emissions_profile_any_add_values_to_categorize}()}, +\code{\link{emissions_profile_any_add_profile_ranking}()}, \code{\link{sector_profile_any_pivot_type_sector_subsector}()}, \code{\link{sector_profile_any_prepare_scenario}()} } diff --git a/tests/testthat/test-emissions_profile_any_add_values_to_categorize.R b/tests/testthat/test-emissions_profile_any_add_values_to_categorize.R index a959ec44c..c6b7980c8 100644 --- a/tests/testthat/test-emissions_profile_any_add_values_to_categorize.R +++ b/tests/testthat/test-emissions_profile_any_add_values_to_categorize.R @@ -1,33 +1,33 @@ test_that("works with any 'co2-like' dataset", { co2 <- example_products() - expect_no_error(emissions_profile_any_add_values_to_categorize(co2)) + expect_no_error(emissions_profile_any_add_profile_ranking(co2)) co2 <- example_inputs() - expect_no_error(emissions_profile_any_add_values_to_categorize(co2)) + expect_no_error(emissions_profile_any_add_profile_ranking(co2)) }) -test_that("adds columns `grouped_by` and `values_to_categorize`", { +test_that("adds columns `grouped_by` and `profile_ranking`", { co2 <- example_products() - out <- emissions_profile_any_add_values_to_categorize(co2) + out <- emissions_profile_any_add_profile_ranking(co2) - new_names <- c("grouped_by", "values_to_categorize") + new_names <- c("grouped_by", "profile_ranking") expect_equal(setdiff(names(out), names(co2)), new_names) }) -test_that("adds columns `grouped_by` and `values_to_categorize` to the left", { +test_that("adds columns `grouped_by` and `profile_ranking` to the left", { co2 <- example_products() - out <- emissions_profile_any_add_values_to_categorize(co2) + out <- emissions_profile_any_add_profile_ranking(co2) - new_names <- c("grouped_by", "values_to_categorize") + new_names <- c("grouped_by", "profile_ranking") expect_equal(names(out)[1:2], new_names) }) test_that("with one company, adds one row per benchmark per company", { co2 <- example_products() - out <- emissions_profile_any_add_values_to_categorize(co2) + out <- emissions_profile_any_add_profile_ranking(co2) number_of_benchmarks <- length(flat_benchmarks(co2)) expect_equal(nrow(out), number_of_benchmarks) @@ -36,7 +36,7 @@ test_that("with one company, adds one row per benchmark per company", { test_that("with two companies, adds one row per benchmark per company", { co2 <- example_products(!!aka("id") := c("a", "b")) - out <- emissions_profile_any_add_values_to_categorize(co2) + out <- emissions_profile_any_add_profile_ranking(co2) number_of_benchmarks <- length(flat_benchmarks(co2)) expect_equal(nrow(out), 2 * number_of_benchmarks) @@ -47,24 +47,24 @@ test_that("without crucial columns errors gracefully", { crucial <- aka("tsector") bad <- select(co2, -all_of(crucial)) - expect_error(emissions_profile_any_add_values_to_categorize(bad), crucial) + expect_error(emissions_profile_any_add_profile_ranking(bad), crucial) crucial <- aka("xunit") bad <- select(co2, -all_of(crucial)) - expect_error(emissions_profile_any_add_values_to_categorize(bad), crucial) + expect_error(emissions_profile_any_add_profile_ranking(bad), crucial) crucial <- aka("isic") bad <- select(co2, -all_of(crucial)) - expect_error(emissions_profile_any_add_values_to_categorize(bad), crucial) + expect_error(emissions_profile_any_add_profile_ranking(bad), crucial) crucial <- aka("co2footprint") bad <- select(co2, -all_of(crucial)) - expect_error(emissions_profile_any_add_values_to_categorize(bad), crucial) + expect_error(emissions_profile_any_add_profile_ranking(bad), crucial) }) -test_that("if `values_to_categorize` is present, `grouped_by` must be present", { - co2 <- example_products(values_to_categorize = 1) +test_that("if `profile_ranking` is present, `grouped_by` must be present", { + co2 <- example_products(profile_ranking = 1) crucial <- "grouped_by" - expect_error(emissions_profile_any_add_values_to_categorize(co2), crucial) + expect_error(emissions_profile_any_add_profile_ranking(co2), crucial) }) diff --git a/tests/testthat/test-emissions_profile_any_at_product_level.R b/tests/testthat/test-emissions_profile_any_at_product_level.R index 16fbd8b48..dc61ee3cd 100644 --- a/tests/testthat/test-emissions_profile_any_at_product_level.R +++ b/tests/testthat/test-emissions_profile_any_at_product_level.R @@ -287,46 +287,46 @@ test_that("`*rowid` columns are passed through inputs with duplicates", { expect_true(hasName(out, "products_rowid")) }) -test_that("with products, uses `co2$values_to_categorize` if present (#603)", { +test_that("with products, uses `co2$profile_ranking` if present (#603)", { companies <- example_companies() co2 <- example_products(!!aka("uid") := c("a", "b")) co2[extract_name(co2, aka("co2footprint"))] <- 1:2 - lacks_values_to_categorize <- !hasName(co2, "values_to_categorize") - stopifnot(lacks_values_to_categorize) + lacks_profile_ranking <- !hasName(co2, "profile_ranking") + stopifnot(lacks_profile_ranking) out1 <- emissions_profile_any_at_product_level(companies, co2) using_computed_values <- unique(out1$risk_category) - pre_computed <- emissions_profile_any_add_values_to_categorize(co2) - has_values_to_categorize <- hasName(pre_computed, "values_to_categorize") - stopifnot(has_values_to_categorize) + pre_computed <- emissions_profile_any_add_profile_ranking(co2) + has_profile_ranking <- hasName(pre_computed, "profile_ranking") + stopifnot(has_profile_ranking) yields_a_different_risk_category <- 999 - pre_computed$values_to_categorize <- yields_a_different_risk_category + pre_computed$profile_ranking <- yields_a_different_risk_category out2 <- emissions_profile_any_at_product_level(companies, pre_computed) using_pre_computed_values <- unique(out2$risk_category) expect_false(identical(using_computed_values, using_pre_computed_values)) }) -test_that("with inputs, uses `co2$values_to_categorize` if present (#603)", { +test_that("with inputs, uses `co2$profile_ranking` if present (#603)", { companies <- example_companies() co2 <- example_inputs(!!aka("uid") := c("a", "b")) # This is wrong: names(example_inputs(!!aka("co2footprint") := 1:2)) # It yields two columns: `co2_footprint` AND `input_co2_footprint` co2[extract_name(co2, aka("co2footprint"))] <- 1:2 - lacks_values_to_categorize <- !hasName(co2, "values_to_categorize") - stopifnot(lacks_values_to_categorize) + lacks_profile_ranking <- !hasName(co2, "profile_ranking") + stopifnot(lacks_profile_ranking) out1 <- emissions_profile_any_at_product_level(companies, co2) using_computed_values <- unique(out1$risk_category) - pre_computed <- emissions_profile_any_add_values_to_categorize(co2) - has_values_to_categorize <- hasName(pre_computed, "values_to_categorize") - stopifnot(has_values_to_categorize) + pre_computed <- emissions_profile_any_add_profile_ranking(co2) + has_profile_ranking <- hasName(pre_computed, "profile_ranking") + stopifnot(has_profile_ranking) yields_a_different_risk_category <- 999 - pre_computed$values_to_categorize <- yields_a_different_risk_category + pre_computed$profile_ranking <- yields_a_different_risk_category out2 <- emissions_profile_any_at_product_level(companies, pre_computed) using_pre_computed_values <- unique(out2$risk_category)