Skip to content

Commit a021a4c

Browse files
committed
Solving conflicts or merging main branch and my branch
2 parents 7825b8b + 7a4be5a commit a021a4c

56 files changed

Lines changed: 1958 additions & 2369 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.Rbuildignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@
88
^pkgdown$
99
^.*\.Rproj$
1010
^\.Rproj\.user$
11+
^data-raw$
12+
^README\.Rmd$

.Rprofile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
if (Sys.getenv("USE_RENV", unset = "TRUE") == "TRUE") {
22
source("renv/activate.R")
33
}
4+
5+
if (requireNamespace("devtools", quietly = TRUE)) {
6+
devtools::load_all()
7+
}
File renamed without changes.

DESCRIPTION

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
Package: WHEP
2-
Title: What the Package Does (One Line, Title Case)
2+
Title: Who Has Eaten the Planet
33
Version: 0.0.0.9000
4-
Authors@R:
5-
person("First", "Last", , "first.last@example.com", role = c("aut", "cre"))
6-
Description: What the package does (one paragraph).
4+
Authors@R: c(
5+
person("Catalin", "Covaci", , "catalin.covaci@csic.es", role = c("aut", "cre"), comment = c(ORCID = "0009-0005-2186-5972")),
6+
person("Eduardo", "Aguilera", , "eduardo.aguilera@csic.es", role = c("aut"), comment = c(ORCID = "0000-0003-4382-124X")),
7+
person("João", "Serra", , "jserra@agro.au.dk", role = c("ctb"), comment = c(ORCID = "0000-0002-3561-5350"))
8+
)
9+
Description: Gather historical agricultural and trade data from 1850.
710
License: MIT + file LICENSE
811
Imports:
912
dplyr,
@@ -34,4 +37,7 @@ Suggests:
3437
tibble
3538
Config/testthat/edition: 3
3639
VignetteBuilder: knitr
37-
URL: https://eduaguilera.github.io/WHEP/
40+
URL: https://eduaguilera.github.io/WHEP, https://github.com/eduaguilera/WHEP
41+
BugReports: https://github.com/eduaguilera/WHEP/issues
42+
Depends:
43+
R (>= 3.5)

NAMESPACE

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,21 @@
22

33
export(add_area_code)
44
export(add_area_name)
5-
export(add_item_code)
6-
export(add_item_name)
7-
export(add_process_code)
8-
export(add_process_name)
5+
export(add_item_cbs_code)
6+
export(add_item_cbs_name)
7+
export(add_item_prod_code)
8+
export(add_item_prod_name)
99
export(build_supply_use)
1010
export(create_n_inputs_grafs_spain)
1111
export(create_typologies_grafs_spain)
1212
export(create_typologies_of_josette)
1313
export(expand_trade_sources)
1414
export(get_bilateral_trade)
1515
export(get_faostat_data)
16+
export(get_feed_intake)
1617
export(get_file_path)
18+
export(get_primary_production)
19+
export(get_primary_residues)
1720
export(get_processing_coefs)
1821
export(get_wide_cbs)
1922
importFrom(rlang,":=")

