Skip to contents

This workflow runs in chunks of the *companies data and caches intermediate results. This saves memory, completes faster, and allows you to resume after interruptions.

Setup

library(dplyr, warn.conflicts = FALSE)
library(readr, warn.conflicts = FALSE)
library(rappdirs)
library(future)
library(fs)

# Masking `tiltIndicatorAfter::profile*()` to use `chunks`
library(tiltWorkflows)
#> Loading required package: tiltIndicatorAfter
#> Loading required package: tiltToyData
#> 
#> Attaching package: 'tiltWorkflows'
#> The following objects are masked from 'package:tiltIndicatorAfter':
#> 
#>     profile_emissions, profile_emissions_upstream, profile_sector,
#>     profile_sector_upstream

If the parameter chunks is NULL (default) your *companies dataset is automatically chunked to distribute its companies across available cores. This uses your computer resources efficiently but may not be enough. Consider adjusting the chunks parameter manually. Aim to balance memory-usage and speed. A small number of chunks makes each chunk bigger and may overwhelm your memory. A large number of chunks may take longer because of the overhead of caching each chunk.

Parameters
params
#> $chunks
#> [1] ""
#> 
#> $order
#> [1] "sample"
#> 
#> $cache_dir
#> [1] ""
#> 
#> $input
#> [1] "input"
#> 
#> $output
#> [1] "output"
#> 
#> $europages_companies
#> [1] "europages_companies.csv"
#> 
#> $ecoinvent_activities
#> [1] "ecoinvent_activities.csv"
#> 
#> $ecoinvent_europages
#> [1] "ecoinvent_europages.csv"
#> 
#> $isic
#> [1] "isic.csv"
#> 
#> $sector_profile_companies
#> [1] "sector_profile_companies.csv"
#> 
#> $sector_profile_any_scenarios
#> [1] "sector_profile_any_scenarios.csv"
options(
  # Determines the number of chunks
  tiltWorkflows.chunks = params$chunks,
  # Determines the order in which the chunks run
  tiltWorkflows.order = params$order,
  # Determines where to store the cache
  tiltWorkflows.cache_dir = params$cache_dir,
  # Read data quietly
  readr.show_col_types = FALSE,
  # Make printed output wider
  width = 500
)

# Enable computing over multiple workers in parallel
plan(multisession)

# Ensure input/ and output/ directories
if (!dir_exists(params$input)) use_toy_input()
if (!dir_exists(params$output)) dir_create(params$output)
Session information
getwd()
#> [1] "/home/runner/work/tiltWorkflows/tiltWorkflows/vignettes/articles"

availableCores()
#> system 
#>      4

dir_tree(params$input)
#> input
#> ├── ecoinvent_activities.csv
#> ├── ecoinvent_europages.csv
#> ├── ecoinvent_inputs.csv
#> ├── emissions_profile_any_companies.csv
#> ├── emissions_profile_products.csv
#> ├── emissions_profile_upstream_products.csv
#> ├── europages_companies.csv
#> ├── isic.csv
#> ├── sector_profile_any_scenarios.csv
#> ├── sector_profile_companies.csv
#> ├── sector_profile_upstream_companies.csv
#> └── sector_profile_upstream_products.csv

dir_tree(params$output)
#> output
#> ├── emissions_profile_at_company_level.csv
#> ├── emissions_profile_at_product_level.csv
#> ├── emissions_profile_upstream_at_company_level.csv
#> ├── emissions_profile_upstream_at_product_level.csv
#> ├── sector_profile_upstream_at_company_level.csv
#> └── sector_profile_upstream_at_product_level.csv

sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.4.1 (2024-06-14)
#>  os       Ubuntu 22.04.5 LTS
#>  system   x86_64, linux-gnu
#>  ui       X11
#>  language en
#>  collate  C.UTF-8
#>  ctype    C.UTF-8
#>  tz       UTC
#>  date     2024-10-09
#>  pandoc   3.1.11 @ /opt/hostedtoolcache/pandoc/3.1.11/x64/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
#>  package            * version    date (UTC) lib source
#>  bslib                0.8.0      2024-07-29 [1] RSPM
#>  cachem               1.1.0      2024-05-16 [1] RSPM
#>  cli                  3.6.3      2024-06-21 [1] RSPM
#>  codetools            0.2-20     2024-03-31 [3] CRAN (R 4.4.1)
#>  crayon               1.5.3      2024-06-20 [1] RSPM
#>  dchunkr              0.0.0.9001 2024-10-09 [1] Github (maurolepore/dchunkr@9748350)
#>  desc                 1.4.3      2023-12-10 [1] RSPM
#>  digest               0.6.37     2024-08-19 [1] RSPM
#>  dplyr              * 1.1.4      2023-11-17 [1] RSPM
#>  evaluate             1.0.0      2024-09-17 [1] RSPM
#>  fansi                1.0.6      2023-12-08 [1] RSPM
#>  fastmap              1.2.0      2024-05-15 [1] RSPM
#>  fs                 * 1.6.4      2024-04-25 [1] RSPM
#>  furrr                0.3.1      2022-08-15 [1] RSPM
#>  future             * 1.34.0     2024-07-29 [1] RSPM
#>  generics             0.1.3      2022-07-05 [1] RSPM
#>  globals              0.16.3     2024-03-08 [1] RSPM
#>  glue                 1.8.0      2024-09-30 [1] RSPM
#>  hms                  1.1.3      2023-03-21 [1] RSPM
#>  htmltools            0.5.8.1    2024-04-04 [1] RSPM
#>  htmlwidgets          1.6.4      2023-12-06 [1] RSPM
#>  httpuv               1.6.15     2024-03-26 [1] RSPM
#>  jquerylib            0.1.4      2021-04-26 [1] RSPM
#>  jsonlite             1.8.9      2024-09-20 [1] RSPM
#>  knitr                1.48       2024-07-07 [1] RSPM
#>  later                1.3.2      2023-12-06 [1] RSPM
#>  lifecycle            1.0.4      2023-11-07 [1] RSPM
#>  listenv              0.9.1      2024-01-29 [1] RSPM
#>  magrittr             2.0.3      2022-03-30 [1] RSPM
#>  memoise              2.0.1      2021-11-26 [1] RSPM
#>  mime                 0.12       2021-09-28 [1] RSPM
#>  parallelly           1.38.0     2024-07-27 [1] RSPM
#>  pillar               1.9.0      2023-03-22 [1] RSPM
#>  pkgconfig            2.0.3      2019-09-22 [1] RSPM
#>  pkgdown              2.1.1      2024-09-17 [1] RSPM
#>  promises             1.3.0      2024-04-05 [1] RSPM
#>  purrr                1.0.2      2023-08-10 [1] RSPM
#>  R6                   2.5.1      2021-08-19 [1] RSPM
#>  ragg                 1.3.3      2024-09-11 [1] RSPM
#>  rappdirs           * 0.3.3      2021-01-31 [1] RSPM
#>  Rcpp                 1.0.13     2024-07-17 [1] RSPM
#>  readr              * 2.1.5      2024-01-10 [1] RSPM
#>  rlang                1.1.4      2024-06-04 [1] RSPM
#>  rmarkdown            2.28       2024-08-17 [1] RSPM
#>  sass                 0.4.9      2024-03-15 [1] RSPM
#>  sessioninfo          1.2.2      2021-12-06 [1] RSPM
#>  shiny                1.9.1      2024-08-01 [1] RSPM
#>  stringi              1.8.4      2024-05-06 [1] RSPM
#>  stringr              1.5.1      2023-11-14 [1] RSPM
#>  systemfonts          1.1.0      2024-05-15 [1] RSPM
#>  textshaping          0.4.0      2024-05-24 [1] RSPM
#>  tibble               3.2.1      2023-03-20 [1] RSPM
#>  tidyr                1.3.1      2024-01-24 [1] RSPM
#>  tidyselect           1.2.1      2024-03-11 [1] RSPM
#>  tiltAddCO2           0.0.0.9002 2024-10-09 [1] Github (2DegreesInvesting/tiltAddCO2@dc7eacd)
#>  tiltIndicator        0.0.0.9228 2024-10-09 [1] Github (2DegreesInvesting/tiltIndicator@25abe9a)
#>  tiltIndicatorAfter * 0.0.0.9060 2024-10-09 [1] Github (2DegreesInvesting/tiltIndicatorAfter@e952dae)
#>  tiltPolish           0.0.0.9006 2024-10-09 [1] Github (2degreesinvesting/tiltPolish@d8bdff6)
#>  tiltToyData        * 0.0.0.9204 2024-10-09 [1] Github (2DegreesInvesting/tiltToyData@3a2417a)
#>  tiltTransitionRisk   0.0.0.9003 2024-10-09 [1] Github (2degreesinvesting/tiltTransitionRisk@ee013b8)
#>  tiltWorkflows      * 0.0.0.9033 2024-10-09 [1] local
#>  tzdb                 0.4.0      2023-05-12 [1] RSPM
#>  utf8                 1.2.4      2023-10-22 [1] RSPM
#>  vctrs                0.6.5      2023-12-01 [1] RSPM
#>  withr                3.0.1      2024-07-31 [1] RSPM
#>  xfun                 0.48       2024-10-03 [1] RSPM
#>  xtable               1.8-4      2019-04-21 [1] RSPM
#>  yaml                 2.3.10     2024-07-26 [1] RSPM
#> 
#>  [1] /home/runner/work/_temp/Library
#>  [2] /opt/R/4.4.1/lib/R/site-library
#>  [3] /opt/R/4.4.1/lib/R/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

