diff --git a/R/plot_aggregate_loanbooks.R b/R/plot_aggregate_loanbooks.R index d2a2a5ce..453f1df0 100644 --- a/R/plot_aggregate_loanbooks.R +++ b/R/plot_aggregate_loanbooks.R @@ -220,7 +220,7 @@ plot_aggregate_loanbooks <- function(config) { ### scatter plot alignment by exposure and sector comparison---- year_scatter_alignment_exposure <- 2027 region_scatter_alignment_exposure <- region_select - currency <- unique(company_aggregated_alignment_net$loan_size_outstanding_currency) + currency <- unique(company_aggregated_alignment_net[["loan_size_outstanding_currency"]]) if (length(by_group) <= 1) { if ( nrow(loanbook_exposure_aggregated_alignment_net) > 0 @@ -533,7 +533,7 @@ plot_aggregate_loanbooks <- function(config) { if (length(by_group) == 1) { unique_by_group <- company_aggregated_alignment_bo_po %>% dplyr::filter( - .data$sector == .env$sector_scatter, + .data[["sector"]] == .env[["sector_scatter"]], !grepl("benchmark_corporate_economy_", !!rlang::sym(by_group)) ) %>% dplyr::pull(!!rlang::sym(by_group)) %>% @@ -596,7 +596,7 @@ plot_aggregate_loanbooks <- function(config) { if (length(by_group) == 1) { unique_by_group <- company_aggregated_alignment_bo_po %>% dplyr::filter( - .data$sector == .env$sector_scatter, + .data[["sector"]] == .env[["sector_scatter"]], !grepl("benchmark_corporate_economy_", !!rlang::sym(by_group)) ) %>% dplyr::pull(!!rlang::sym(by_group)) %>% @@ -663,7 +663,7 @@ plot_aggregate_loanbooks <- function(config) { if (length(by_group) == 1) { unique_by_group <- company_aggregated_alignment_bo_po %>% dplyr::filter( - .data$sector == .env$sector_scatter, + .data[["sector"]] == .env[["sector_scatter"]], !grepl("benchmark_corporate_economy_", !!rlang::sym(by_group)) ) %>% dplyr::pull(!!rlang::sym(by_group)) %>% @@ -726,7 +726,7 @@ plot_aggregate_loanbooks <- function(config) { if (length(by_group) == 1) { unique_by_group <- company_aggregated_alignment_bo_po %>% dplyr::filter( - .data$sector == .env$sector_scatter, + .data[["sector"]] == .env[["sector_scatter"]], !grepl("benchmark_corporate_economy_", !!rlang::sym(by_group)) ) %>% dplyr::pull(!!rlang::sym(by_group)) %>% diff --git a/R/plot_sankey.R b/R/plot_sankey.R index 5e2cce7b..b7c2ada1 100644 --- a/R/plot_sankey.R +++ b/R/plot_sankey.R @@ -48,12 +48,12 @@ plot_sankey <- function(data, data_links <- data %>% dplyr::mutate( group_var = r2dii.plot::to_title(!!rlang::sym(group_var)), - middle_node = r2dii.plot::to_title(.data$middle_node) + middle_node = r2dii.plot::to_title(.data[["middle_node"]]) ) if ("middle_node2" %in% names(data_links)) { data_links <- data_links %>% dplyr::mutate( - middle_node2 = r2dii.plot::to_title(.data$middle_node2) + middle_node2 = r2dii.plot::to_title(.data[["middle_node2"]]) ) } } else { @@ -62,7 +62,7 @@ plot_sankey <- function(data, links_1 <- data_links %>% dplyr::select( - source = .env$group_var, + source = .env[["group_var"]], target = "middle_node", value = "loan_size_outstanding", group = "is_aligned" @@ -71,7 +71,7 @@ plot_sankey <- function(data, if ("middle_node2" %in% names(data_links)) { links_2 <- data_links %>% dplyr::select( - .env$group_var, + .env[["group_var"]], source = "middle_node", target = "middle_node2", value = "loan_size_outstanding", @@ -80,7 +80,7 @@ plot_sankey <- function(data, links_3 <- data_links %>% dplyr::select( - .env$group_var, + .env[["group_var"]], source = "middle_node2", target = "is_aligned", value = "loan_size_outstanding", @@ -91,7 +91,7 @@ plot_sankey <- function(data, } else { links_2 <- data_links %>% dplyr::select( - .env$group_var, + .env[["group_var"]], source = "middle_node", target = "is_aligned", value = "loan_size_outstanding", @@ -102,10 +102,10 @@ plot_sankey <- function(data, } links <- links %>% - dplyr::group_by(.data$source, .data$target, .data$group) %>% - dplyr::summarise(value = sum(.data$value, na.rm = TRUE)) %>% + dplyr::group_by(.data[["source"]], .data[["target"]], .data[["group"]]) %>% + dplyr::summarise(value = sum(.data[["value"]], na.rm = TRUE)) %>% dplyr::ungroup() %>% - dplyr::arrange(.data$source, .data$group) %>% + dplyr::arrange(.data[["source"]], .data[["group"]]) %>% as.data.frame() # TODO: colour the companies if fully aligned or not @@ -114,7 +114,7 @@ plot_sankey <- function(data, ) %>% dplyr::mutate( group = dplyr::case_when( - .data$name %in% c("Aligned", "Not aligned", "Unknown") ~ .data$name, + .data[["name"]] %in% c("Aligned", "Not aligned", "Unknown") ~ .data[["name"]], TRUE ~ "other" ) ) diff --git a/R/plot_scatter.R b/R/plot_scatter.R index bdf9a61a..a847de08 100644 --- a/R/plot_scatter.R +++ b/R/plot_scatter.R @@ -85,18 +85,18 @@ plot_scatter <- function(data, if (!is.null(floor_outliers)) { data <- data %>% dplyr::mutate( - buildout = dplyr::if_else(.data$buildout <= .env$floor_outliers, .env$floor_outliers, .data$buildout), - phaseout = dplyr::if_else(.data$phaseout <= .env$floor_outliers, .env$floor_outliers, .data$phaseout), - net = dplyr::if_else(.data$net <= .env$floor_outliers, .env$floor_outliers, .data$net) + buildout = dplyr::if_else(.data[["buildout"]] <= .env[["floor_outliers"]], .env[["floor_outliers"]], .data[["buildout"]]), + phaseout = dplyr::if_else(.data[["phaseout"]] <= .env[["floor_outliers"]], .env[["floor_outliers"]], .data[["phaseout"]]), + net = dplyr::if_else(.data[["net"]] <= .env[["floor_outliers"]], .env[["floor_outliers"]], .data[["net"]]) ) subtitle <- glue::glue("{subtitle}\nThe outliers are displayed on the borders of the plot.", .trim = FALSE) } if (!is.null(cap_outliers)) { data <- data %>% dplyr::mutate( - buildout = dplyr::if_else(.data$buildout >= .env$cap_outliers, .env$cap_outliers, .data$buildout), - phaseout = dplyr::if_else(.data$phaseout >= .env$cap_outliers, .env$cap_outliers, .data$phaseout), - net = dplyr::if_else(.data$net >= .env$cap_outliers, .env$cap_outliers, .data$net) + buildout = dplyr::if_else(.data[["buildout"]] >= .env[["cap_outliers"]], .env[["cap_outliers"]], .data[["buildout"]]), + phaseout = dplyr::if_else(.data[["phaseout"]] >= .env[["cap_outliers"]], .env[["cap_outliers"]], .data[["phaseout"]]), + net = dplyr::if_else(.data[["net"]] >= .env[["cap_outliers"]], .env[["cap_outliers"]], .data[["net"]]) ) if (is.null(floor_outliers)) { subtitle <- glue::glue("{subtitle}\nThe outliers are displayed on the borders of the plot.", .trim = FALSE) @@ -113,7 +113,7 @@ plot_scatter <- function(data, net = c(0, 0, 0) ) - p <- ggplot2::ggplot(data, ggplot2::aes(x = .data$buildout, y = .data$phaseout, colour = .data$net)) + + p <- ggplot2::ggplot(data, ggplot2::aes(x = .data[["buildout"]], y = .data[["phaseout"]], colour = .data[["net"]])) + ggplot2::geom_hline(yintercept = 0, colour = "#c0c0c0") + ggplot2::geom_vline(xintercept = 0, colour = "#c0c0c0") + ggplot2::geom_line(data = data_net_0) + @@ -162,7 +162,7 @@ plot_scatter <- function(data, size = 3, hjust = 0 ) + - ggplot2::geom_point(ggplot2::aes(shape = .data$datapoint)) + + ggplot2::geom_point(ggplot2::aes(shape = .data[["datapoint"]])) + ggplot2::scale_x_continuous( name = "Deviation from scenario value\nfor low-carbon technologies build-out", labels = scales::percent, diff --git a/R/plot_scatter_alignment_exposure.R b/R/plot_scatter_alignment_exposure.R index f24fdcc7..4a7fe9f4 100644 --- a/R/plot_scatter_alignment_exposure.R +++ b/R/plot_scatter_alignment_exposure.R @@ -40,9 +40,9 @@ plot_scatter_alignment_exposure <- function(data, data <- data %>% dplyr::mutate( exposure_weighted_net_alignment = dplyr::if_else( - .data$exposure_weighted_net_alignment <= .env$floor_outliers, - .env$floor_outliers, - .data$exposure_weighted_net_alignment + .data[["exposure_weighted_net_alignment"]] <= .env[["floor_outliers"]], + .env[["floor_outliers"]], + .data[["exposure_weighted_net_alignment"]] ) ) } @@ -51,9 +51,9 @@ plot_scatter_alignment_exposure <- function(data, data <- data %>% dplyr::mutate( exposure_weighted_net_alignment = dplyr::if_else( - .data$exposure_weighted_net_alignment >= .env$cap_outliers, - .env$cap_outliers, - .data$exposure_weighted_net_alignment + .data[["exposure_weighted_net_alignment"]] >= .env[["cap_outliers"]], + .env[["cap_outliers"]], + .data[["exposure_weighted_net_alignment"]] ) ) } @@ -68,11 +68,11 @@ plot_scatter_alignment_exposure <- function(data, } plot <- data %>% - dplyr::mutate(sector = tools::toTitleCase(.data$sector)) %>% + dplyr::mutate(sector = tools::toTitleCase(.data[["sector"]])) %>% ggplot2::ggplot( ggplot2::aes( - x = .data$sum_loan_size_outstanding, - y = .data$exposure_weighted_net_alignment, + x = .data[["sum_loan_size_outstanding"]], + y = .data[["exposure_weighted_net_alignment"]], color = !!rlang::sym(group_var) ) ) + diff --git a/R/plot_scatter_animated.R b/R/plot_scatter_animated.R index 4021fee1..9ab6065d 100644 --- a/R/plot_scatter_animated.R +++ b/R/plot_scatter_animated.R @@ -81,12 +81,12 @@ plot_scatter_animated <- function(data, if (!is.null(floor_outliers)) { data <- data %>% dplyr::mutate( - buildout = dplyr::if_else(.data$buildout <= .env$floor_outliers, .env$floor_outliers, .data$buildout), - phaseout = dplyr::if_else(.data$phaseout <= .env$floor_outliers, .env$floor_outliers, .data$phaseout), + buildout = dplyr::if_else(.data[["buildout"]] <= .env[["floor_outliers"]], .env[["floor_outliers"]], .data[["buildout"]]), + phaseout = dplyr::if_else(.data[["phaseout"]] <= .env[["floor_outliers"]], .env[["floor_outliers"]], .data[["phaseout"]]), net = dplyr::if_else( - .data$buildout <= .env$floor_outliers | .data$phaseout <= .env$floor_outliers, - .data$buildout + .data$phaseout, - .data$net + .data[["buildout"]] <= .env[["floor_outliers"]] | .data[["phaseout"]] <= .env[["floor_outliers"]], + .data[["buildout"]] + .data[["phaseout"]], + .data[["net"]] ) # net is a sum of buildout and phaseout ) subtitle <- glue::glue("{subtitle}\nThe outliers are displayed on the borders of the plot.", .trim = FALSE) @@ -94,12 +94,12 @@ plot_scatter_animated <- function(data, if (!is.null(cap_outliers)) { data <- data %>% dplyr::mutate( - buildout = dplyr::if_else(.data$buildout >= .env$cap_outliers, .env$cap_outliers, .data$buildout), - phaseout = dplyr::if_else(.data$phaseout >= .env$cap_outliers, .env$cap_outliers, .data$phaseout), + buildout = dplyr::if_else(.data[["buildout"]] >= .env[["cap_outliers"]], .env[["cap_outliers"]], .data[["buildout"]]), + phaseout = dplyr::if_else(.data[["phaseout"]] >= .env[["cap_outliers"]], .env[["cap_outliers"]], .data[["phaseout"]]), net = dplyr::if_else( - .data$buildout >= .env$cap_outliers | .data$phaseout >= .env$cap_outliers, - .data$buildout + .data$phaseout, - .data$net + .data[["buildout"]] >= .env[["cap_outliers"]] | .data[["phaseout"]] >= .env[["cap_outliers"]], + .data[["buildout"]] + .data[["phaseout"]], + .data[["net"]] ) # net is a sum of buildout and phaseout ) if (is.null(floor_outliers)) { diff --git a/R/plots.R b/R/plots.R index 1ad3227c..e1c2c46c 100644 --- a/R/plots.R +++ b/R/plots.R @@ -24,9 +24,9 @@ plot_match_success_rate <- function(data, stop_if_not_inherits(currency, "character") data <- data %>% - dplyr::filter(.data$sector != "not in scope") %>% - dplyr::filter(.data$metric_type == .env$metric_type) %>% - dplyr::filter(.data$match_success_type == .env$match_success_type) + dplyr::filter(.data[["sector"]] != "not in scope") %>% + dplyr::filter(.data[["metric_type"]] == .env[["metric_type"]]) %>% + dplyr::filter(.data[["match_success_type"]] == .env[["match_success_type"]]) # plot design fill_scale <- c( @@ -139,30 +139,30 @@ generate_individual_outputs <- function(data, data <- data %>% dplyr::filter( - .data[[by_group]] == .env$by_group_value, - .data[["scenario_source"]] == .env$scenario_source, - .data[["region"]] == .env$region, - .data[["sector"]] %in% .env$sector + .data[[by_group]] == .env[["by_group_value"]], + .data[["scenario_source"]] == .env[["scenario_source"]], + .data[["region"]] == .env[["region"]], + .data[["sector"]] %in% .env[["sector"]] ) matched_prioritized <- matched_prioritized %>% dplyr::filter( - .data[[by_group]] == .env$by_group_value, - .data[["sector"]] %in% .env$sector + .data[[by_group]] == .env[["by_group_value"]], + .data[["sector"]] %in% .env[["sector"]] ) if (target_type == "tms") { # plot tech mix for given sector data_techmix <- data %>% dplyr::filter( - .data[["metric"]] %in% c("projected", "corporate_economy", .env$target_scenario), - dplyr::between(.data[["year"]], .env$start_year, .env$start_year + .env$time_horizon) + .data[["metric"]] %in% c("projected", "corporate_economy", .env[["target_scenario"]]), + dplyr::between(.data[["year"]], .env[["start_year"]], .env[["start_year"]] + .env[["time_horizon"]]) ) %>% dplyr::mutate( label = dplyr::case_when( .data[["metric"]] == "projected" ~ "Portfolio", .data[["metric"]] == "corporate_economy" ~ "Corporate Economy", - .data[["metric"]] == .env$target_scenario ~ glue::glue("{r2dii.plot::to_title(toupper(.env$scenario))} Scenario") + .data[["metric"]] == .env[["target_scenario"]] ~ glue::glue("{r2dii.plot::to_title(toupper(.env$scenario))} Scenario") ) ) %>% r2dii.plot::prep_techmix( @@ -207,13 +207,13 @@ generate_individual_outputs <- function(data, # plot trajectory charts for all available techs in given sector technologies_in_sector <- r2dii.data::increasing_or_decreasing %>% - dplyr::filter(.data[["sector"]] == .env$sector) %>% + dplyr::filter(.data[["sector"]] == .env[["sector"]]) %>% dplyr::pull(.data[["technology"]]) technologies_to_plot <- data %>% dplyr::filter( - .data[["metric"]] == .env$target_scenario, - .data[["technology"]] %in% .env$technologies_in_sector + .data[["metric"]] == .env[["target_scenario"]], + .data[["technology"]] %in% .env[["technologies_in_sector"]] ) %>% dplyr::distinct(.data[["technology"]]) %>% dplyr::arrange(.data[["technology"]]) %>% @@ -222,8 +222,8 @@ generate_individual_outputs <- function(data, for (i in 1:length(technologies_to_plot)) { data_trajectory <- data %>% dplyr::filter( - .data[["technology"]] == .env$technologies_to_plot[i], - dplyr::between(.data[["year"]], .env$start_year, .env$start_year + .env$time_horizon) + .data[["technology"]] == .env[["technologies_to_plot"]][i], + dplyr::between(.data[["year"]], .env[["start_year"]], .env[["start_year"]] + .env[["time_horizon"]]) ) %>% r2dii.plot::prep_trajectory( convert_label = r2dii.plot::recode_metric_trajectory, @@ -271,15 +271,15 @@ generate_individual_outputs <- function(data, dplyr::filter( dplyr::between( .data[["year"]], - .env$start_year, - .env$start_year + .env$time_horizon) + .env[["start_year"]], + .env[["start_year"]] + .env[["time_horizon"]]) ) %>% dplyr::filter( .data[["emission_factor_metric"]] %in% c( "projected", "corporate_economy", - .env$target_scenario, - .env$adjusted_scenario + .env[["target_scenario"]], + .env[["adjusted_scenario"]] ) ) %>% dplyr::mutate( @@ -288,8 +288,8 @@ generate_individual_outputs <- function(data, levels = c( "projected", "corporate_economy", - .env$target_scenario, - .env$adjusted_scenario + .env[["target_scenario"]], + .env[["adjusted_scenario"]] ) ) ) %>% @@ -350,7 +350,7 @@ generate_individual_outputs <- function(data, dplyr::select( dplyr::all_of( c( - .env$by_group, "name_abcd", "sector_abcd", "loan_size_outstanding", + .env[["by_group"]], "name_abcd", "sector_abcd", "loan_size_outstanding", "loan_size_outstanding_currency", "loan_size_credit_limit", "loan_size_credit_limit_currency" ) diff --git a/R/prep_sankey.R b/R/prep_sankey.R index 62d21105..5797dfb1 100644 --- a/R/prep_sankey.R +++ b/R/prep_sankey.R @@ -46,8 +46,8 @@ prep_sankey <- function(data_alignment, data_alignment <- data_alignment %>% dplyr::filter( - .data$region == .env$region, - .data$year == .env$year + .data[["region"]] == .env[["region"]], + .data[["year"]] == .env[["year"]] ) if (is.null(middle_node2)) { @@ -61,26 +61,26 @@ prep_sankey <- function(data_alignment, middle_node = !!rlang::sym(middle_node) ) %>% dplyr::select(group_var, "middle_node", "is_aligned", "loan_size_outstanding") %>% - dplyr::group_by(!!rlang::sym(group_var), .data$middle_node, .data$is_aligned) %>% - dplyr::summarise(loan_size_outstanding = sum(.data$loan_size_outstanding, na.rm = TRUE)) %>% + dplyr::group_by(!!rlang::sym(group_var), .data[["middle_node"]], .data[["is_aligned"]]) %>% + dplyr::summarise(loan_size_outstanding = sum(.data[["loan_size_outstanding"]], na.rm = TRUE)) %>% dplyr::ungroup() %>% - dplyr::arrange(!!rlang::sym(group_var), .data$is_aligned) + dplyr::arrange(!!rlang::sym(group_var), .data[["is_aligned"]]) } else { data_out <- data_alignment %>% dplyr::mutate( is_aligned = dplyr::case_when( - alignment_metric >= 0 ~ "Aligned", - alignment_metric < 0 ~ "Not aligned", + .data[["alignment_metric"]] >= 0 ~ "Aligned", + .data[["alignment_metric"]] < 0 ~ "Not aligned", TRUE ~ "Unknown" ), middle_node = !!rlang::sym(middle_node), middle_node2 = !!rlang::sym(middle_node2) ) %>% dplyr::select(group_var, "middle_node", "middle_node2", "is_aligned", "loan_size_outstanding") %>% - dplyr::group_by(!!rlang::sym(group_var), .data$middle_node, .data$middle_node2, .data$is_aligned) %>% - dplyr::summarise(loan_size_outstanding = sum(.data$loan_size_outstanding, na.rm = TRUE)) %>% + dplyr::group_by(!!rlang::sym(group_var), .data[["middle_node"]], .data[["middle_node2"]], .data[["is_aligned"]]) %>% + dplyr::summarise(loan_size_outstanding = sum(.data[["loan_size_outstanding"]], na.rm = TRUE)) %>% dplyr::ungroup() %>% - dplyr::arrange(!!rlang::sym(group_var), .data$is_aligned) + dplyr::arrange(!!rlang::sym(group_var), .data[["is_aligned"]]) } data_out } diff --git a/R/prep_scatter.R b/R/prep_scatter.R index c55dea16..b189e1f1 100644 --- a/R/prep_scatter.R +++ b/R/prep_scatter.R @@ -69,9 +69,9 @@ prep_scatter <- function(data_bopo, data_scatter <- data_bopo %>% dplyr::bind_rows(data_net) %>% dplyr::filter( - .data$year == .env$year, - .data$sector == .env$sector, - .data$region == .env$region, + .data[["year"]] == .env[["year"]], + .data[["sector"]] == .env[["sector"]], + .data[["region"]] == .env[["region"]], !!rlang::sym(group_var) %in% groups_to_plot ) %>% dplyr::select("name" = name_col, "direction", "value" = value_col) %>% @@ -79,9 +79,9 @@ prep_scatter <- function(data_bopo, tidyr::pivot_wider(names_from = "direction", values_from = "value") %>% dplyr::mutate( datapoint = dplyr::case_when( - grepl(".*[Bb]enchmark,*", .data$name) ~ "benchmark", - TRUE & (data_level == "group_var") ~ "group", - TRUE & (data_level == "company") ~ "company", + grepl(".*[Bb]enchmark,*", .data[["name"]]) ~ "benchmark", + TRUE & (.env[["data_level"]] == "group_var") ~ "group", + TRUE & (.env[["data_level"]] == "company") ~ "company", TRUE ~ "other" ) ) diff --git a/R/prep_scatter_alignment_exposure.R b/R/prep_scatter_alignment_exposure.R index ef78f9a9..5ea53e88 100644 --- a/R/prep_scatter_alignment_exposure.R +++ b/R/prep_scatter_alignment_exposure.R @@ -40,12 +40,12 @@ prep_scatter_alignment_exposure <- function(data, data <- data %>% dplyr::filter( - !grepl(paste0(.env$exclude_groups, collapse = "|"), !!rlang::sym(group_var)) + !grepl(paste0(.env[["exclude_groups"]], collapse = "|"), !!rlang::sym(group_var)) ) %>% dplyr::filter( - .data$year == .env$year, - .data$region == .env$region, - .data$scenario == .env$scenario + .data[["year"]] == .env[["year"]], + .data[["region"]] == .env[["region"]], + .data[["scenario"]] == .env[["scenario"]] ) %>% dplyr::select( dplyr::all_of( diff --git a/R/prep_scatter_animated.R b/R/prep_scatter_animated.R index f917e2a1..2e2238d1 100644 --- a/R/prep_scatter_animated.R +++ b/R/prep_scatter_animated.R @@ -66,8 +66,8 @@ prep_scatter_animated <- function(data_bopo, data_scatter <- data_bopo %>% dplyr::bind_rows(data_net) %>% dplyr::filter( - .data$sector == .env$sector, - .data$region == .env$region, + .data[["sector"]] == .env[["sector"]], + .data[["region"]] == .env[["region"]], !!rlang::sym(group_var) %in% groups_to_plot ) %>% dplyr::select("name" = name_col, "direction", "year", "value" = value_col) %>% @@ -75,16 +75,16 @@ prep_scatter_animated <- function(data_bopo, tidyr::pivot_wider(names_from = "direction", values_from = "value") %>% dplyr::mutate( datapoint = dplyr::case_when( - grepl(".*[Bb]enchmark,*", .data$name) ~ "Benchmark", - TRUE & data_level == "group_var" ~ "Group", - TRUE & data_level == "company" ~ "Company", + grepl(".*[Bb]enchmark,*", .data[["name"]]) ~ "Benchmark", + TRUE & .env[["data_level"]] == "group_var" ~ "Group", + TRUE & .env[["data_level"]] == "company" ~ "Company", TRUE ~ "Portfolio" ) ) %>% dplyr::mutate( - datapoint = factor(.data$datapoint, levels = c("Group", "Company", "Portfolio", "Benchmark")) + datapoint = factor(.data[["datapoint"]], levels = c("Group", "Company", "Portfolio", "Benchmark")) ) %>% - dplyr::arrange(.data$datapoint) + dplyr::arrange(.data[["datapoint"]]) data_scatter } diff --git a/R/prepare_abcd.R b/R/prepare_abcd.R index e2989de9..b14ddf6b 100644 --- a/R/prepare_abcd.R +++ b/R/prepare_abcd.R @@ -45,10 +45,10 @@ prepare_abcd <- function(config) { time_frame) { comp_sec_no_prod_t5 <- data %>% dplyr::filter( - .data[["year"]] %in% c(.env$start_year, .env$start_year + .env$time_frame) + .data[["year"]] %in% c(.env[["start_year"]], .env[["start_year"]] + .env[["time_frame"]]) ) %>% dplyr::summarise( - sum_production = sum(.data$production, na.rm = TRUE), + sum_production = sum(.data[["production"]], na.rm = TRUE), .by = c("name_company", "sector", "year") ) %>% tidyr::pivot_wider( @@ -61,24 +61,24 @@ prepare_abcd <- function(config) { .data[[paste0("prod_", start_year + time_frame)]] == 0 ) %>% dplyr::distinct( - .data$name_company, - .data$sector + .data[["name_company"]], + .data[["sector"]] ) comp_sec_no_prod_t0_to_t5 <- data %>% dplyr::filter( - .data[["year"]] %in% c(.env$start_year, .env$start_year + .env$time_frame) + .data[["year"]] %in% c(.env[["start_year"]], .env[["start_year"]] + .env[["time_frame"]]) ) %>% dplyr::summarise( - sum_production = sum(.data$production, na.rm = TRUE), + sum_production = sum(.data[["production"]], na.rm = TRUE), .by = c("name_company", "sector") ) %>% dplyr::filter( - .data$sum_production == 0 + .data[["sum_production"]] == 0 ) %>% dplyr::distinct( - .data$name_company, - .data$sector + .data[["name_company"]], + .data[["sector"]] ) data <- data %>% diff --git a/R/prepare_sector_split.R b/R/prepare_sector_split.R index dd238f69..67580279 100644 --- a/R/prepare_sector_split.R +++ b/R/prepare_sector_split.R @@ -55,7 +55,7 @@ prepare_sector_split <- function(config) { col_types = readr::cols_only(company_id = "d"), col_select = "company_id" ) %>% - dplyr::pull(.data$company_id) + dplyr::pull(.data[["company_id"]]) # optional: remove inactive companies if (remove_inactive_companies) { @@ -72,7 +72,7 @@ prepare_sector_split <- function(config) { dplyr::rename_with(.fn = ~ gsub(" ", "_", tolower(.x))) %>% # to compare primary energy units, we need power generation, not power capacity dplyr::filter( - (.data$asset_sector == "Power" & .data$activity_unit == "MWh") | .data$asset_sector != "Power" + (.data[["asset_sector"]] == "Power" & .data[["activity_unit"]] == "MWh") | .data[["asset_sector"]] != "Power" ) %>% dplyr::select( -dplyr::all_of( @@ -90,28 +90,28 @@ prepare_sector_split <- function(config) { values_to = "value", values_ptypes = list("value" = numeric()) ) %>% - dplyr::mutate(year = as.numeric(.data$year)) %>% + dplyr::mutate(year = as.numeric(.data[["year"]])) %>% dplyr::mutate( - sector = tolower(.data$sector), + sector = tolower(.data[["sector"]]), sector = dplyr::case_when( - .data$sector == "oil&gas" ~ "oil and gas", - .data$sector == "ldv" ~ "automotive", - TRUE ~ .data$sector + .data[["sector"]] == "oil&gas" ~ "oil and gas", + .data[["sector"]] == "ldv" ~ "automotive", + TRUE ~ .data[["sector"]] ), technology = dplyr::case_when( - .data$sector == "coal" ~ "coal", - .data$sector == "oil and gas" & grepl("Gas", .data$technology) ~ "gas", - .data$sector == "oil and gas" & grepl("Oil", .data$technology) ~ "oil", - .data$sector == "power" ~ tolower(.data$technology), - TRUE ~ tolower(.data$technology) + .data[["sector"]] == "coal" ~ "coal", + .data[["sector"]] == "oil and gas" & grepl("Gas", .data[["technology"]]) ~ "gas", + .data[["sector"]] == "oil and gas" & grepl("Oil", .data[["technology"]]) ~ "oil", + .data[["sector"]] == "power" ~ tolower(.data[["technology"]]), + TRUE ~ tolower(.data[["technology"]]) ) ) %>% dplyr::filter( - !.data$sector %in% c("hdv", "shipping"), - !.data$activity_unit == "tkm" + !.data[["sector"]] %in% c("hdv", "shipping"), + !.data[["activity_unit"]] == "tkm" ) %>% dplyr::summarise( - value = sum(.data$value, na.rm = TRUE), + value = sum(.data[["value"]], na.rm = TRUE), .by = c( "company_id", "company_name", @@ -127,7 +127,7 @@ prepare_sector_split <- function(config) { production_unit = "activity_unit" ) %>% # we calculate the sector split based on the primary energy mix of the start year - dplyr::filter(.data$year == .env$start_year) + dplyr::filter(.data[["year"]] == .env[["start_year"]]) # optional: remove inactive companies if (remove_inactive_companies) { @@ -140,46 +140,46 @@ prepare_sector_split <- function(config) { n_sectors_by_company <- advanced_company_indicators %>% dplyr::mutate( energy_sector = dplyr::if_else( - .data$sector %in% c("coal", "oil and gas", "power"), TRUE, FALSE + .data[["sector"]] %in% c("coal", "oil and gas", "power"), TRUE, FALSE ) ) %>% dplyr::distinct( - .data$company_id, - .data$sector, - .data$energy_sector + .data[["company_id"]], + .data[["sector"]], + .data[["energy_sector"]] ) %>% dplyr::mutate( n_sectors = dplyr::n(), .by = "company_id" ) %>% dplyr::summarise( - n_energy_sectors = sum(.data$energy_sector, na.rm = TRUE), + n_energy_sectors = sum(.data[["energy_sector"]], na.rm = TRUE), .by = c("company_id", "n_sectors") ) ### identify companies active in more than one energy sector---- companies_in_multiple_energy_sectors <- n_sectors_by_company %>% - dplyr::filter(.data$n_energy_sectors > 1) %>% - dplyr::pull(.data$company_id) + dplyr::filter(.data[["n_energy_sectors"]] > 1) %>% + dplyr::pull(.data[["company_id"]]) ## calculate equal weights sector split for all sectors---- # for each company add sector split by number of sectors the company operates in equally sector_split_all_companies <- advanced_company_indicators %>% dplyr::filter( - .data$year == .env$start_year + .data[["year"]] == .env[["start_year"]] ) %>% dplyr::inner_join( n_sectors_by_company, by = "company_id" ) %>% dplyr::mutate( - sector_split = 1 / .data$n_sectors + sector_split = 1 / .data[["n_sectors"]] ) %>% dplyr::summarise( - production = sum(.data$production, na.rm = TRUE), - n_sectors = max(.data$n_sectors, na.rm = TRUE), - n_energy_sectors = max(.data$n_energy_sectors, na.rm = TRUE), - sector_split = max(.data$sector_split, na.rm = TRUE), + production = sum(.data[["production"]], na.rm = TRUE), + n_sectors = max(.data[["n_sectors"]], na.rm = TRUE), + n_energy_sectors = max(.data[["n_energy_sectors"]], na.rm = TRUE), + sector_split = max(.data[["sector_split"]], na.rm = TRUE), .by = c("company_id", "name_company", "sector", "year", "production_unit") ) @@ -190,29 +190,29 @@ prepare_sector_split <- function(config) { # keep only companies that are active in multiple energy sectors sector_split_multi_energy_companies <- advanced_company_indicators %>% dplyr::filter( - .data$company_id %in% .env$companies_in_multiple_energy_sectors, - .data$sector %in% c("coal", "oil and gas", "power"), - .data$year == .env$start_year + .data[["company_id"]] %in% .env[["companies_in_multiple_energy_sectors"]], + .data[["sector"]] %in% c("coal", "oil and gas", "power"), + .data[["year"]] == .env[["start_year"]] ) # adjust power generation by primary energy efficiency sector_split_multi_energy_companies_power <- sector_split_multi_energy_companies %>% - dplyr::filter(.data$sector == "power") %>% + dplyr::filter(.data[["sector"]] == "power") %>% dplyr::inner_join( primary_energy_efficiency, by = c("sector", "technology") ) %>% dplyr::mutate( - production = .data$production / .data$primary_energy_efficiency_factor + production = .data[["production"]] / .data[["primary_energy_efficiency_factor"]] ) %>% dplyr::select(-"primary_energy_efficiency_factor") # transform all energy sectors to common unit of energy: mtoe sector_split_multi_energy_companies <- sector_split_multi_energy_companies %>% - dplyr::filter(.data$sector != "power") %>% + dplyr::filter(.data[["sector"]] != "power") %>% dplyr::bind_rows(sector_split_multi_energy_companies_power) %>% dplyr::summarise( - production = sum(.data$production, na.rm = TRUE), + production = sum(.data[["production"]], na.rm = TRUE), .by = c("company_id", "name_company", "sector", "year", "production_unit") ) %>% dplyr::inner_join( @@ -220,7 +220,7 @@ prepare_sector_split <- function(config) { by = c("sector", "production_unit" = "unit") ) %>% dplyr::mutate( - production = .data$production * .data$value_in_mtoe, + production = .data[["production"]] * .data[["value_in_mtoe"]], production_unit = "mtoe" ) %>% dplyr::select(-"value_in_mtoe") @@ -228,7 +228,7 @@ prepare_sector_split <- function(config) { # get the sector split for each multi energy sector company based on common energy units sector_split_multi_energy_companies <- sector_split_multi_energy_companies %>% dplyr::mutate( - sector_split = .data$production / sum(.data$production, na.rm = TRUE), + sector_split = .data[["production"]] / sum(.data[["production"]], na.rm = TRUE), .by = c( "company_id", "name_company", @@ -254,7 +254,7 @@ prepare_sector_split <- function(config) { # keep only companies that are provided in input company list sector_split_multi_energy_companies <- sector_split_multi_energy_companies %>% - dplyr::filter(.data$company_id %in% company_ids_primary_energy_split) + dplyr::filter(.data[["company_id"]] %in% .env[["company_ids_primary_energy_split"]]) ### check that the sum of the primary energy based sector split of each company is 1---- stop_if_sector_split_not_one(sector_split_multi_energy_companies) @@ -271,11 +271,11 @@ prepare_sector_split <- function(config) { suffix = c("_all", "_energy") ) %>% dplyr::mutate( - sector_split_energy_scaled = (.data$n_energy_sectors / .data$n_sectors) * .data$sector_split_energy, + sector_split_energy_scaled = (.data[["n_energy_sectors"]] / .data[["n_sectors"]]) * .data[["sector_split_energy"]], sector_split = dplyr::if_else( - is.na(.data$sector_split_energy), - .data$sector_split_all, - .data$sector_split_energy_scaled + is.na(.data[["sector_split_energy"]]), + .data[["sector_split_all"]], + .data[["sector_split_energy_scaled"]] ) ) %>% dplyr::rename( diff --git a/R/read_.R b/R/read_.R index 7e1a885c..1f1f9b9b 100644 --- a/R/read_.R +++ b/R/read_.R @@ -20,5 +20,5 @@ read_abcd_raw <- function(path, sheet) { ) ) - dplyr::mutate(abcd, year = as.integer(.data$year)) + dplyr::mutate(abcd, year = as.integer(.data[["year"]])) } diff --git a/R/run_aggregate_alignment_metric.R b/R/run_aggregate_alignment_metric.R index 3d65f86d..f674ed94 100644 --- a/R/run_aggregate_alignment_metric.R +++ b/R/run_aggregate_alignment_metric.R @@ -32,8 +32,8 @@ run_aggregate_alignment_metric <- function(config) { # load input data---- region_isos_select <- r2dii.data::region_isos %>% dplyr::filter( - .data$source == .env$scenario_source_input, - .data$region %in% .env$region_select + .data[["source"]] == .env[["scenario_source_input"]], + .data[["region"]] %in% .env[["region_select"]] ) scenario_input_tms <- readr::read_csv( @@ -74,27 +74,27 @@ run_aggregate_alignment_metric <- function(config) { increasing_or_decreasing_aggregate_alignment <- r2dii.data::increasing_or_decreasing %>% dplyr::mutate( increasing_or_decreasing = dplyr::if_else( - .data$technology %in% c("hydrocap", "nuclearcap"), + .data[["technology"]] %in% c("hydrocap", "nuclearcap"), "decreasing", - .data$increasing_or_decreasing + .data[["increasing_or_decreasing"]] ) ) # define if technologies should be treated as build out or phase down in the # aggregation technology_direction <- scenario_input_tms %>% - dplyr::filter(.data$year %in% c(.env$start_year, .env$start_year + .env$time_frame)) %>% + dplyr::filter(.data[["year"]] %in% c(.env[["start_year"]], .env[["start_year"]] + .env[["time_frame"]])) %>% dplyr::distinct( - .data$scenario_source, - .data$scenario, - .data$sector, - .data$technology, - .data$region + .data[["scenario_source"]], + .data[["scenario"]], + .data[["sector"]], + .data[["technology"]], + .data[["region"]] ) %>% dplyr::inner_join(r2dii.data::increasing_or_decreasing, by = c("sector", "technology")) %>% dplyr::mutate( directional_dummy = dplyr::if_else( - .data$increasing_or_decreasing == "increasing", + .data[["increasing_or_decreasing"]] == "increasing", 1, -1 ) @@ -172,7 +172,7 @@ run_aggregate_alignment_metric <- function(config) { ) sda_result_for_aggregation <- sda_result_for_aggregation %>% - dplyr::filter(.data$year >= .env$start_year) + dplyr::filter(.data[["year"]] >= .env[["start_year"]]) ## aggregate SDA P4B results to company level alignment metric---- company_alignment_net_sda <- sda_result_for_aggregation %>% @@ -205,13 +205,13 @@ run_aggregate_alignment_metric <- function(config) { by_group <- glue::glue("_by_{paste(.by, collapse = \"_\")}") } - data$company %>% + data[["company"]] %>% readr::write_csv( file = file.path(output_dir, glue::glue("company_exposure_{level}_aggregate_alignment{by_group}.csv")), na = "" ) - data$aggregate %>% + data[["aggregate"]] %>% readr::write_csv( file = file.path(output_dir, glue::glue("loanbook_exposure_{level}_aggregate_alignment{by_group}.csv")), na = "" diff --git a/R/run_calculate_loanbook_coverage.R b/R/run_calculate_loanbook_coverage.R index c38f2a82..37675eeb 100644 --- a/R/run_calculate_loanbook_coverage.R +++ b/R/run_calculate_loanbook_coverage.R @@ -35,7 +35,7 @@ run_calculate_loanbook_coverage <- function(config) { abcd["production"][is.na(abcd["production"])] <- 0 # filter to start year as we calculate coverage in start year - abcd <- dplyr::filter(abcd, .data$year == .env$start_year) + abcd <- dplyr::filter(abcd, .data[["year"]] == .env[["start_year"]]) ## read matched prioritized loan books---- list_matched_prioritized <- list.files(path = dir_matched, pattern = "^matched_prio_.*csv$") @@ -51,18 +51,17 @@ run_calculate_loanbook_coverage <- function(config) { # TODO: decide if this should be removed from outputs if (is.null(by_group)) { by_group <- "meta" - matched_prioritized <- matched_prioritized %>% - dplyr::mutate(meta = "meta") + matched_prioritized <- dplyr::mutate(.data = matched_prioritized, meta = "meta") } matched_companies <- matched_prioritized %>% dplyr::distinct( .data[[by_group]], - .data$name_abcd, - .data$sector_abcd, - .data$loan_size_outstanding, - .data$loan_size_outstanding_currency, - .data$score + .data[["name_abcd"]], + .data[["sector_abcd"]], + .data[["loan_size_outstanding"]], + .data[["loan_size_outstanding_currency"]], + .data[["score"]] ) ## get required countries for region_select---- @@ -70,33 +69,33 @@ run_calculate_loanbook_coverage <- function(config) { region_isos_select <- region_isos_complete %>% dplyr::filter( - .data$source == .env$scenario_source_input + .data[["source"]] == .env[["scenario_source_input"]] ) # create summary of loan book coverage---- # coverage of production by companies in loan books compared to total production # calculate summary stats for each available region - available_regions <- unique(region_isos_select$region) + available_regions <- unique(region_isos_select[["region"]]) production_coverage_summary <- NULL for (region_i in available_regions) { countries_select_i <- region_isos_select %>% - dplyr::filter(.data$region == .env$region_i) %>% - dplyr::pull(.data$isos) %>% + dplyr::filter(.data[["region"]] == .env[["region_i"]]) %>% + dplyr::pull(.data[["isos"]]) %>% toupper() # get total production and average emission intensity for each relevant region for all companies production_coverage_summary_i <- abcd %>% - dplyr::filter(.data$plant_location %in% .env$countries_select_i) %>% + dplyr::filter(.data[["plant_location"]] %in% .env[["countries_select_i"]]) %>% dplyr::summarise( emission_factor = stats::weighted.mean( - x = .data$emission_factor, - w = .data$production, + x = .data[["emission_factor"]], + w = .data[["production"]], na.rm = TRUE ), - production = sum(.data$production, na.rm = TRUE), + production = sum(.data[["production"]], na.rm = TRUE), .by = c( "company_id", "name_company", @@ -124,11 +123,11 @@ run_calculate_loanbook_coverage <- function(config) { # calculate summary statistics production_coverage_summary_i <- production_coverage_summary_i %>% dplyr::mutate( - financed_production = dplyr::if_else(.data$score == 1, .data$production, 0), - matched_company = dplyr::if_else(.data$score == 1, .data$name_company, NA_character_) + financed_production = dplyr::if_else(.data[["score"]] == 1, .data[["production"]], 0), + matched_company = dplyr::if_else(.data[["score"]] == 1, .data[["name_company"]], NA_character_) ) %>% dplyr::mutate( - matched_rows_company_sector = sum(.data$score, na.rm = TRUE), + matched_rows_company_sector = sum(.data[["score"]], na.rm = TRUE), .by = dplyr::all_of( c( by_group, @@ -138,14 +137,14 @@ run_calculate_loanbook_coverage <- function(config) { ) ) %>% dplyr::mutate( - n_companies_total = dplyr::n_distinct(.data$name_company, na.rm = TRUE), - production_total = sum(.data$production, na.rm = TRUE), + n_companies_total = dplyr::n_distinct(.data[["name_company"]], na.rm = TRUE), + production_total = sum(.data[["production"]], na.rm = TRUE), .by = c("sector") ) %>% dplyr::summarise( - total_exposure = sum(.data$loan_size_outstanding / .data$matched_rows_company_sector, na.rm = TRUE), - n_companies_matched = dplyr::n_distinct(.data$matched_company, na.rm = TRUE), - production_financed = sum(.data$financed_production, na.rm = TRUE), + total_exposure = sum(.data[["loan_size_outstanding"]] / .data[["matched_rows_company_sector"]], na.rm = TRUE), + n_companies_matched = dplyr::n_distinct(.data[["matched_company"]], na.rm = TRUE), + production_financed = sum(.data[["financed_production"]], na.rm = TRUE), .by = dplyr::all_of( c( by_group, @@ -156,9 +155,9 @@ run_calculate_loanbook_coverage <- function(config) { ) ) %>% dplyr::mutate( - share_companies_matched = .data$n_companies_matched / .data$n_companies_total, - share_production_financed = .data$production_financed / .data$production_total, - region = .env$region_i + share_companies_matched = .data[["n_companies_matched"]] / .data[["n_companies_total"]], + share_production_financed = .data[["production_financed"]] / .data[["production_total"]], + region = .env[["region_i"]] ) # remove entries that were not matched to any loan book AFTER calculating diff --git a/R/run_calculate_match_success_rate.R b/R/run_calculate_match_success_rate.R index 4658e06e..d186b763 100644 --- a/R/run_calculate_match_success_rate.R +++ b/R/run_calculate_match_success_rate.R @@ -73,8 +73,8 @@ run_calculate_match_success_rate <- function(config) { raw_lbk <- raw_lbk %>% dplyr::mutate( - group_id = gsub(glue::glue("{dir_raw}/"), "", .data$group_id), - group_id = gsub(".csv", "", .data$group_id) + group_id = gsub(glue::glue("{dir_raw}/"), "", .data[["group_id"]]), + group_id = gsub(".csv", "", .data[["group_id"]]) ) ## load matched prioritized loan books---- @@ -105,7 +105,7 @@ run_calculate_match_success_rate <- function(config) { col_select = dplyr::all_of(col_select_sector_classification) ) } else { - sector_classifications_used <- unique(raw_lbk$sector_classification_system) + sector_classifications_used <- unique(raw_lbk[["sector_classification_system"]]) if (length(sector_classifications_used) != 1) { cli::cli_abort( @@ -118,7 +118,7 @@ run_calculate_match_success_rate <- function(config) { } sector_classification_system <- r2dii.data::sector_classifications %>% - dplyr::filter(.data$code_system == .env$sector_classifications_used) + dplyr::filter(.data[["code_system"]] == .env[["sector_classifications_used"]]) } ## remove misclassified loans---- @@ -159,7 +159,7 @@ run_calculate_match_success_rate <- function(config) { ) # plot match success rate---- - plot_match_success_currency <- unique(raw_lbk$loan_size_outstanding_currency) + plot_match_success_currency <- unique(raw_lbk[["loan_size_outstanding_currency"]]) ## plot relative match success rates for individual loan books---- plot_match_success_rate_rel_n_ind <- data_lbk_match_success_rate %>% diff --git a/R/run_match_prioritize.R b/R/run_match_prioritize.R index df621ac1..f5d6efdc 100644 --- a/R/run_match_prioritize.R +++ b/R/run_match_prioritize.R @@ -52,7 +52,7 @@ run_match_prioritize <- function(config) { file = file.path(dir_matched, list_matched_manual), col_types = col_types_matched_manual ) %>% - dplyr::group_split(.data$group_id) + dplyr::group_split(.data[["group_id"]]) ## optional: load sector split---- if (apply_sector_split & sector_split_type_select == "equal_weights") { @@ -69,12 +69,12 @@ run_match_prioritize <- function(config) { # prioritize and save files---- for (i in seq_along(matched_lbk_manual)) { - group_name <- unique(matched_lbk_manual[[i]]$group_id) + group_name <- unique(matched_lbk_manual[[i]][["group_id"]]) ## prioritize matched loan book---- matched_prio_i <- matched_lbk_manual[[i]] %>% r2dii.match::prioritize(priority = match_prio_priority) %>% - dplyr::mutate(group_id = .env$group_name) + dplyr::mutate(group_id = .env[["group_name"]]) # optional: apply sector split---- if (apply_sector_split & sector_split_type_select == "equal_weights") { @@ -88,7 +88,7 @@ run_match_prioritize <- function(config) { ## ensure that id_loan is unique across all loan books---- matched_prio_i <- matched_prio_i %>% dplyr::mutate( - id_loan = paste(.data$id_loan, .data$group_id, sep = "_") + id_loan = paste(.data[["id_loan"]], .data[["group_id"]], sep = "_") ) ## write matched prioritized loan book to file---- diff --git a/R/run_matching.R b/R/run_matching.R index 205aad7b..4740009e 100644 --- a/R/run_matching.R +++ b/R/run_matching.R @@ -85,8 +85,8 @@ run_matching <- function(config) { col_types = col_types_raw, id = "group_id" ) %>% - dplyr::mutate(group_id = tools::file_path_sans_ext(basename(.data$group_id))) %>% - dplyr::group_split(.data$group_id) + dplyr::mutate(group_id = tools::file_path_sans_ext(basename(.data[["group_id"]]))) %>% + dplyr::group_split(.data[["group_id"]]) # match and save loan books---- cli::cli_progress_bar( @@ -94,7 +94,7 @@ run_matching <- function(config) { format = "{cli::pb_spin} Matching loanbooks {cli::pb_current}/{cli::pb_total} | ETA: {cli::pb_eta}" ) for (i in seq_along(raw_lbk)) { - group_name <- unique(raw_lbk[[i]]$group_id) + group_name <- unique(raw_lbk[[i]][["group_id"]]) ## match data---- if (matching_use_own_sector_classification) { diff --git a/R/run_pacta.R b/R/run_pacta.R index 362d5924..2a491ad3 100644 --- a/R/run_pacta.R +++ b/R/run_pacta.R @@ -48,8 +48,8 @@ run_pacta <- function(config) { # load input data---- region_isos_select <- r2dii.data::region_isos %>% dplyr::filter( - .data$source == .env$scenario_source_input, - .data$region %in% .env$region_select + .data[["source"]] == .env[["scenario_source_input"]], + .data[["region"]] %in% .env[["region_select"]] ) scenario_input_tms <- readr::read_csv( @@ -109,7 +109,7 @@ run_pacta <- function(config) { co2_intensity_scenario = scenario_input_sda, region_isos = region_isos_select ) %>% - dplyr::mutate("{by_group}" := .env$i) + dplyr::mutate("{by_group}" := .env[["i"]]) results_sda_total <- results_sda_total %>% dplyr::bind_rows(results_sda_i) @@ -139,7 +139,7 @@ run_pacta <- function(config) { scenario = scenario_input_tms, region_isos = region_isos_select ) %>% - dplyr::mutate("{by_group}" := .env$i) + dplyr::mutate("{by_group}" := .env[["i"]]) results_tms_total <- results_tms_total %>% dplyr::bind_rows(results_tms_i) @@ -168,7 +168,7 @@ run_pacta <- function(config) { dplyr::filter( .data[[by_group]] == .env[["tms_i"]], .data[["scenario_source"]] == .env[["scenario_source_input"]], - grepl(.env[["scenario_select"]], .data$metric), + grepl(.env[["scenario_select"]], .data[["metric"]]), .data[["region"]] == .env[["region_select"]], .data[["sector"]] == .env[["sector_select"]] ) %>% @@ -199,7 +199,7 @@ run_pacta <- function(config) { dplyr::filter( .data[[by_group]] == .env[["tms_i"]], .data[["scenario_source"]] == .env[["scenario_source_input"]], - grepl(.env[["scenario_select"]], .data$metric), + grepl(.env[["scenario_select"]], .data[["metric"]]), .data[["region"]] == .env[["region_select"]], .data[["sector"]] == .env[["sector_select"]] ) %>% @@ -230,7 +230,7 @@ run_pacta <- function(config) { dplyr::filter( .data[[by_group]] == .env[["tms_i"]], .data[["scenario_source"]] == .env[["scenario_source_input"]], - grepl(.env[["scenario_select"]], .data$metric), + grepl(.env[["scenario_select"]], .data[["metric"]]), .data[["region"]] == .env[["region_select"]], .data[["sector"]] == .env[["sector_select"]] ) %>% @@ -261,7 +261,7 @@ run_pacta <- function(config) { dplyr::filter( .data[[by_group]] == .env[["tms_i"]], .data[["scenario_source"]] == .env[["scenario_source_input"]], - grepl(.env[["scenario_select"]], .data$metric), + grepl(.env[["scenario_select"]], .data[["metric"]]), .data[["region"]] == .env[["region_select"]], .data[["sector"]] == .env[["sector_select"]] ) %>% @@ -293,7 +293,7 @@ run_pacta <- function(config) { dplyr::filter( .data[[by_group]] == .env[["sda_i"]], .data[["scenario_source"]] == .env[["scenario_source_input"]], - grepl(.env[["scenario_select"]], .data$emission_factor_metric), + grepl(.env[["scenario_select"]], .data[["emission_factor_metric"]]), .data[["region"]] == .env[["region_select"]], .data[["sector"]] == .env[["sector_select"]] ) %>% @@ -324,7 +324,7 @@ run_pacta <- function(config) { dplyr::filter( .data[[by_group]] == .env[["sda_i"]], .data[["scenario_source"]] == .env[["scenario_source_input"]], - grepl(.env[["scenario_select"]], .data$emission_factor_metric), + grepl(.env[["scenario_select"]], .data[["emission_factor_metric"]]), .data[["region"]] == .env[["region_select"]], .data[["sector"]] == .env[["sector_select"]] ) %>% @@ -355,7 +355,7 @@ run_pacta <- function(config) { dplyr::filter( .data[[by_group]] == .env[["sda_i"]], .data[["scenario_source"]] == .env[["scenario_source_input"]], - grepl(.env[["scenario_select"]], .data$emission_factor_metric), + grepl(.env[["scenario_select"]], .data[["emission_factor_metric"]]), .data[["region"]] == .env[["region_select"]], .data[["sector"]] == .env[["sector_select"]] ) %>% diff --git a/tests/testthat/test-aggregate_alignment_loanbook_exposure.R b/tests/testthat/test-aggregate_alignment_loanbook_exposure.R index 9eecdd50..c4501f6b 100644 --- a/tests/testthat/test-aggregate_alignment_loanbook_exposure.R +++ b/tests/testthat/test-aggregate_alignment_loanbook_exposure.R @@ -75,12 +75,12 @@ test_that("number of identified companies equals unique list of companies in inp test_that("number of identified companies per direction equals unique list of companies in input data", { n_companies_input_bopo <- test_data_aggregate_alignment_loanbook_exposure_bopo %>% - dplyr::distinct(.data$name_abcd) %>% + dplyr::distinct(.data[["name_abcd"]]) %>% nrow() n_output_buildout <- test_output_aggregate_alignment_loanbook_exposure_bopo$aggregate %>% - dplyr::filter(.data$direction == "buildout") %>% - dplyr::pull(.data$n_companies) + dplyr::filter(.data[["direction"]] == "buildout") %>% + dplyr::pull(.data[["n_companies"]]) expect_equal( n_output_buildout, @@ -88,8 +88,8 @@ test_that("number of identified companies per direction equals unique list of co ) n_output_phaseout <- test_output_aggregate_alignment_loanbook_exposure_bopo$aggregate %>% - dplyr::filter(.data$direction == "phaseout") %>% - dplyr::pull(.data$n_companies) + dplyr::filter(.data[["direction"]] == "phaseout") %>% + dplyr::pull(.data[["n_companies"]]) expect_equal( n_output_phaseout, @@ -177,7 +177,7 @@ test_that("net aggregate results with .by specified returns results for each gro }) test_that("net aggregate results with multiple variables specified in .by returns results for each combination of groups", { - n_groups_2 <- nrow(dplyr::distinct(test_matched_dot_by, .data$foo, .data$bar)) + n_groups_2 <- nrow(dplyr::distinct(test_matched_dot_by, .data[["foo"]], .data[["bar"]])) test_output_with_dot_by_2 <- test_data_company_net %>% aggregate_alignment_loanbook_exposure( @@ -206,8 +206,8 @@ test_data_company_bopo <- tibble::tribble( # styler: on test_that("bopo aggregate results grouped by foo returns results for each available combination of buildout/phaseout and group foo", { - n_groups <- dplyr::distinct(test_matched_dot_by, .data$foo) - n_directions <- dplyr::distinct(test_data_company_bopo, .data$direction) + n_groups <- dplyr::distinct(test_matched_dot_by, .data[["foo"]]) + n_directions <- dplyr::distinct(test_data_company_bopo, .data[["direction"]]) test_output_with_dot_by <- test_data_company_bopo %>% aggregate_alignment_loanbook_exposure( diff --git a/tests/testthat/test-calculate_company_alignment_metric.R b/tests/testthat/test-calculate_company_alignment_metric.R index 954ae2e4..fa82987f 100644 --- a/tests/testthat/test-calculate_company_alignment_metric.R +++ b/tests/testthat/test-calculate_company_alignment_metric.R @@ -295,12 +295,12 @@ test_that("calculate_company_aggregate_alignment_tms returns expected directions # number of units analysed n_groups_net <- test_data_calculate_company_aggregate_alignment_tms %>% dplyr::distinct( - .data$name_abcd, - .data$scenario_source, - .data$region, - .data$year, - .data$sector, - .data$activity_unit + .data[["name_abcd"]], + .data[["scenario_source"]], + .data[["region"]], + .data[["year"]], + .data[["sector"]], + .data[["activity_unit"]] ) %>% nrow() @@ -447,12 +447,12 @@ test_output_fill_missing_direction <- fill_missing_direction( # number of units of analysis n_units <- test_data_fill_missing_direction %>% dplyr::distinct( - .data$name_abcd, - .data$scenario_source, - .data$region, - .data$year, - .data$sector, - .data$activity_unit + .data[["name_abcd"]], + .data[["scenario_source"]], + .data[["region"]], + .data[["year"]], + .data[["sector"]], + .data[["activity_unit"]] ) %>% nrow() @@ -492,11 +492,11 @@ expected_output_columns <- expected_output_columns[!expected_output_columns %in% expected_output_rows <- test_data_calculate_company_aggregate_alignment_sda %>% dplyr::distinct( - .data$sector, - .data$year, - .data$region, - .data$scenario_source, - .data$name_abcd + .data[["sector"]], + .data[["year"]], + .data[["region"]], + .data[["scenario_source"]], + .data[["name_abcd"]] ) %>% nrow() @@ -593,9 +593,9 @@ test_that("output columns replace emission_factor_* cols with projected and targ test_nrows <- nrow(test_output_prep_and_wrangle_aggregate_alignment_sda_1) expected_nrows <- test_data_prep_and_wrangle_aggregate_alignment_sda_1 %>% dplyr::distinct( - .data$scenario_source, - .data$name_abcd, - .data$year + .data[["scenario_source"]], + .data[["name_abcd"]], + .data[["year"]] ) %>% nrow() @@ -630,8 +630,8 @@ test_output_prep_and_wrangle_aggregate_alignment_sda_2 <- prep_and_wrangle_aggre test_output_years <- unique(test_output_prep_and_wrangle_aggregate_alignment_sda_2$year) expected_output_year <- test_data_prep_and_wrangle_aggregate_alignment_sda_2 %>% - dplyr::filter(dplyr::between(.data$year, test_start_year, test_start_year + test_time_frame_short)) %>% - dplyr::pull(.data$year) %>% + dplyr::filter(dplyr::between(.data[["year"]], test_start_year, test_start_year + test_time_frame_short)) %>% + dplyr::pull(.data[["year"]]) %>% unique() test_that("years outside of start_year and start_year + time_frame are dropped", { diff --git a/tests/testthat/test-run_calculate_match_success_rate.R b/tests/testthat/test-run_calculate_match_success_rate.R index 2e93c773..3344af6b 100644 --- a/tests/testthat/test-run_calculate_match_success_rate.R +++ b/tests/testthat/test-run_calculate_match_success_rate.R @@ -18,7 +18,7 @@ test_that("combine_raw_and_matched_loan_books identifies correct matched and unm ) nace_sectors <- r2dii.data::sector_classifications %>% - dplyr::filter(.data$code_system == "NACE") + dplyr::filter(.data[["code_system"]] == "NACE") test_raw_lbk_with_sectors <- add_sectors_to_raw_lbk( raw_lbk = test_raw, @@ -52,7 +52,7 @@ test_that("combine_raw_and_matched_loan_books removes group_id from id_loan wher dplyr::mutate(group_id = "test") nace_sectors <- r2dii.data::sector_classifications %>% - dplyr::filter(.data$code_system == "NACE") + dplyr::filter(.data[["code_system"]] == "NACE") test_raw_lbk_with_sectors <- add_sectors_to_raw_lbk( raw_lbk = test_raw, @@ -82,7 +82,7 @@ test_that("combine_raw_and_matched_loan_books removes sector_abcd from id_loan w dplyr::mutate(group_id = "test") nace_sectors <- r2dii.data::sector_classifications %>% - dplyr::filter(.data$code_system == "NACE") + dplyr::filter(.data[["code_system"]] == "NACE") test_raw_lbk_with_sectors <- add_sectors_to_raw_lbk( raw_lbk = test_raw, @@ -140,7 +140,7 @@ test_that("calculate_match_success_rate returns results for each group of variab dplyr::filter(.data[["level"]] == "direct_loantaker") nace_sectors <- r2dii.data::sector_classifications %>% - dplyr::filter(.data$code_system == "NACE") + dplyr::filter(.data[["code_system"]] == "NACE") lbk_match_success_rate_meta <- calculate_match_success_rate( raw_lbk = test_raw_meta, @@ -197,7 +197,7 @@ test_that("calculate_match_success_rate can only be calculated for by_groups tha dplyr::filter(.data[["level"]] == "direct_loantaker") nace_sectors <- r2dii.data::sector_classifications %>% - dplyr::filter(.data$code_system == "NACE") + dplyr::filter(.data[["code_system"]] == "NACE") testthat::expect_no_condition( calculate_match_success_rate(