Skip to content

Commit ef097e2

Browse files
authored
Merge pull request #13 from eduaguilera/catalin/create-utils-code-to-name
Create utils code to name
2 parents 6444744 + 812dd7c commit ef097e2

17 files changed

Lines changed: 1167 additions & 9 deletions

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Imports:
1313
mipfp,
1414
purrr,
1515
readr,
16+
rlang,
1617
stringr,
1718
tidyr
1819
Encoding: UTF-8
@@ -24,7 +25,6 @@ Suggests:
2425
here,
2526
knitr,
2627
pointblank,
27-
rlang,
2828
rmarkdown,
2929
testthat (>= 3.0.0),
3030
tibble

NAMESPACE

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
# Generated by roxygen2: do not edit by hand
22

3+
export(add_area_code)
4+
export(add_area_name)
5+
export(add_item_code)
6+
export(add_item_name)
7+
export(add_process_code)
8+
export(add_process_name)
9+
export(build_supply_use)
310
export(expand_trade_sources)
411
export(get_bilateral_trade)
512
export(get_faostat_data)
613
export(get_file_path)
714
export(get_processing_coefs)
815
export(get_wide_cbs)
16+
importFrom(rlang,":=")

R/WHEP-package.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#' @keywords internal
2+
"_PACKAGE"
3+
4+
## usethis namespace: start
5+
#' @importFrom rlang :=
6+
## usethis namespace: end
7+
NULL

