-
Notifications
You must be signed in to change notification settings - Fork 2
Create utils code to name #13
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2537a0e
Create functions for area codes and names
lbm364dl 6c98cd5
Create functions for item codes and names
lbm364dl 733a552
Use regions CSV from FABIO
lbm364dl 7f29a1f
Create functions for process codes and names
lbm364dl e92d506
Reuse existing download file if skip_on_cran
lbm364dl c06a6b8
Remove `:=` R CMD Check notes
lbm364dl ec30ce7
Merge branch 'main' into catalin/create-utils-code-to-name
lbm364dl fba1792
Use WHEP list of CBS items
lbm364dl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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,":=") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.