Skip to content

Commit

Permalink
Move new columns to the left
Browse files Browse the repository at this point in the history
  • Loading branch information
maurolepore committed Nov 13, 2023
1 parent d7dc92e commit decbf84
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
9 changes: 6 additions & 3 deletions R/emissions_profile_any_add_values_to_categorize.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ emissions_profile_any_add_values_to_categorize <- function(data) {
check_emissions_profile_any_add_values_to_categorize(data)

if (hasName(data, "values_to_categorize")) {
return(data)
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")
}

benchmarks <- set_names(epa_benchmarks(data), flat_benchmarks(data))
map_df(benchmarks, ~ add_rank(data, .x), .id = "grouped_by")
related_cols <- c("grouped_by", "values_to_categorize")
relocate(out, all_of(related_cols))
}

check_emissions_profile_any_add_values_to_categorize <- function(data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ test_that("adds columns `grouped_by` and `values_to_categorize`", {
expect_equal(setdiff(names(out), names(co2)), new_names)
})

test_that("adds columns `grouped_by` and `values_to_categorize` to the left", {
co2 <- example_products()

out <- emissions_profile_any_add_values_to_categorize(co2)

new_names <- c("grouped_by", "values_to_categorize")
expect_equal(names(out)[1:2], new_names)
})

test_that("with one company, adds one row per benchmark per company", {
co2 <- example_products()

Expand Down Expand Up @@ -52,3 +61,10 @@ test_that("without crucial columns errors gracefully", {
bad <- select(co2, -all_of(crucial))
expect_error(emissions_profile_any_add_values_to_categorize(bad), crucial)
})

test_that("if `values_to_categorize` is present, `grouped_by` must be present", {
co2 <- example_products(values_to_categorize = 1)

crucial <- "grouped_by"
expect_error(emissions_profile_any_add_values_to_categorize(co2), crucial)
})

0 comments on commit decbf84

Please sign in to comment.