Data

This example defaults to using toy datasets but you may use the parameters of this file to instead use your own data.

europages_companies <- read_csv(path(params$input, params$europages_companies))
ecoinvent_activities <- read_csv(path(params$input, params$ecoinvent_activities))
ecoinvent_europages <- read_csv(path(params$input, params$ecoinvent_europages))
isic <- read_csv(path(params$input, params$isic))

Data specific to this indicator.

sector_profile_companies <- read_csv(path(params$input, params$sector_profile_companies))
sector_profile_any_scenarios <- read_csv(path(params$input, params$sector_profile_any_scenarios))

Sector profile

For this TILT indicator, compute results both at product and company level.

sector_profile <- profile_sector(
  companies = sector_profile_companies,
  scenarios = sector_profile_any_scenarios,
  europages_companies = europages_companies,
  ecoinvent_activities = ecoinvent_activities,
  ecoinvent_europages = ecoinvent_europages,
  isic = isic
)
#> Warning: Splitting `companies` into 4 chunks.

Results

Overview and save results at each level.

sector_profile |>
  unnest_product() |>
  print() |>
  write_csv(path(params$output, "sector_profile_at_product_level.csv"))
#> # A tibble: 304 × 28
#>    companies_id           company_name country sector_profile reduction_targets scenario  year ep_product             matched_activity_name                                         matched_reference_product                          unit  tilt_sector  tilt_subsector           multi_match matching_certainty matching_certainty_c…¹ company_city postcode address main_activity activity_uuid_produc…² isic_4digit sector_scenario subsector_scenario min_headcount max_headcount ei_geography isic_4digit_name
#>    <chr>                  <chr>        <chr>   <chr>                      <dbl> <chr>    <dbl> <chr>                  <chr>                                                         <chr>                                              <chr> <chr>        <chr>                    <lgl>       <chr>              <chr>                  <chr>        <chr>    <chr>   <chr>         <chr>                  <chr>       <chr>           <chr>                      <dbl>         <dbl> <chr>        <chr>           
#>  1 antimonarchy_canine    NA           NA      medium                      0.18 1.5C RPS  2030 tent                   market for shed, large, wood, non-insulated, fire-unprotected shed, large, wood, non-insulated, fire-unprotected m2    construction construction residential NA          NA                 NA                     NA           NA       NA      NA            76269c17-78d6-420b-99… '4100'      buildings       NA                            NA            NA tilt_world   NA              
#>  2 antimonarchy_canine    NA           NA      high                        0.98 1.5C RPS  2050 tent                   market for shed, large, wood, non-insulated, fire-unprotected shed, large, wood, non-insulated, fire-unprotected m2    construction construction residential NA          NA                 NA                     NA           NA       NA      NA            76269c17-78d6-420b-99… '4100'      buildings       NA                            NA            NA tilt_world   NA              
#>  3 antimonarchy_canine    NA           NA      high                        0.4  NZ 2050   2030 tent                   market for shed, large, wood, non-insulated, fire-unprotected shed, large, wood, non-insulated, fire-unprotected m2    construction construction residential NA          NA                 NA                     NA           NA       NA      NA            76269c17-78d6-420b-99… '4100'      total           residential                   NA            NA tilt_world   NA              
#>  4 antimonarchy_canine    NA           NA      high                        0.97 NZ 2050   2050 tent                   market for shed, large, wood, non-insulated, fire-unprotected shed, large, wood, non-insulated, fire-unprotected m2    construction construction residential NA          NA                 NA                     NA           NA       NA      NA            76269c17-78d6-420b-99… '4100'      total           residential                   NA            NA tilt_world   NA              
#>  5 celestial_lovebird     NA           NA      medium                      0.18 1.5C RPS  2030 table hire for parties market for shed, large, wood, non-insulated, fire-unprotected shed, large, wood, non-insulated, fire-unprotected m2    construction construction residential NA          NA                 NA                     NA           NA       NA      NA            76269c17-78d6-420b-99… '4100'      buildings       NA                            NA            NA tilt_world   NA              
#>  6 celestial_lovebird     NA           NA      high                        0.98 1.5C RPS  2050 table hire for parties market for shed, large, wood, non-insulated, fire-unprotected shed, large, wood, non-insulated, fire-unprotected m2    construction construction residential NA          NA                 NA                     NA           NA       NA      NA            76269c17-78d6-420b-99… '4100'      buildings       NA                            NA            NA tilt_world   NA              
#>  7 celestial_lovebird     NA           NA      high                        0.4  NZ 2050   2030 table hire for parties market for shed, large, wood, non-insulated, fire-unprotected shed, large, wood, non-insulated, fire-unprotected m2    construction construction residential NA          NA                 NA                     NA           NA       NA      NA            76269c17-78d6-420b-99… '4100'      total           residential                   NA            NA tilt_world   NA              
#>  8 celestial_lovebird     NA           NA      high                        0.97 NZ 2050   2050 table hire for parties market for shed, large, wood, non-insulated, fire-unprotected shed, large, wood, non-insulated, fire-unprotected m2    construction construction residential NA          NA                 NA                     NA           NA       NA      NA            76269c17-78d6-420b-99… '4100'      total           residential                   NA            NA tilt_world   NA              
#>  9 nonphilosophical_llama NA           NA      low                         0.09 1.5C RPS  2030 surface engineering    market for deep drawing, steel, 10000 kN press, automode      deep drawing, steel, 10000 kN press, automode      kg    metals       other metals             NA          NA                 NA                     NA           NA       NA      NA            833caa78-30df-4374-90… '2591'      industry        other industry                NA            NA tilt_world   NA              
#> 10 nonphilosophical_llama NA           NA      high                        0.95 1.5C RPS  2050 surface engineering    market for deep drawing, steel, 10000 kN press, automode      deep drawing, steel, 10000 kN press, automode      kg    metals       other metals             NA          NA                 NA                     NA           NA       NA      NA            833caa78-30df-4374-90… '2591'      industry        other industry                NA            NA tilt_world   NA              
#> # ℹ 294 more rows
#> # ℹ abbreviated names: ¹​matching_certainty_company_average, ²​activity_uuid_product_uuid

