|
| 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 | +} |
0 commit comments