R/code_names.R

Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
#' Get area names from area codes
2+
#'
3+
#' @description
4+
#' Add a new column to an existing tibble with the corresponding name
5+
#' for each code. The codes are assumed to be from those defined by
6+
#' the `FABIO` model.
7+
#'
8+
#' @param table The table that will be modified with a new column.
9+
#' @param code_column The name of the column in `table` containing the codes.
10+
#' @param name_column The name of the output column containing the names.
11+
#'
12+
#' @returns A tibble with all the contents of `table` and an extra column
13+
#' named `name_column`, which contains the names. If there is no name match,
14+
#' an `NA` is included.
15+
#'
16+
#' @export
17+
#'
18+
#' @examples
19+
#' table <- tibble::tibble(area_code = c(1, 2, 4444, 3))
20+
#'
21+
#' add_area_name(table)
22+
#'
23+
#' table |>
24+
#' dplyr::rename(my_area_code = area_code) |>
25+
#' add_area_name(code_column = "my_area_code")
26+
#'
27+
#' add_area_name(table, name_column = "my_custom_name")
28+
add_area_name <- function(
29+
table,
30+
code_column = "area_code",
31+
name_column = "area_name") {
32+
regions <- .get_regions(name_column, code_column)
33+
34+
table |>
35+
dplyr::left_join(regions, {{ code_column }})
36+
}
37+
38+
#' Get area codes from area names
39+
#'
40+
#' @description
41+
#' Add a new column to an existing tibble with the corresponding code
42+
#' for each name. The codes are assumed to be from those defined by
43+
#' the `FABIO` model.
44+
#'
45+
#' @param table The table that will be modified with a new column.
46+
#' @param code_column The name of the output column containing the codes.
47+
#' @param name_column The name of the column in `table` containing the names.
48+
#'
49+
#' @returns A tibble with all the contents of `table` and an extra column
50+
#' named `code_column`, which contains the codes. If there is no code match,
51+
#' an `NA` is included.
52+
#'
53+
#' @export
54+
#'
55+
#' @examples
56+
#' table <- tibble::tibble(
57+
#' area_name = c("Armenia", "Afghanistan", "Dummy Country", "Albania")
58+
#' )
59+
#'
60+
#' add_area_code(table)
61+
#'
62+
#' table |>
63+
#' dplyr::rename(my_area_name = area_name) |>
64+
#' add_area_code(name_column = "my_area_name")
65+
#'
66+
#' add_area_code(table, code_column = "my_custom_code")
67+
add_area_code <- function(
68+
table,
69+
name_column = "area_name",
70+
code_column = "area_code") {
71+
regions <- .get_regions(name_column, code_column)
72+
73+
table |>
74+
dplyr::left_join(regions, {{ name_column }})
75+
}
76+
77+
#' Get item names from item codes
78+
#'
79+
#' @description
80+
#' Add a new column to an existing tibble with the corresponding name
81+
#' for each item code. The codes are assumed to be from those defined by
82+
#' FAOSTAT.
83+
#'
84+
#' @param table The table that will be modified with a new column.
85+
#' @param code_column The name of the column in `table` containing the codes.
86+
#' @param name_column The name of the output column containing the names.
87+
#'
88+
#' @returns A tibble with all the contents of `table` and an extra column
89+
#' named `name_column`, which contains the names. If there is no name match,
90+
#' an `NA` is included.
91+
#'
92+
#' @export
93+
#'
94+
#' @examples
95+
#' table <- tibble::tibble(item_code = c(2559, 2744, 9876))
96+
#' add_item_name(table)
97+
#'
98+
#' table |>
99+
#' dplyr::rename(my_item_code = item_code) |>
100+
#' add_item_name(code_column = "my_item_code")
101+
#'
102+
#' add_item_name(table, name_column = "my_custom_name")
103+
add_item_name <- function(
104+
table,
105+
code_column = "item_code",
106+
name_column = "item_name") {
107+
items <- .get_items(name_column, code_column)
108+
109+
table |>
110+
dplyr::left_join(items, {{ code_column }})
111+
}
112+
113+
#' Get item codes from item names
114+
#'
115+
#' @description
116+
#' Add a new column to an existing tibble with the corresponding code
117+
#' for each item name. The codes are assumed to be from those defined by
118+
#' the FAOSTAT.
119+
#'
120+
#' @param table The table that will be modified with a new column.
121+
#' @param code_column The name of the output column containing the codes.
122+
#' @param name_column The name of the column in `table` containing the names.
123+
#'
124+
#' @returns A tibble with all the contents of `table` and an extra column
125+
#' named `code_column`, which contains the codes. If there is no code match,
126+
#' an `NA` is included.
127+
#'
128+
#' @export
129+
#'
130+
#' @examples
131+
#' table <- tibble::tibble(item_name = c("Cottonseed", "Eggs", "Dummy Item"))
132+
#' add_item_code(table)
133+
#'
134+
#' table |>
135+
#' dplyr::rename(my_item_name = item_name) |>
136+
#' add_item_code(name_column = "my_item_name")
137+
#'
138+
#' add_item_code(table, code_column = "my_custom_code")
139+
add_item_code <- function(
140+
table,
141+
name_column = "item_name",
142+
code_column = "item_code") {
143+
items <- .get_items(name_column, code_column)
144+
145+
table |>
146+
dplyr::left_join(items, {{ name_column }})
147+
}
148+
149+
#' Get process names from process codes
150+
#'
151+
#' @description
152+
#' Add a new column to an existing tibble with the corresponding name
153+
#' for each process code. The codes are assumed to be from those defined by
154+
#' the FABIO model.
155+
#'
156+
#' @param table The table that will be modified with a new column.
157+
#' @param code_column The name of the column in `table` containing the codes.
158+
#' @param name_column The name of the output column containing the names.
159+
#'
160+
#' @returns A tibble with all the contents of `table` and an extra column
161+
#' named `name_column`, which contains the names. If there is no name match,
162+
#' an `NA` is included.
163+
#'
164+
#' @export
165+
#'
166+
#' @examples
167+
#' table <- tibble::tibble(process_code = c("p017", "p076", "dummy"))
168+
#' add_process_name(table)
169+
#'
170+
#' table |>
171+
#' dplyr::rename(my_process_code = process_code) |>
172+
#' add_process_name(code_column = "my_process_code")
173+
#'
174+
#' add_process_name(table, name_column = "my_custom_name")
175+
add_process_name <- function(
176+
table,
177+
code_column = "process_code",
178+
name_column = "process_name") {
179+
processes <- .get_processes(name_column, code_column)
180+
181+
table |>
182+
dplyr::left_join(processes, {{ code_column }})
183+
}
184+
185+
#' Get process codes from process names
186+
#'
187+
#' @description
188+
#' Add a new column to an existing tibble with the corresponding code
189+
#' for each process name. The codes are assumed to be from those defined by
190+
#' the FABIO model.
191+
#'
192+
#' @param table The table that will be modified with a new column.
193+
#' @param code_column The name of the output column containing the codes.
194+
#' @param name_column The name of the column in `table` containing the names.
195+
#'
196+
#' @returns A tibble with all the contents of `table` and an extra column
197+
#' named `code_column`, which contains the codes. If there is no code match,
198+
#' an `NA` is included.
199+
#'
200+
#' @export
201+
#'
202+
#' @examples
203+
#' table <- tibble::tibble(
204+
#' process_name = c("Beans production", "Olive Oil extraction", "Dummy")
205+
#' )
206+
#' add_process_code(table)
207+
#'
208+
#' table |>
209+
#' dplyr::rename(my_process_name = process_name) |>
210+
#' add_process_code(name_column = "my_process_name")
211+
#'
212+
#' add_process_code(table, code_column = "my_custom_code")
213+
add_process_code <- function(
214+
table,
215+
name_column = "process_name",
216+
code_column = "process_code") {
217+
processes <- .get_processes(name_column, code_column)
218+
219+
table |>
220+
dplyr::left_join(processes, {{ name_column }})
221+
}
222+
223+
.get_regions <- function(name_column, code_column) {
224+
"input/raw/regions.csv" |>
225+
.read_local_csv() |>
226+
dplyr::select(!!name_column := area, !!code_column := area_code)
227+
}
228+
229+
.get_items <- function(name_column, code_column) {
230+
"input/raw/items.csv" |>
231+
.read_local_csv() |>
232+
dplyr::select(!!name_column := item, !!code_column := item_code)
233+
}
234+
235+
.get_processes <- function(name_column, code_column) {
236+
"input/raw/processes.csv" |>
237+
.read_local_csv() |>
238+
dplyr::select(!!name_column := process, !!code_column := process_code)
239+
}