R/bilateral_trade.R

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,13 @@
1212
#' memory usage, the tibble is not exactly in tidy format.
1313
#' It contains the following columns:
1414
#' - `year`: The year in which the recorded event occurred.
15-
#' - `item`: Natural language name for the item that is being traded.
15+
#' - `item_cbs_code`: FAOSTAT internal code for the item that is being traded.
16+
#' For code details see e.g. `add_item_cbs_name()`.
1617
#' - `bilateral_trade`: Square matrix of `NxN` dimensions where `N` is the
1718
#' total number of countries being considered. The matrix row and column
1819
#' names are exactly equal and they represent country codes.
19-
#' - Row name: FAOSTAT internal code for the country that is exporting the
20-
#' item. Equivalences with ISO 3166-1 numeric can be found in the
21-
#' _Area Codes_ CSV from the zip file that can be downloaded from
22-
#' [FAOSTAT](https://www.fao.org/faostat/en/#data/FBS). TODO: Think about
23-
#' this, would be nice to use ISO3 codes but won't be enough for our
24-
#' periods
20+
#' - Row name: The code of the country where the data is from. For code
21+
#' details see e.g. `add_area_name()`.
2522
#' - Column name: FAOSTAT internal code for the country that is importing the
2623
#' item. See row name explanation above.
2724
#'
@@ -107,7 +104,7 @@ get_bilateral_trade <- function(file_path) {
107104
cbs <- "commodity_balance_sheet" |>
108105
get_file_path() |>
109106
get_wide_cbs() |>
110-
dplyr::select(year, item, area_code, export, import)
107+
dplyr::select(year, item_cbs_code, area_code, export, import)
111108

112109
btd <- file_path |>
113110
readr::read_csv(show_col_types = FALSE) |>
@@ -116,7 +113,7 @@ get_bilateral_trade <- function(file_path) {
116113
codes <- .get_all_country_codes(btd, cbs)
117114

118115
btd |>
119-
.nest_by_year_item(cbs, codes) |>
116+
.nest_by_year_item_code(cbs, codes) |>
120117
.process_bilateral_trade(codes) |>
121118
dplyr::select(-total_trade)
122119
}
@@ -177,14 +174,14 @@ get_bilateral_trade <- function(file_path) {
177174
btd |>
178175
dplyr::rename_with(tolower) |>
179176
dplyr::mutate(
180-
item = as.factor(item),
181177
unit = ifelse(unit == "Head", "heads", unit),
182178
from_code = ifelse(element == "Export", area_code, area_code_p),
183179
to_code = ifelse(element == "Export", area_code_p, area_code),
184180
dplyr::across(c(year, from_code, to_code), as.integer)
185181
) |>
182+
add_item_cbs_code(name_column = "item") |>
186183
.prefer_flow_direction("Export") |>
187-
dplyr::select(year, from_code, to_code, item, unit, value)
184+
dplyr::select(year, from_code, to_code, item_cbs_code, unit, value)
188185
}
189186

190187
# Keep all rows with preferred direction (Import, Export)
@@ -196,7 +193,7 @@ get_bilateral_trade <- function(file_path) {
196193
bilateral_trade |>
197194
dplyr::anti_join(
198195
preferred_direction,
199-
by = c("from_code", "to_code", "year", "item")
196+
by = c("from_code", "to_code", "year", "item_cbs_code")
200197
) |>
201198
dplyr::bind_rows(preferred_direction)
202199
}
@@ -240,7 +237,7 @@ get_bilateral_trade <- function(file_path) {
240237
}
241238
}
242239

243-
.nest_by_year_item <- function(btd, cbs, codes) {
240+
.nest_by_year_item_code <- function(btd, cbs, codes) {
244241
cbs <- cbs |>
245242
dplyr::mutate(area_code = factor(area_code, levels = codes))
246243

@@ -254,53 +251,52 @@ get_bilateral_trade <- function(file_path) {
254251
.filter_only_items_in_cbs(cbs) |>
255252
tidyr::nest(
256253
bilateral_trade = c(from_code, to_code, value),
257-
.by = c(year, item)
254+
.by = c(year, item_cbs_code)
258255
) |>
259-
dplyr::left_join(.get_nested_cbs(cbs, codes), c("year", "item"))
256+
dplyr::left_join(.get_nested_cbs(cbs, codes), c("year", "item_cbs_code"))
260257
}
261258

262259
.get_nested_cbs <- function(cbs, codes) {
263260
cbs |>
264-
dplyr::mutate(item = as.factor(item)) |>
265261
.complete_total_trade(codes) |>
266-
dplyr::group_by(year, item) |>
262+
dplyr::group_by(year, item_cbs_code) |>
267263
.balance_total_trade() |>
268264
dplyr::ungroup() |>
269265
tidyr::nest(
270266
total_trade = c(
271267
area_code, export, import, balanced_export, balanced_import
272268
),
273-
.by = c(year, item)
269+
.by = c(year, item_cbs_code)
274270
)
275271
}
276272

277273
.complete_total_trade <- function(total_trade, codes) {
278274
df_codes <- tibble::tibble(area_code = codes)
279275
combs <- total_trade |>
280-
dplyr::distinct(year, item) |>
276+
dplyr::distinct(year, item_cbs_code) |>
281277
dplyr::cross_join(df_codes)
282278

283279
total_trade |>
284-
dplyr::right_join(combs, by = c("year", "item", "area_code")) |>
280+
dplyr::right_join(combs, by = c("year", "item_cbs_code", "area_code")) |>
285281
tidyr::replace_na(list(export = 0, import = 0))
286282
}
287283

288284
.filter_only_items_in_cbs <- function(btd, cbs) {
289285
btd_items <- btd |>
290-
dplyr::pull(item) |>
286+
dplyr::pull(item_cbs_code) |>
291287
unique() |>
292288
sort()
293289

294290
cbs_items <- cbs |>
295-
dplyr::pull(item) |>
291+
dplyr::pull(item_cbs_code) |>
296292
unique() |>
297293
sort()
298294

299295
# TODO: Also include these (need total export/import reports)
300296
items_not_in_cbs <- btd_items[!btd_items %in% cbs_items]
301297

302298
btd |>
303-
dplyr::filter(!item %in% items_not_in_cbs)
299+
dplyr::filter(!item_cbs_code %in% items_not_in_cbs)
304300
}
305301

306302
.get_all_country_codes <- function(btd, cbs) {

0 commit comments

Comments
 (0)