Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Imports:
mipfp,
purrr,
readr,
rlang,
stringr,
tidyr
Encoding: UTF-8
Expand All @@ -24,7 +25,6 @@ Suggests:
here,
knitr,
pointblank,
rlang,
rmarkdown,
testthat (>= 3.0.0),
tibble
Expand Down
8 changes: 8 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
# Generated by roxygen2: do not edit by hand

export(add_area_code)
export(add_area_name)
export(add_item_code)
export(add_item_name)
export(add_process_code)
export(add_process_name)
export(build_supply_use)
export(expand_trade_sources)
export(get_bilateral_trade)
export(get_faostat_data)
export(get_file_path)
export(get_processing_coefs)
export(get_wide_cbs)
importFrom(rlang,":=")
7 changes: 7 additions & 0 deletions R/WHEP-package.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#' @keywords internal
"_PACKAGE"

## usethis namespace: start
#' @importFrom rlang :=
## usethis namespace: end
NULL
239 changes: 239 additions & 0 deletions R/code_names.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
#' Get area names from area codes
#'
#' @description
#' Add a new column to an existing tibble with the corresponding name
#' for each code. The codes are assumed to be from those defined by
#' the `FABIO` model.
#'
#' @param table The table that will be modified with a new column.
#' @param code_column The name of the column in `table` containing the codes.
#' @param name_column The name of the output column containing the names.
#'
#' @returns A tibble with all the contents of `table` and an extra column
#' named `name_column`, which contains the names. If there is no name match,
#' an `NA` is included.
#'
#' @export
#'
#' @examples
#' table <- tibble::tibble(area_code = c(1, 2, 4444, 3))
#'
#' add_area_name(table)
#'
#' table |>
#' dplyr::rename(my_area_code = area_code) |>
#' add_area_name(code_column = "my_area_code")
#'
#' add_area_name(table, name_column = "my_custom_name")
add_area_name <- function(
table,
code_column = "area_code",
name_column = "area_name") {
regions <- .get_regions(name_column, code_column)

table |>
dplyr::left_join(regions, {{ code_column }})
}

#' Get area codes from area names
#'
#' @description
#' Add a new column to an existing tibble with the corresponding code
#' for each name. The codes are assumed to be from those defined by
#' the `FABIO` model.
#'
#' @param table The table that will be modified with a new column.
#' @param code_column The name of the output column containing the codes.
#' @param name_column The name of the column in `table` containing the names.
#'
#' @returns A tibble with all the contents of `table` and an extra column
#' named `code_column`, which contains the codes. If there is no code match,
#' an `NA` is included.
#'
#' @export
#'
#' @examples
#' table <- tibble::tibble(
#' area_name = c("Armenia", "Afghanistan", "Dummy Country", "Albania")
#' )
#'
#' add_area_code(table)
#'
#' table |>
#' dplyr::rename(my_area_name = area_name) |>
#' add_area_code(name_column = "my_area_name")
#'
#' add_area_code(table, code_column = "my_custom_code")
add_area_code <- function(
table,
name_column = "area_name",
code_column = "area_code") {
regions <- .get_regions(name_column, code_column)

table |>
dplyr::left_join(regions, {{ name_column }})
}

#' Get item names from item codes
#'
#' @description
#' Add a new column to an existing tibble with the corresponding name
#' for each item code. The codes are assumed to be from those defined by
#' FAOSTAT.
#'
#' @param table The table that will be modified with a new column.
#' @param code_column The name of the column in `table` containing the codes.
#' @param name_column The name of the output column containing the names.
#'
#' @returns A tibble with all the contents of `table` and an extra column
#' named `name_column`, which contains the names. If there is no name match,
#' an `NA` is included.
#'
#' @export
#'
#' @examples
#' table <- tibble::tibble(item_code = c(2559, 2744, 9876))
#' add_item_name(table)
#'
#' table |>
#' dplyr::rename(my_item_code = item_code) |>
#' add_item_name(code_column = "my_item_code")
#'
#' add_item_name(table, name_column = "my_custom_name")
add_item_name <- function(
table,
code_column = "item_code",
name_column = "item_name") {
items <- .get_items(name_column, code_column)

table |>
dplyr::left_join(items, {{ code_column }})
}

#' Get item codes from item names
#'
#' @description
#' Add a new column to an existing tibble with the corresponding code
#' for each item name. The codes are assumed to be from those defined by
#' the FAOSTAT.
#'
#' @param table The table that will be modified with a new column.
#' @param code_column The name of the output column containing the codes.
#' @param name_column The name of the column in `table` containing the names.
#'
#' @returns A tibble with all the contents of `table` and an extra column
#' named `code_column`, which contains the codes. If there is no code match,
#' an `NA` is included.
#'
#' @export
#'
#' @examples
#' table <- tibble::tibble(item_name = c("Cottonseed", "Eggs", "Dummy Item"))
#' add_item_code(table)
#'
#' table |>
#' dplyr::rename(my_item_name = item_name) |>
#' add_item_code(name_column = "my_item_name")
#'
#' add_item_code(table, code_column = "my_custom_code")
add_item_code <- function(
table,
name_column = "item_name",
code_column = "item_code") {
items <- .get_items(name_column, code_column)

table |>
dplyr::left_join(items, {{ name_column }})
}