R/utils.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ utils::globalVariables(
2727
"Name",
2828
"No",
2929
"proc",
30+
"process",
31+
"process_code",
3032
"processeditem",
3133
"product_fraction",
3234
"SACO_link",

inst/extdata/input/raw/items.csv

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
item_code,item
2+
2666,Abaca
3+
2617,Apples and products
4+
2615,Bananas
5+
2513,Barley and products
6+
2546,Beans
7+
2656,Beer
8+
2731,Bovine Meat
9+
2740,"Butter, Ghee"
10+
2532,Cassava and products
11+
2520,"Cereals, Other"
12+
2614,"Citrus, Other"
13+
2642,Cloves
14+
2633,Cocoa Beans and products
15+
2578,Coconut Oil
16+
248,Coconuts
17+
2560,Coconuts - Incl Copra
18+
2630,Coffee and products
19+
2661,Cotton lint
20+
2559,Cottonseed
21+
2575,Cottonseed Oil
22+
2619,Dates
23+
2744,Eggs
24+
2737,"Fats, Animals, Raw"
25+
2107,Firewood
26+
2000,Fodder cereal and grasses
27+
2001,Fodder legumes
28+
2003,Fodder mix
29+
2002,Fodder vegetables and roots
30+
2625,"Fruits, Other"
31+
2613,Grapefruit and products
32+
2620,Grapes and products (excl wine)
33+
3000,Grassland
34+
2572,Groundnut Oil
35+
2552,Groundnuts
36+
2667,"Hard Fibres, Other"
37+
776,Hemp
38+
2748,Hides and skins
39+
2745,Honey
40+
677,Hops
41+
2662,Jute
42+
2663,Jute-Like Fibres
43+
310,Kapok fruit
44+
2612,"Lemons, Limes and products"
45+
772,Linum
46+
2582,Maize Germ Oil
47+
2514,Maize and products
48+
2735,"Meat, Other"
49+
2848,Milk - Excluding Butter
50+
2517,Millet and products
51+
2544,Molasses
52+
2732,Mutton & Goat Meat
53+
2551,Nuts and products
54+
2516,Oats
55+
2736,"Offals, Edible"
56+
254,"Oil, palm fruit"
57+
2586,"Oilcrops Oil, Other"
58+
2570,"Oilcrops, Other"
59+
2580,Olive Oil
60+
2563,Olives (including preserved)
61+
2602,Onions
62+
2611,"Oranges, Mandarines"
63+
2106,Other crop residues
64+
2577,Palm Oil
65+
2562,Palm kernels
66+
2576,Palmkernel Oil
67+
2547,Peas
68+
2640,Pepper
69+
2733,Pigmeat
70+
2641,Pimento
71+
2618,Pineapples and products
72+
2616,Plantains
73+
2531,Potatoes and products
74+
2734,Poultry Meat
75+
2549,"Pulses, Other and products"
76+
2574,Rape and Mustard Oil
77+
2558,Rape and Mustardseed
78+
2807,Rice and products
79+
2534,"Roots, Other"
80+
2672,Rubber
81+
2515,Rye and products
82+
3500,Scavenging
83+
328,Seed cotton
84+
2561,Sesame seed
85+
2579,Sesameseed Oil
86+
2747,Silk
87+
2665,Sisal
88+
2664,"Soft-Fibres, Other"
89+
2518,Sorghum and products
90+
2571,Soyabean Oil
91+
2555,Soyabeans
92+
2645,"Spices, Other"
93+
2105,Straw
94+
2542,Sugar (Raw Equivalent)
95+
2537,Sugar beet
96+
2536,Sugar cane
97+
2557,Sunflower seed
98+
2573,Sunflowerseed Oil
99+
2533,Sweet potatoes
100+
2635,Tea (including mate)
101+
3002,Temporary grassland
102+
2671,Tobacco
103+
2601,Tomatoes and products
104+
2605,"Vegetables, Other"
105+
2511,Wheat and products
106+
2655,Wine
107+
2746,Wool (Clean Eq.)
108+
2535,Yams

0 commit comments

Comments
 (0)