Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polish workflow and make it reproducible using toy data #6

Merged
merged 16 commits into from
Dec 8, 2023
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.Rproj.user
docs
inst/templates/tilt-profiles.html
inst/templates/tiltWorkflows.html
290 changes: 131 additions & 159 deletions inst/templates/tilt-profiles.Rmd
Original file line number Diff line number Diff line change
@@ -1,236 +1,208 @@
---
title: "tilt workflow"
title: "Creating outputs for all tilt indicators"
author: "Kalash Singhal"
---

```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
# FIXME when this file become reproducible
eval = FALSE
comment = "#>"
)
```

This file uses the output from the tiltIndicatorBefore package as the inputs to
the tiltIndicator and tiltIndicatorAfter packages. This script uses the CSV
files from the tiltIndicatorBefore package to create the final output of the
four indicators.
This workflow creates the output of all tilt indicators.

## Setup

```{r}
```{r global}
library(dplyr, warn.conflicts = FALSE)
library(readr, warn.conflicts = FALSE)
library(tidyr, warn.conflicts = FALSE)
# devtools::install_github("2DegreesInvesting/tiltIndicatorAfter")
library(tiltIndicatorAfter)
library(fs)
library(tiltWorkflows)

# Help create paths under ~/Downloads/tilt/
tilt_path <- function(...) {
path_home("Downloads", "tilt", ...)
}
# Create a folder to save results
dir_create(tilt_path("output"))
# Print wider data frames
options(width = 500)
```

## tiltIndicatorAfter data common for all indicators
## Data

`ep_companies`, `ei_activities_overview`, `mapper_ep_ei`, `ei_input_data`, and
`isic_4digit_name` are the five datasets which are used as the input for the
tiltIndicatorAfter package. They all are the output from tiltIndicatorBefore
package. These five datasets are used for adding additional data to all the four
indicators (Emissions profile, Emissions profile upstream, Sector profile,
Sector profile upstream).
Here we use toy datasets that you should replace with your real data.

```{r}
# ep_companies
europages_campanies <- read_csv("/path/ep_companies.csv") |>
select("company_name", "country", "company_city", "postcode", "address", "main_activity", "companies_id")

# ecoinvent_activities
ei_activities_ov <- read_csv("/path/ei_activities_overview.csv")

# matches_mapper
mapper_ep_ei <- read_csv("/path/mapper_ep_ei.csv")

# ecoinvent_inputs
ei_input <- read_csv("/path/ei_input_data.csv") |>
select("input_activity_uuid_product_uuid", "exchange_name", "exchange_unit_name") |>
europages_campanies <- tiltIndicatorAfter::ep_companies |>
select(
"company_name",
"country",
"company_city",
"postcode",
"address",
"main_activity",
"companies_id"
)

ecoinvent_activities <- tiltIndicatorAfter::ecoinvent_activities

ecoinvent_europages <- tiltIndicatorAfter::matches_mapper

ecoinvent_inputs <- tiltIndicatorAfter::ecoinvent_inputs |>
select(
"input_activity_uuid_product_uuid",
"exchange_name",
"exchange_unit_name"
) |>
distinct()

# isic_4digit_name
isic_4digit_name <- read_csv("/path/isic_4digit_name.csv")
isic <- tiltIndicatorAfter::isic_tilt_mapper
```

## Emissions profile

### Load Data

The input data of Emissions_profile which comes from tiltIndicatorBefore package
becomes the input for the tiltIndicator package.

```{r}
# emissions_profile companies
ep_company <- read_csv("/path/emissions_profile_any_companies_ecoinvent.csv")

