diff --git a/R/map_chunks.R b/R/map_chunks.R index d26f721..a8ab864 100644 --- a/R/map_chunks.R +++ b/R/map_chunks.R @@ -3,7 +3,8 @@ map_chunks <- function(data, .by, chunks, cache_dir, - order = "identity") { + order = "identity", + options = NULL) { parent <- rm_namespace(deparse(substitute(.f))) job <- data |> nest_chunk(.by = .by, chunks = chunks) |> @@ -14,7 +15,12 @@ map_chunks <- function(data, dchunkr::order_rows(.fun = order) |> select("data", "file") |> future_pwalk( - \(data, file) .f(data, ...) |> write_rds(file), + function(data, file) { + # Pass options https://github.com/HenrikBengtsson/future/issues/134 + withr::local_options(options) + .f(data, ...) |> + write_rds(file) + }, .progress = TRUE, .options = furrr::furrr_options(seed = TRUE) ) diff --git a/R/profile.R b/R/profile.R index 2cbeb48..8c9cc5d 100644 --- a/R/profile.R +++ b/R/profile.R @@ -37,10 +37,11 @@ profile_emissions <- function(companies, } else { map_chunks( companies, + .by = "companies_id", chunks = handle_chunks(companies), cache_dir = cache_dir, order = order, - .by = "companies_id", + options = tiltIndicatorAfter_options(), .f = tiltIndicatorAfter::profile_emissions, co2 = co2, europages_companies = europages_companies, @@ -98,6 +99,7 @@ profile_emissions_upstream <- function(companies, chunks = handle_chunks(companies), cache_dir = cache_dir, order = order, + options = tiltIndicatorAfter_options(), .f = tiltIndicatorAfter::profile_emissions_upstream, co2 = co2, europages_companies = europages_companies, @@ -154,6 +156,7 @@ profile_sector <- function(companies, chunks = handle_chunks(companies), cache_dir = cache_dir, order = order, + options = tiltIndicatorAfter_options(), .f = tiltIndicatorAfter::profile_sector, scenarios = scenarios, europages_companies = europages_companies, @@ -213,6 +216,7 @@ profile_sector_upstream <- function(companies, chunks = handle_chunks(companies), cache_dir = cache_dir, order = order, + options = tiltIndicatorAfter_options(), .f = tiltIndicatorAfter::profile_sector_upstream, scenarios = scenarios, inputs = inputs, diff --git a/R/utils.R b/R/utils.R new file mode 100644 index 0000000..3ba7763 --- /dev/null +++ b/R/utils.R @@ -0,0 +1,7 @@ +extract_options <- function(pattern) { + options()[grep(pattern, names(options()), value = TRUE)] +} + +tiltIndicatorAfter_options <- function() { + extract_options("tiltIndicatorAfter") +} diff --git a/tests/testthat/test-profile.R b/tests/testthat/test-profile.R index 14d3ce6..e39cea7 100644 --- a/tests/testthat/test-profile.R +++ b/tests/testthat/test-profile.R @@ -259,3 +259,31 @@ test_that("with `order = 'rev'` the chunks work in reverse order", { expect_equal(actual, rev(as.character(1:3))) }) + +test_that("can optionally output `co2_*` columns", { + tmp_cache <- local_tempfile() + local_options(list( + tiltIndicatorAfter.output_co2_footprint = TRUE, + tiltWorkflows.chunks = 3, + tiltWorkflows.cache_dir = tmp_cache + )) + + companies <- read_csv(toy_emissions_profile_any_companies()) + co2 <- read_csv(toy_emissions_profile_products_ecoinvent()) + europages_companies <- read_csv(toy_europages_companies()) + ecoinvent_activities <- read_csv(toy_ecoinvent_activities()) + ecoinvent_europages <- read_csv(toy_ecoinvent_europages()) + isic_name <- read_csv(toy_isic_name()) + + out <- tiltWorkflows::profile_emissions( + companies, + co2, + europages_companies, + ecoinvent_activities, + ecoinvent_europages, + isic_name + ) + + expect_true(hasName(unnest_product(out), "co2_footprint")) + expect_true(hasName(unnest_company(out), "co2_avg")) +})