From 8be430fe45c73ede763554f92d1af897c82aa8aa Mon Sep 17 00:00:00 2001 From: Jackson Hoffart Date: Tue, 6 Feb 2024 14:46:38 +0100 Subject: [PATCH] `target_market_share` now correctly handles input `scenario` with a hyphen in name (#464) * add failing test * update NEWS.md * separate_metric only on first '-', merge rest --- NEWS.md | 2 ++ R/target_market_share.R | 7 ++++++- tests/testthat/test-target_market_share.R | 14 ++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index a9a6d88c..570f1eaf 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 diff --git a/R/target_market_share.R b/R/target_market_share.R index 7b5452a5..d253fba4 100644 --- a/R/target_market_share.R +++ b/R/target_market_share.R @@ -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) { diff --git a/tests/testthat/test-target_market_share.R b/tests/testthat/test-target_market_share.R index 5765793a..5637cc71 100644 --- a/tests/testthat/test-target_market_share.R +++ b/tests/testthat/test-target_market_share.R @@ -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") + +})