#' Get process names from process codes
#'
#' @description
#' Add a new column to an existing tibble with the corresponding name
#' for each process code. The codes are assumed to be from those defined by
#' the FABIO model.
#'
#' @param table The table that will be modified with a new column.
#' @param code_column The name of the column in `table` containing the codes.
#' @param name_column The name of the output column containing the names.
#'
#' @returns A tibble with all the contents of `table` and an extra column
#' named `name_column`, which contains the names. If there is no name match,
#' an `NA` is included.
#'
#' @export
#'
#' @examples
#' table <- tibble::tibble(process_code = c("p017", "p076", "dummy"))
#' add_process_name(table)
#'
#' table |>
#' dplyr::rename(my_process_code = process_code) |>
#' add_process_name(code_column = "my_process_code")
#'
#' add_process_name(table, name_column = "my_custom_name")
add_process_name <- function(
table,
code_column = "process_code",
name_column = "process_name") {
processes <- .get_processes(name_column, code_column)

table |>
dplyr::left_join(processes, {{ code_column }})
}

#' Get process codes from process names
#'
#' @description
#' Add a new column to an existing tibble with the corresponding code
#' for each process name. The codes are assumed to be from those defined by
#' the FABIO model.
#'
#' @param table The table that will be modified with a new column.
#' @param code_column The name of the output column containing the codes.
#' @param name_column The name of the column in `table` containing the names.
#'
#' @returns A tibble with all the contents of `table` and an extra column
#' named `code_column`, which contains the codes. If there is no code match,
#' an `NA` is included.
#'
#' @export
#'
#' @examples
#' table <- tibble::tibble(
#' process_name = c("Beans production", "Olive Oil extraction", "Dummy")
#' )
#' add_process_code(table)
#'
#' table |>
#' dplyr::rename(my_process_name = process_name) |>
#' add_process_code(name_column = "my_process_name")
#'
#' add_process_code(table, code_column = "my_custom_code")
add_process_code <- function(
table,
name_column = "process_name",
code_column = "process_code") {
processes <- .get_processes(name_column, code_column)

table |>
dplyr::left_join(processes, {{ name_column }})
}

.get_regions <- function(name_column, code_column) {
"input/raw/regions.csv" |>
.read_local_csv() |>
dplyr::select(!!name_column := area, !!code_column := area_code)
}

.get_items <- function(name_column, code_column) {
"input/raw/items.csv" |>
.read_local_csv() |>
dplyr::select(!!name_column := item, !!code_column := item_code)
}

.get_processes <- function(name_column, code_column) {
"input/raw/processes.csv" |>
.read_local_csv() |>
dplyr::select(!!name_column := process, !!code_column := process_code)
}
2 changes: 2 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ utils::globalVariables(
"Name",
"No",
"proc",
"process",
"process_code",
"processeditem",
"product_fraction",
"SACO_link",
Expand Down
108 changes: 108 additions & 0 deletions inst/extdata/input/raw/items.csv
Comment thread
lbm364dl marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
item_code,item
2666,Abaca
2617,Apples and products
2615,Bananas
2513,Barley and products
2546,Beans
2656,Beer
2731,Bovine Meat
2740,"Butter, Ghee"
2532,Cassava and products
2520,"Cereals, Other"
2614,"Citrus, Other"
2642,Cloves
2633,Cocoa Beans and products
2578,Coconut Oil
248,Coconuts
2560,Coconuts - Incl Copra
2630,Coffee and products
2661,Cotton lint
2559,Cottonseed
2575,Cottonseed Oil
2619,Dates
2744,Eggs
2737,"Fats, Animals, Raw"
2107,Firewood
2000,Fodder cereal and grasses
2001,Fodder legumes
2003,Fodder mix
2002,Fodder vegetables and roots
2625,"Fruits, Other"
2613,Grapefruit and products
2620,Grapes and products (excl wine)
3000,Grassland
2572,Groundnut Oil
2552,Groundnuts
2667,"Hard Fibres, Other"
776,Hemp
2748,Hides and skins
2745,Honey
677,Hops
2662,Jute
2663,Jute-Like Fibres
310,Kapok fruit
2612,"Lemons, Limes and products"
772,Linum
2582,Maize Germ Oil
2514,Maize and products
2735,"Meat, Other"
2848,Milk - Excluding Butter
2517,Millet and products
2544,Molasses
2732,Mutton & Goat Meat
2551,Nuts and products
2516,Oats
2736,"Offals, Edible"
254,"Oil, palm fruit"
2586,"Oilcrops Oil, Other"
2570,"Oilcrops, Other"
2580,Olive Oil
2563,Olives (including preserved)
2602,Onions
2611,"Oranges, Mandarines"
2106,Other crop residues
2577,Palm Oil
2562,Palm kernels
2576,Palmkernel Oil
2547,Peas
2640,Pepper
2733,Pigmeat
2641,Pimento
2618,Pineapples and products
2616,Plantains
2531,Potatoes and products
2734,Poultry Meat
2549,"Pulses, Other and products"
2574,Rape and Mustard Oil
2558,Rape and Mustardseed
2807,Rice and products
2534,"Roots, Other"
2672,Rubber
2515,Rye and products
3500,Scavenging
328,Seed cotton
2561,Sesame seed
2579,Sesameseed Oil
2747,Silk
2665,Sisal
2664,"Soft-Fibres, Other"
2518,Sorghum and products
2571,Soyabean Oil
2555,Soyabeans
2645,"Spices, Other"
2105,Straw
2542,Sugar (Raw Equivalent)
2537,Sugar beet
2536,Sugar cane
2557,Sunflower seed
2573,Sunflowerseed Oil
2533,Sweet potatoes
2635,Tea (including mate)
3002,Temporary grassland
2671,Tobacco
2601,Tomatoes and products
2605,"Vegetables, Other"
2511,Wheat and products
2655,Wine
2746,Wool (Clean Eq.)
2535,Yams
Loading