Skip to content

Commit

Permalink
target_market_share now correctly handles input scenario with a h…
Browse files Browse the repository at this point in the history
…yphen in name (#464)

* add failing test

* update NEWS.md

* separate_metric only on first '-', merge rest
  • Loading branch information
jdhoffa authored Feb 6, 2024
1 parent 9fce9b2 commit 8be430f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# r2dii.analysis (development version)

* `target_market_share` now correctly handles input scenarios with a hyphen in their name (#425).

* `target_market_share` now handles `abcd` with rows where `production` is `NA` by filling with `0` (#423).

# r2dii.analysis 0.3.0
Expand Down
7 changes: 6 additions & 1 deletion R/target_market_share.R
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,12 @@ separate_metric_from_name <- function(data) {
name = sub("(production)_", "\\1-", .data$name),
name = sub("(technology_share)_", "\\1-", .data$name)
) %>%
tidyr::separate(.data$name, into = c("name", "metric"), sep = "-")
tidyr::separate(
.data$name,
into = c("name", "metric"),
sep = "-",
extra = "merge"
)
}

check_input_for_crucial_columns <- function(data, abcd, scenario) {
Expand Down
14 changes: 14 additions & 0 deletions tests/testthat/test-target_market_share.R
Original file line number Diff line number Diff line change
Expand Up @@ -1428,3 +1428,17 @@ test_that("with `abcd` with `NA` for start year, replaces `NA` with 0 (#423)", {

expect_equal(out$production, c(0, 1))
})

test_that("correctly splits scenario names with hyphen #425", {

out <- target_market_share(
fake_matched(),
fake_abcd(),
fake_scenario(scenario = "1.5c-scen"),
region_isos_stable
) %>%
filter(grepl("target", metric))

expect_equal(unique(out$metric), "target_1.5c-scen")

})

0 comments on commit 8be430f

Please sign in to comment.