sector_profile |>
  unnest_company() |>
  print() |>
  write_csv(path(params$output, "sector_profile_at_company_level.csv"))
#> # A tibble: 1,152 × 13
#>    companies_id        company_name country sector_profile_share sector_profile scenario  year matching_certainty_company_average company_city postcode address main_activity reduction_targets_avg
#>    <chr>               <chr>        <chr>                  <dbl> <chr>          <chr>    <dbl> <chr>                              <chr>        <chr>    <chr>   <chr>                         <dbl>
#>  1 antimonarchy_canine NA           NA                         0 high           1.5C RPS  2030 NA                                 NA           NA       NA      NA                             0.18
#>  2 antimonarchy_canine NA           NA                         1 medium         1.5C RPS  2030 NA                                 NA           NA       NA      NA                             0.18
#>  3 antimonarchy_canine NA           NA                         0 low            1.5C RPS  2030 NA                                 NA           NA       NA      NA                             0.18
#>  4 antimonarchy_canine NA           NA                         0 NA             1.5C RPS  2030 NA                                 NA           NA       NA      NA                             0.18
#>  5 antimonarchy_canine NA           NA                         1 high           1.5C RPS  2050 NA                                 NA           NA       NA      NA                             0.98
#>  6 antimonarchy_canine NA           NA                         0 medium         1.5C RPS  2050 NA                                 NA           NA       NA      NA                             0.98
#>  7 antimonarchy_canine NA           NA                         0 low            1.5C RPS  2050 NA                                 NA           NA       NA      NA                             0.98
#>  8 antimonarchy_canine NA           NA                         0 NA             1.5C RPS  2050 NA                                 NA           NA       NA      NA                             0.98
#>  9 antimonarchy_canine NA           NA                         1 high           NZ 2050   2030 NA                                 NA           NA       NA      NA                             0.4 
#> 10 antimonarchy_canine NA           NA                         0 medium         NZ 2050   2030 NA                                 NA           NA       NA      NA                             0.4 
#> # ℹ 1,142 more rows