# emissions_profile products
ep_product <- read_csv("/path/emissions_profile_products_ecoinvent.csv")
```

### Create Emissions_profile product and company level outputs

The code takes the output of tiltIndicator and use it as an input for the
tiltIndicatorAfter package.

```{r}
emissions_profile <- profile_emissions(ep_company,
ep_product,
# Load data specific to this indicator
emissions_profile_any_companies <- read_csv(toy_emissions_profile_any_companies())
# FIXME User toy_emissions_profile_products_ecoinvent()
# See https://github.com/2DegreesInvesting/tiltToyData/pull/12
# https://github.com/2DegreesInvesting/tiltWorkflows/issues/9
emissions_profile_products <- read_csv(toy_emissions_profile_products())

# Create results at product and company level
emissions_profile <- profile_emissions(
emissions_profile_any_companies,
emissions_profile_products,
europages_companies = europages_campanies,
ecoinvent_activities = ei_activities_ov,
ecoinvent_europages = mapper_ep_ei,
isic_tilt = isic_4digit_name,
ecoinvent_activities = ecoinvent_activities,
ecoinvent_europages = ecoinvent_europages,
isic = isic,
low_threshold = 1 / 3,
high_threshold = 2 / 3
)

emissions_profile_at_product_level <- emissions_profile |> unnest_product()
emissions_profile_at_company_level <- emissions_profile |> unnest_company()
```
emissions_profile_at_product_level <- emissions_profile |>
unnest_product()
emissions_profile_at_product_level

### Save the Emissions profile final results
emissions_profile_at_company_level <- emissions_profile |>
unnest_company()
emissions_profile_at_company_level

```{r}
emissions_profile_at_product_level |>
write_csv("/path/emissions_profile_at_product_level.csv")

write_csv(tilt_path("output", "emissions_profile_at_product_level.csv"))
emissions_profile_at_company_level |>
write_csv("/path/emissions_profile_at_company_level.csv")
write_csv(tilt_path("output", "emissions_profile_at_company_level.csv"))
```

## Emissions Profile Upstream

### Load Data

The input data of Emissions Profile Upstream which comes from
tiltIndicatorBefore package becomes the input for the tiltIndicator package.
## Emissions profile upstream

```{r}
epu_company <- read_csv("/path/emissions_profile_any_companies_ecoinvent.csv")

epu_product <- read_csv("/path/emissions_profile_upstream_products_ecoinvent.csv")
```

### Create Emissions profile upstream product and company level outputs

The code takes the output of tiltIndicator and use it as an input for the
tiltIndicatorAfter package.

```{r}
emissions_profile_upstream <- profile_emissions_upstream(epu_company,
epu_product,
# Load data specific to this indicator
# FIXME User toy_emissions_profile_upstream_products_ecoinvent()
# See https://github.com/2DegreesInvesting/tiltToyData/pull/12
# https://github.com/2DegreesInvesting/tiltWorkflows/issues/9
emissions_profile_upstream_products <- read_csv(toy_emissions_profile_upstream_products())

# Create results at product and company level
emissions_profile_upstream <- profile_emissions_upstream(
emissions_profile_any_companies,
emissions_profile_upstream_products,
europages_companies = europages_campanies,
ecoinvent_activities = ei_activities_ov,
ecoinvent_inputs = ei_input,
ecoinvent_europages = mapper_ep_ei,
isic_tilt = isic_4digit_name,
ecoinvent_activities = ecoinvent_activities,
ecoinvent_inputs = ecoinvent_inputs,
ecoinvent_europages = ecoinvent_europages,
isic = isic,
low_threshold = 1 / 3,
high_threshold = 2 / 3
)

emissions_profile_upstream_at_product_level <- emissions_profile_upstream |> unnest_product()
emissions_profile_upstream_at_company_level <- emissions_profile_upstream |> unnest_company()
```
emissions_profile_upstream_at_product_level <- emissions_profile_upstream |>
unnest_product()
emissions_profile_upstream_at_product_level

### Save the Emissions profile upstream final results
emissions_profile_upstream_at_company_level <- emissions_profile_upstream |>
unnest_company()
emissions_profile_upstream_at_company_level

```{r}
emissions_profile_upstream_at_product_level |>
write_csv("/path/emissions_profile_upstream_at_product_level.csv")

write_csv(tilt_path("output", "emissions_profile_upstream_at_product_level.csv"))
emissions_profile_upstream_at_company_level |>
write_csv("/path/emissions_profile_upstream_at_company_level.csv")
write_csv(tilt_path("output", "emissions_profile_upstream_at_company_level.csv"))
```

## Sector Profile

### Load Data

The input data of Sector Profile which comes from tiltIndicatorBefore package
becomes the input for the tiltIndicator package.

```{r}
sp_company <- read_csv("/path/sector_profile_companies.csv")

sp_scenarios <- read_csv("/path/sector_profile_any_scenarios.csv")
```

### Create Sector profile product and company level outputs

The code takes the output of tiltIndicator and use it as an input for the
tiltIndicatorAfter package.
## Sector profile

```{r}
sector_profile <- profile_sector(sp_company,
sp_scenarios,
# Load data specific to this indicator
sector_profile_companies <- read_csv(toy_sector_profile_companies())
sector_profile_any_scenarios <- read_csv(toy_sector_profile_any_scenarios())

# Create results at product and company level
sector_profile <- profile_sector(
sector_profile_companies,
sector_profile_any_scenarios,
europages_companies = europages_campanies,
ecoinvent_activities = ei_activities_ov,
ecoinvent_europages = mapper_ep_ei,
isic_tilt = isic_4digit_name,
ecoinvent_activities = ecoinvent_activities,
ecoinvent_europages = ecoinvent_europages,
isic = isic,
low_threshold = 1 / 3,
high_threshold = 2 / 3
)

sector_profile_at_product_level <- sector_profile |> unnest_product()
sector_profile_at_company_level <- sector_profile |> unnest_company()
```
sector_profile_at_product_level <- sector_profile |>
unnest_product()
sector_profile_at_product_level

### Save the Sector profile final results
sector_profile_at_company_level <- sector_profile |>
unnest_company()
sector_profile_at_company_level

```{r}
sector_profile_at_product_level |>
write_csv("/path/sector_profile_at_product_level.csv")

write_csv(tilt_path("output", "sector_profile_at_product_level.csv"))
sector_profile_at_company_level |>
write_csv("/path/sector_profile_at_company_level.csv")
write_csv(tilt_path("output", "sector_profile_at_company_level.csv"))
```

## Sector profile upstream

### Load Data

The input data of Sector profile upstream which comes from tiltIndicatorBefore
package becomes the input for the tiltIndicator package.

```{r}
spu_company <- read_csv("/path/sector_profile_upstream_companies.csv")

spu_product <- read_csv("/path/sector_profile_upstream_products.csv")

spu_scenarios <- read_csv("/path/sector_profile_any_scenarios.csv")
```

### Create Sector profile upstream product and company level outputs.

The code takes the output of tiltIndicator and use it as an input for the
tiltIndicatorAfter package.

```{r}
sector_profile_upstream <- profile_sector_upstream(spu_company,
spu_scenarios,
spu_product,
# Load data specific to this indicator
sector_profile_upstream_companies <- read_csv(toy_sector_profile_upstream_companies())
sector_profile_upstream_products <- read_csv(toy_sector_profile_upstream_products())

# Create results at product and company level
sector_profile_upstream <- profile_sector_upstream(
sector_profile_upstream_companies,
sector_profile_any_scenarios,
sector_profile_upstream_products,
europages_companies = europages_campanies,
ecoinvent_activities = ei_activities_ov,
ecoinvent_inputs = ei_input,
ecoinvent_europages = mapper_ep_ei,
isic_tilt = isic_4digit_name,
ecoinvent_activities = ecoinvent_activities,
ecoinvent_inputs = ecoinvent_inputs,
ecoinvent_europages = ecoinvent_europages,
isic = isic,
low_threshold = 1 / 3,
high_threshold = 2 / 3
)

sector_profile_upstream_at_product_level <- sector_profile_upstream |> unnest_product()
sector_profile_upstream_at_company_level <- sector_profile_upstream |> unnest_company()
```
sector_profile_upstream_at_product_level <- sector_profile_upstream |>
unnest_product()
sector_profile_upstream_at_product_level

### Save the Sector profile upstream final results
sector_profile_upstream_at_company_level <- sector_profile_upstream |>
unnest_company()
sector_profile_upstream_at_company_level

```{r}
sector_profile_upstream_at_product_level |>
write_csv("/path/sector_profile_upstream_at_product_level.csv")

write_csv(tilt_path("output", "sector_profile_upstream_at_product_level.csv"))
sector_profile_upstream_at_company_level |>
write_csv("/path/sector_profile_upstream_at_company_level.csv")
write_csv(tilt_path("output", "sector_profile_upstream_at_company_level.csv"))
```

## Results

```{r}
dir_tree(tilt_path("output"))
```
2 changes: 1 addition & 1 deletion vignettes/articles/tiltWorkflows.Rmd
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: "tilt workflow"
title: "Creating outputs for all tilt indicators"
author: "Kalash Singhal"
---

Expand Down