Skip to content

Commit 6444744

Browse files
authored
Merge pull request #12 from eduaguilera/catalin/build-supply-use-tables
Build supply use tables
2 parents 5cfcb80 + 8b7d904 commit 6444744

8 files changed

Lines changed: 1906 additions & 6 deletions

File tree

R/input_files.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ get_file_path <- function(file_alias, force_download = FALSE) {
9494
}
9595

9696
.fetch_file_info <- function(file_alias) {
97-
file_info <- dplyr::filter(.get_input_files_data(), alias == file_alias)
97+
input_files <- .read_local_csv("input/raw/input_files.csv")
98+
file_info <- dplyr::filter(input_files, alias == file_alias)
9899

99100
if (nrow(file_info) == 0) {
100101
stop(stringr::str_glue("There is no file entry with alias {file_alias}"))
@@ -114,8 +115,8 @@ get_file_path <- function(file_alias, force_download = FALSE) {
114115
c(file_info)
115116
}
116117

117-
.get_input_files_data <- function() {
118+
.read_local_csv <- function(csv_path) {
118119
"extdata" |>
119-
system.file("input/raw/input_files.csv", package = utils::packageName()) |>
120+
system.file(csv_path, package = utils::packageName()) |>
120121
readr::read_csv(show_col_types = FALSE)
121122
}

R/supply_use.R

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# TODO: Revisit the whole file. I'm pretty sure there are mistakes,
2+
# along with incomplete parts. It's better to first finish the model
3+
# and then come back here.
4+
5+
#' Supply and use tables
6+
#'
7+
#' @description
8+
#' Create a table with processes, their inputs (_use_) and their
9+
#' outputs (_supply_).
10+
#'
11+
#' @returns
12+
#' A tibble with the supply and use data for processes.
13+
#' It contains the following columns:
14+
#' - `year`: The year in which the recorded event occurred.
15+
#' - `area`: The name of the country where the data is from.
16+
#' - `proc`: Natural language name of the process taking place.
17+
#' - `item`: Natural language name of the item taking part in the process.
18+
#' - `type`: Can have two values:
19+
#' - `use`: The given item is an input of the process.
20+
#' - `supply`: The given item is an output of the process.
21+
#' - `value`: Quantity in tonnes.
22+
#'
23+
#' @export
24+
#'
25+
#' @examples
26+
#' \dontrun{
27+
#' build_supply_use()
28+
#' }
29+
build_supply_use <- function() {
30+
# TODO: There's a name mismatch in two items
31+
# CBS_item supply_process_item
32+
# Fodder cereal and grasses Fodder crops
33+
# Fodder legumes Grazing
34+
# Keep balance sheet names for now
35+
36+
.build_supply_use_from_inputs(
37+
supply_processes = .read_local_csv("input/raw/items_supply.csv"),
38+
use_processes = .read_local_csv("input/raw/items_use.csv"),
39+
coeffs = get_processing_coefs(get_file_path("processing_coefs")),
40+
cbs = get_wide_cbs(get_file_path("commodity_balance_sheet"))
41+
)
42+
}
43+
44+
.build_supply_use_from_inputs <- function(
45+
supply_processes,
46+
use_processes,
47+
coeffs,
48+
cbs) {
49+
processes_table <- .join_supply_use_processes(supply_processes, use_processes)
50+
51+
tibble::tibble() |>
52+
.add_use_for_feed(processes_table) |>
53+
.add_use_for_seed(processes_table, cbs) |>
54+
.add_use_for_slaughtering(processes_table) |>
55+
.add_supply_use_for_processing(processes_table, coeffs) |>
56+
.add_rest_of_supply(supply_processes, cbs)
57+
}
58+
59+
.join_supply_use_processes <- function(supply_processes, use_processes) {
60+
use_processes |>
61+
dplyr::full_join(
62+
supply_processes,
63+
by = c("proc", "proc_code"),
64+
relationship = "many-to-many",
65+
suffix = c("_to_process", "_processed")
66+
)
67+
}
68+
69+
.add_rest_of_supply <- function(supply_use, supply_process_table, cbs) {
70+
items_done <- supply_use |>
71+
dplyr::filter(type == "supply") |>
72+
dplyr::distinct(item)
73+
74+
cbs |>
75+
dplyr::anti_join(items_done, "item") |>
76+
dplyr::inner_join(supply_process_table, "item") |>
77+
dplyr::filter(domestic_supply > 0) |>
78+
dplyr::select(year, area, proc, item, domestic_supply) |>
79+
dplyr::rename(value = domestic_supply) |>
80+
dplyr::mutate(type = "supply") |>
81+
dplyr::bind_rows(supply_use)
82+
}
83+
84+
.add_use_for_seed <- function(supply_use, processes_table, cbs) {
85+
processes_table |>
86+
dplyr::filter(type == "seedwaste") |>
87+
# TODO: compare with full_join, why seedwaste use on animal products?
88+
dplyr::inner_join(cbs, dplyr::join_by(item_code_to_process == item_code)) |>
89+
dplyr::filter(seed > 0) |>
90+
dplyr::select(year, area, proc, item_to_process, seed) |>
91+
dplyr::rename(item = item_to_process, value = seed) |>
92+
dplyr::mutate(type = "use") |>
93+
dplyr::bind_rows(supply_use)
94+
}
95+
96+
# TODO: Treat slaughtering processes
97+
# (probably need conversion factor from Livestock Units to physical unit)
98+
.add_use_for_slaughtering <- function(supply_use, processes_table) {
99+
supply_use
100+
}
101+
102+
# TODO: Treat animal feed use processes
103+
# Use feed intake data from Eduardo
104+
.add_use_for_feed <- function(supply_use, processes_table) {
105+
supply_use
106+
}
107+
108+
.add_supply_use_for_processing <- function(
109+
supply_use,
110+
processes_table,
111+
coeffs) {
112+
processes <- processes_table |>
113+
dplyr::filter(!type %in% c("feed", "seedwaste", "slaughtering")) |>
114+
# TODO: compare with full_join, deal with missing processes
115+
dplyr::inner_join(coeffs, by = c("item_processed", "item_to_process"))
116+
117+
supply <- processes |>
118+
dplyr::group_by(year, area, proc, item_processed) |>
119+
dplyr::summarise(value = sum(final_value_processed)) |>
120+
dplyr::ungroup() |>
121+
dplyr::rename(item = item_processed) |>
122+
dplyr::mutate(type = "supply")
123+
124+
use <- processes |>
125+
dplyr::distinct(year, area, proc, item_to_process, value_to_process) |>
126+
dplyr::rename(item = item_to_process, value = value_to_process) |>
127+
dplyr::mutate(type = "use")
128+
129+
dplyr::bind_rows(supply, use, supply_use)
130+
}

R/utils.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,36 @@
22
utils::globalVariables(
33
c(
44
"alias",
5+
"area",
56
"area_code",
67
"area_code_p",
78
"balanced_export",
89
"balanced_import",
910
"bilateral_trade",
1011
"cf",
12+
"domestic_supply",
1113
"element",
1214
"Element",
1315
"export",
16+
"final_value_processed",
1417
"from_code",
1518
"import",
1619
"Imp/Exp",
1720
"Info_Format",
1821
"item",
1922
"Item",
2023
"item_code",
24+
"item_code_to_process",
25+
"item_processed",
26+
"item_to_process",
2127
"Name",
2228
"No",
29+
"proc",
2330
"processeditem",
2431
"product_fraction",
2532
"SACO_link",
2633
"scaling",
34+
"seed",
2735
"sex",
2836
"stock_variation",
2937
"Timeline_End",
@@ -34,11 +42,13 @@ utils::globalVariables(
3442
"total_trade",
3543
"to_code",
3644
"Trade",
45+
"type",
3746
"unit",
3847
"value",
3948
"Value",
4049
"value_proc",
4150
"value_proc_raw",
51+
"value_to_process",
4252
"year",
4353
"Year"
4454
)
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
proc_code,proc,comm_code,item_code,item
2+
p001,Rice production,c001,2807,Rice and products
3+
p002,Wheat production,c002,2511,Wheat and products
4+
p003,Barley production,c003,2513,Barley and products
5+
p004,Maize production,c004,2514,Maize and products
6+
p005,Rye production,c005,2515,Rye and products
7+
p006,Oat production,c006,2516,Oats
8+
p007,Millet production,c007,2517,Millet and products
9+
p008,Sorghum production,c008,2518,Sorghum and products
10+
p009,"Cereals production, Other",c009,2520,"Cereals, Other"
11+
p010,Potatoes production,c010,2531,Potatoes and products
12+
p011,Cassava production,c011,2532,Cassava and products
13+
p012,Sweet potatoes production,c012,2533,Sweet potatoes
14+
p013,"Roots production, Other",c013,2534,"Roots, Other"
15+
p014,Yams production,c014,2535,Yams
16+
p015,Sugar cane production,c015,2536,Sugar cane
17+
p016,Sugar beet production,c016,2537,Sugar beet
18+
p017,Beans production,c017,2546,Beans
19+
p018,Peas production,c018,2547,Peas
20+
p019,"Pulses production, Other",c019,2549,"Pulses, Other and products"
21+
p020,Nuts production,c020,2551,Nuts and products
22+
p021,Soyabeans production,c021,2555,Soyabeans
23+
p022,Groundnuts production,c022,2552,Groundnuts
24+
p023,Sunflower seed production,c023,2557,Sunflower seed
25+
p024,Rape and Mustardseed production,c024,2558,Rape and Mustardseed
26+
p025,Seed cotton production,c025,328,Seed cotton
27+
p026,Coconuts production,c026,2560,Coconuts - Incl Copra
28+
p027,Sesame seed production,c027,2561,Sesame seed
29+
p028,Oil palm fruit production,c028,254,"Oil, palm fruit"
30+
p029,Olives production,c029,2563,Olives (including preserved)
31+
p030,"Oilcrops production, Other",c030,2570,"Oilcrops, Other"
32+
p031,Tomatoes production,c031,2601,Tomatoes and products
33+
p032,Onions production,c032,2602,Onions
34+
p033,"Vegetables production, Other",c033,2605,"Vegetables, Other"
35+
p034,"Oranges, Mandarines production",c034,2611,"Oranges, Mandarines"
36+
p035,"Lemons, Limes production",c035,2612,"Lemons, Limes and products"
37+
p036,Grapefruit production,c036,2613,Grapefruit and products
38+
p037,"Citrus production, Other",c037,2614,"Citrus, Other"
39+
p038,Bananas production,c038,2615,Bananas
40+
p039,Plantains production,c039,2616,Plantains
41+
p040,Apples production,c040,2617,Apples and products
42+
p041,Pineapples production,c041,2618,Pineapples and products
43+
p042,Dates production,c042,2619,Dates
44+
p043,Grapes production,c043,2620,Grapes and products (excl wine)
45+
p044,"Fruits production, Other",c044,2625,"Fruits, Other"
46+
p045,Coffee production,c045,2630,Coffee and products
47+
p046,Cocoa Beans production,c046,2633,Cocoa Beans and products
48+
p047,Tea production,c047,2635,Tea (including mate)
49+
p048,Hops production,c048,677,Hops
50+
p049,Pepper production,c049,2640,Pepper
51+
p050,Pimento production,c050,2641,Pimento
52+
p051,Cloves production,c051,2642,Cloves
53+
p052,"Spices production, Other",c052,2645,"Spices, Other"
54+
p053,Jute production,c053,2662,Jute
55+
p054,Jute-Like Fibres production,c054,2663,Jute-Like Fibres
56+
p055,"Soft-Fibres production, Other",c055,2664,"Soft-Fibres, Other"
57+
p056,Sisal production,c056,2665,Sisal
58+
p057,Abaca production,c057,2666,Abaca
59+
p058,"Hard Fibres production, Other",c058,2667,"Hard Fibres, Other"
60+
p059,Tobacco production,c059,2671,Tobacco
61+
p060,Rubber production,c060,2672,Rubber
62+
p061,Fodder crops production,c061,2000,Fodder crops
63+
p062,Grazing production,c062,2001,Grazing
64+
p063,Cotton production,c063,2559,Cottonseed
65+
p063,Cotton production,c096,2661,Cotton lint
66+
p064,"Sugar production, non-centrifugal",c065,2541,Sugar non-centrifugal
67+
p065,Sugar production,c067,2542,"Sugar (Raw Equivalent)"
68+
p066,"Sweeteners production, Other",c068,2543,"Sweeteners, Other"
69+
p067,Soyabean Oil extraction,c069,2571,Soyabean Oil
70+
p068,Groundnut Oil extraction,c070,2572,Groundnut Oil
71+
p069,Sunflowerseed Oil extraction,c071,2573,Sunflowerseed Oil
72+
p070,Rape and Mustard Oil extraction,c072,2574,Rape and Mustard Oil
73+
p071,Cottonseed Oil extraction,c073,2575,Cottonseed Oil
74+
p072,Palmkernel Oil extraction,c074,2576,Palmkernel Oil
75+
p073,Palm Oil production,c064,2562,Palm kernels
76+
p073,Palm Oil production,c075,2577,Palm Oil
77+
p074,Coconut Oil extraction,c076,2578,Coconut Oil
78+
p075,Sesameseed Oil extraction,c077,2579,Sesameseed Oil
79+
p076,Olive Oil extraction,c078,2580,Olive Oil
80+
p077,Ricebran Oil extraction,c079,2581,Ricebran Oil
81+
p078,Maize Germ Oil extraction,c080,2582,Maize Germ Oil
82+
p079,"Oilcrops Oil extraction, Other",c081,2586,"Oilcrops Oil, Other"
83+
p067,Soyabean Oil extraction,c082,2590,Soyabean Cake
84+
p068,Groundnut Oil extraction,c083,2591,Groundnut Cake
85+
p069,Sunflowerseed Oil extraction,c084,2592,Sunflowerseed Cake
86+
p070,Rape and Mustard Oil extraction,c085,2593,Rape and Mustard Cake
87+
p071,Cottonseed Oil extraction,c086,2594,Cottonseed Cake
88+
p072,Palmkernel Oil extraction,c087,2595,Palmkernel Cake
89+
p074,Coconut Oil extraction,c088,2596,Copra Cake
90+
p075,Sesameseed Oil extraction,c089,2597,Sesameseed Cake
91+
p077,Ricebran Oil extraction,c090,2598,"Oilseed Cakes, Other"
92+
p078,Maize Germ Oil extraction,c090,2598,"Oilseed Cakes, Other"
93+
p079,"Oilcrops Oil extraction, Other",c090,2598,"Oilseed Cakes, Other"
94+
p080,Wine production,c091,2655,Wine
95+
p081,Beer production,c092,2656,Beer
96+
p082,"Beverages production, Fermented",c093,2657,"Beverages, Fermented"
97+
p083,"Beverages production, Alcoholic",c094,2658,"Beverages, Alcoholic"
98+
p084,"Alcohol production, Non-Food",c095,2659,"Alcohol, Non-Food"
99+
p085,Cattle husbandry,c097,866,Cattle
100+
p086,Buffaloes husbandry,c098,946,Buffaloes
101+
p087,Sheep husbandry,c099,976,Sheep
102+
p088,Goats husbandry,c100,1016,Goats
103+
p089,Pigs farming,c101,1034,Pigs
104+
p090,Poultry Birds farming,c102,2029,Poultry Birds
105+
p091,Horses husbandry,c103,1096,Horses
106+
p092,Asses husbandry,c104,1107,Asses
107+
p093,Mules husbandry,c105,1110,Mules
108+
p094,Camels husbandry,c106,1126,Camels
109+
p095,"Camelids husbandry, other",c107,1157,"Camelids, other"
110+
p096,Rabbits husbandry,c108,1140,Rabbits and hares
111+
p097,"Rodents husbandry, other",c109,1150,"Rodents, other"
112+
p099,Dairy cattle husbandry,c110,2848,Milk - Excluding Butter
113+
p100,Dairy buffaloes husbandry,c110,2848,Milk - Excluding Butter
114+
p101,Dairy sheep husbandry,c110,2848,Milk - Excluding Butter
115+
p102,Dairy goats husbandry,c110,2848,Milk - Excluding Butter
116+
p103,Dairy camels husbandry,c110,2848,Milk - Excluding Butter
117+
p121,Butter production,c111,2740,"Butter, Ghee"
118+
p104,Cattle slaughtering,c114,2731,Bovine Meat
119+
p105,Buffaloes slaughtering,c114,2731,Bovine Meat
120+
p106,Sheep slaughtering,c115,2732,Mutton & Goat Meat
121+
p107,Goat slaughtering,c115,2732,Mutton & Goat Meat
122+
p106,Sheep slaughtering,c113,2746,Wool (Clean Eq.)
123+
p108,Pigs slaughtering,c116,2733,Pigmeat
124+
p109,Poultry slaughtering,c117,2734,Poultry Meat
125+
p109,Poultry slaughtering,c112,2744,Eggs
126+
p110,Horses slaughtering,c118,2735,"Meat, Other"
127+
p111,Asses slaughtering,c118,2735,"Meat, Other"
128+
p112,Mules slaughtering,c118,2735,"Meat, Other"
129+
p113,Camels slaughtering,c118,2735,"Meat, Other"
130+
p114,"Camelids slaughtering, other",c118,2735,"Meat, Other"
131+
p115,Rabbits slaughtering,c118,2735,"Meat, Other"
132+
p116,"Rodents slaughtering, other",c118,2735,"Meat, Other"
133+
p104,Cattle slaughtering,c119,2736,"Offals, Edible"
134+
p105,Buffaloes slaughtering,c119,2736,"Offals, Edible"
135+
p106,Sheep slaughtering,c119,2736,"Offals, Edible"
136+
p107,Goat slaughtering,c119,2736,"Offals, Edible"
137+
p108,Pigs slaughtering,c119,2736,"Offals, Edible"
138+
p109,Poultry slaughtering,c119,2736,"Offals, Edible"
139+
p110,Horses slaughtering,c119,2736,"Offals, Edible"
140+
p111,Asses slaughtering,c119,2736,"Offals, Edible"
141+
p112,Mules slaughtering,c119,2736,"Offals, Edible"
142+
p113,Camels slaughtering,c119,2736,"Offals, Edible"
143+
p114,"Camelids slaughtering, other",c119,2736,"Offals, Edible"
144+
p115,Rabbits slaughtering,c119,2736,"Offals, Edible"
145+
p116,"Rodents slaughtering, other",c119,2736,"Offals, Edible"
146+
p104,Cattle slaughtering,c120,2737,"Fats, Animals, Raw"
147+
p105,Buffaloes slaughtering,c120,2737,"Fats, Animals, Raw"
148+
p106,Sheep slaughtering,c120,2737,"Fats, Animals, Raw"
149+
p107,Goat slaughtering,c120,2737,"Fats, Animals, Raw"
150+
p108,Pigs slaughtering,c120,2737,"Fats, Animals, Raw"
151+
p109,Poultry slaughtering,c120,2737,"Fats, Animals, Raw"
152+
p110,Horses slaughtering,c120,2737,"Fats, Animals, Raw"
153+
p111,Asses slaughtering,c120,2737,"Fats, Animals, Raw"
154+
p112,Mules slaughtering,c120,2737,"Fats, Animals, Raw"
155+
p113,Camels slaughtering,c120,2737,"Fats, Animals, Raw"
156+
p114,"Camelids slaughtering, other",c120,2737,"Fats, Animals, Raw"
157+
p115,Rabbits slaughtering,c120,2737,"Fats, Animals, Raw"
158+
p116,"Rodents slaughtering, other",c120,2737,"Fats, Animals, Raw"
159+
p104,Cattle slaughtering,c121,2748,Hides and skins
160+
p105,Buffaloes slaughtering,c121,2748,Hides and skins
161+
p106,Sheep slaughtering,c121,2748,Hides and skins
162+
p107,Goat slaughtering,c121,2748,Hides and skins
163+
p118,Beekeeping,c123,2745,Honey
164+
p119,Silkworm breeding,c124,2747,Silk
165+
p120,Fishing,c125,2960,"Fish, Seafood"

0 commit comments

Comments
 (0)