The results at product and company level are now saved in the output/ directory.

# NOTE: If other workflows run before this one, this shows the results of all
params$output |> dir_tree()
#> output
#> ├── emissions_profile_at_company_level.csv
#> ├── emissions_profile_at_product_level.csv
#> ├── emissions_profile_upstream_at_company_level.csv
#> ├── emissions_profile_upstream_at_product_level.csv
#> ├── sector_profile_at_company_level.csv
#> ├── sector_profile_at_product_level.csv
#> ├── sector_profile_upstream_at_company_level.csv
#> └── sector_profile_upstream_at_product_level.csv

Cleanup

Here is the cache that allows you to resume after interruptions.

  • The number of files is determined by params$chunks.
# NOTE: If other workflows run before this one, this shows the cache of all
cache_info()
#> # A tibble: 16 × 18
#>    modification_time   path                                                               type         size permissions user   group  device_id hard_links special_device_id  inode block_size blocks flags generation access_time         change_time         birth_time         
#>    <dttm>              <fs::path>                                                         <fct> <fs::bytes> <fs::perms> <chr>  <chr>      <dbl>      <dbl>             <dbl>  <dbl>      <dbl>  <dbl> <int>      <dbl> <dttm>              <dttm>              <dttm>             
#>  1 2024-10-09 11:00:07 /home/runner/.cache/tiltWorkflows/profile_emissions/1.rds          file       186.1K rw-r--r--   runner docker      2049          1                 0 547925       4096    376     0          0 2024-10-09 11:00:08 2024-10-09 11:00:07 2024-10-09 11:00:07
#>  2 2024-10-09 11:00:07 /home/runner/.cache/tiltWorkflows/profile_emissions/2.rds          file       185.5K rw-r--r--   runner docker      2049          1                 0 547926       4096    376     0          0 2024-10-09 11:00:08 2024-10-09 11:00:07 2024-10-09 11:00:07
#>  3 2024-10-09 11:00:08 /home/runner/.cache/tiltWorkflows/profile_emissions/3.rds          file       190.7K rw-r--r--   runner docker      2049          1                 0 547927       4096    384     0          0 2024-10-09 11:00:08 2024-10-09 11:00:08 2024-10-09 11:00:08
#>  4 2024-10-09 11:00:08 /home/runner/.cache/tiltWorkflows/profile_emissions/4.rds          file         191K rw-r--r--   runner docker      2049          1                 0 547928       4096    384     0          0 2024-10-09 11:00:08 2024-10-09 11:00:08 2024-10-09 11:00:08
#>  5 2024-10-09 11:00:09 /home/runner/.cache/tiltWorkflows/profile_emissions_upstream/1.rds file       911.9K rw-r--r--   runner docker      2049          1                 0 547930       4096   1824     0          0 2024-10-09 11:00:10 2024-10-09 11:00:09 2024-10-09 11:00:09
#>  6 2024-10-09 11:00:09 /home/runner/.cache/tiltWorkflows/profile_emissions_upstream/2.rds file       190.9K rw-r--r--   runner docker      2049          1                 0 547931       4096    384     0          0 2024-10-09 11:00:10 2024-10-09 11:00:09 2024-10-09 11:00:09
#>  7 2024-10-09 11:00:10 /home/runner/.cache/tiltWorkflows/profile_emissions_upstream/3.rds file       922.8K rw-r--r--   runner docker      2049          1                 0 547932       4096   1848     0          0 2024-10-09 11:00:10 2024-10-09 11:00:10 2024-10-09 11:00:10
#>  8 2024-10-09 11:00:10 /home/runner/.cache/tiltWorkflows/profile_emissions_upstream/4.rds file       921.3K rw-r--r--   runner docker      2049          1                 0 547933       4096   1848     0          0 2024-10-09 11:00:10 2024-10-09 11:00:10 2024-10-09 11:00:10
#>  9 2024-10-09 11:00:13 /home/runner/.cache/tiltWorkflows/profile_sector/1.rds             file        91.6K rw-r--r--   runner docker      2049          1                 0 547946       4096    184     0          0 2024-10-09 11:00:14 2024-10-09 11:00:13 2024-10-09 11:00:13
#> 10 2024-10-09 11:00:14 /home/runner/.cache/tiltWorkflows/profile_sector/2.rds             file        88.4K rw-r--r--   runner docker      2049          1                 0 547947       4096    184     0          0 2024-10-09 11:00:14 2024-10-09 11:00:14 2024-10-09 11:00:14
#> 11 2024-10-09 11:00:14 /home/runner/.cache/tiltWorkflows/profile_sector/3.rds             file        89.8K rw-r--r--   runner docker      2049          1                 0 547948       4096    184     0          0 2024-10-09 11:00:14 2024-10-09 11:00:14 2024-10-09 11:00:14
#> 12 2024-10-09 11:00:14 /home/runner/.cache/tiltWorkflows/profile_sector/4.rds             file        87.9K rw-r--r--   runner docker      2049          1                 0 547949       4096    176     0          0 2024-10-09 11:00:14 2024-10-09 11:00:14 2024-10-09 11:00:14
#> 13 2024-10-09 11:00:15 /home/runner/.cache/tiltWorkflows/profile_sector_upstream/1.rds    file       100.5K rw-r--r--   runner docker      2049          1                 0 547951       4096    208     0          0 2024-10-09 11:00:15 2024-10-09 11:00:15 2024-10-09 11:00:15
#> 14 2024-10-09 11:00:15 /home/runner/.cache/tiltWorkflows/profile_sector_upstream/2.rds    file       119.8K rw-r--r--   runner docker      2049          1                 0 547952       4096    240     0          0 2024-10-09 11:00:15 2024-10-09 11:00:15 2024-10-09 11:00:15
#> 15 2024-10-09 11:00:15 /home/runner/.cache/tiltWorkflows/profile_sector_upstream/3.rds    file       101.2K rw-r--r--   runner docker      2049          1                 0 547953       4096    208     0          0 2024-10-09 11:00:15 2024-10-09 11:00:15 2024-10-09 11:00:15
#> 16 2024-10-09 11:00:15 /home/runner/.cache/tiltWorkflows/profile_sector_upstream/4.rds    file       135.9K rw-r--r--   runner docker      2049          1                 0 547954       4096    272     0          0 2024-10-09 11:00:15 2024-10-09 11:00:15 2024-10-09 11:00:15

If you want to recompute some result, you must first delete the relevant cache:

library(fs)
library(rappdirs)

dir_delete(user_cache_dir("tiltWorkflows/PROFILE-DIRECTORY-YOU-WANT-TO-DELETE"))

# DANGER: Or delete the entire default cache directory with
cache_delete()