Skip to content

Commit fbeddc2

Browse files
authored
Merge pull request #9 from eduaguilera/catalin/balance-bilateral-trade
Optimize bilateral trade and allow public link downloads
2 parents f7262aa + 46edb3d commit fbeddc2

13 files changed

Lines changed: 5112 additions & 1311 deletions

.github/workflows/R-CMD-check_and_lintr.yaml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,18 @@ jobs:
1515
env:
1616
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
1717
R_KEEP_PKG_SOURCE: yes
18-
GOOGLE_CLOUD_KEY: ${{ secrets.GOOGLE_CLOUD_KEY }}
1918
steps:
2019
- uses: actions/checkout@v4
2120

2221
- uses: r-lib/actions/setup-pandoc@v2
2322

2423
- uses: r-lib/actions/setup-r@v2
2524
with:
25+
r-version: 'renv'
2626
use-public-rspm: true
2727

2828
- uses: r-lib/actions/setup-renv@v2
2929

30-
- name: Set googlesheets4 token
31-
run: echo "$GOOGLE_CLOUD_KEY" > ${{ github.workspace }}/inst/google_cloud_key.json
32-
3330
- uses: r-lib/actions/check-r-package@v2
3431
with:
3532
upload-snapshots: true

.github/workflows/pkgdown.yaml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ jobs:
2121
env:
2222
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
2323
R_KEEP_PKG_SOURCE: yes
24-
GOOGLE_CLOUD_KEY: ${{ secrets.GOOGLE_CLOUD_KEY }}
2524
permissions:
2625
contents: write
2726
steps:
@@ -35,9 +34,6 @@ jobs:
3534

3635
- uses: r-lib/actions/setup-renv@v2
3736

38-
- name: Set googlesheets4 token
39-
run: echo "$GOOGLE_CLOUD_KEY" > ${{ github.workspace }}/inst/google_cloud_key.json
40-
4137
- name: Install package
4238
run: devtools::install()
4339
shell: Rscript {0}

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Imports:
99
dplyr,
1010
FAOSTAT,
1111
googledrive,
12+
httr2,
1213
mipfp,
1314
purrr,
1415
readr,

R/bilateral_trade.R

Lines changed: 41 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -109,27 +109,28 @@ get_bilateral_trade <- function(file_path) {
109109
get_wide_cbs() |>
110110
dplyr::select(year, item, area_code, export, import)
111111

112-
btd <-
113-
file_path |>
112+
btd <- file_path |>
114113
readr::read_csv(show_col_types = FALSE) |>
115114
.clean_bilateral_trade()
116115

117116
codes <- .get_all_country_codes(btd, cbs)
118117

119118
btd |>
120-
.build_trade_matrices(cbs, codes) |>
121-
.fill_all_missing_trade(cbs) |>
122-
.balance_matrices() |>
119+
.nest_by_year_item(cbs, codes) |>
120+
.process_bilateral_trade(codes) |>
123121
dplyr::select(-total_trade)
124122
}
125123

126-
.balance_matrices <- function(btd) {
124+
.process_bilateral_trade <- function(btd, codes) {
127125
btd |>
128126
dplyr::mutate(
129127
bilateral_trade = purrr::map2(
130128
bilateral_trade,
131129
total_trade,
132-
.balance_matrix
130+
~ .x |>
131+
.build_trade_matrix(codes) |>
132+
.fill_missing_trade(.y) |>
133+
.balance_matrix(.y)
133134
)
134135
)
135136
}
@@ -180,6 +181,7 @@ get_bilateral_trade <- function(file_path) {
180181
unit = ifelse(unit == "Head", "heads", unit),
181182
from_code = ifelse(element == "Export", area_code, area_code_p),
182183
to_code = ifelse(element == "Export", area_code_p, area_code),
184+
dplyr::across(c(year, from_code, to_code), as.integer)
183185
) |>
184186
.prefer_flow_direction("Export") |>
185187
dplyr::select(year, from_code, to_code, item, unit, value)
@@ -199,17 +201,6 @@ get_bilateral_trade <- function(file_path) {
199201
dplyr::bind_rows(preferred_direction)
200202
}
201203

202-
.fill_all_missing_trade <- function(btd, cbs) {
203-
btd |>
204-
dplyr::mutate(
205-
bilateral_trade = purrr::map2(
206-
bilateral_trade,
207-
total_trade,
208-
.fill_missing_trade
209-
)
210-
)
211-
}
212-
213204
.fill_missing_trade <- function(trade_matrix, total_trade) {
214205
exports <- total_trade |>
215206
dplyr::pull(export)
@@ -249,47 +240,49 @@ get_bilateral_trade <- function(file_path) {
249240
}
250241
}
251242

252-
.build_trade_matrices <- function(btd, cbs, codes) {
243+
.nest_by_year_item <- function(btd, cbs, codes) {
244+
cbs <- cbs |>
245+
dplyr::mutate(area_code = factor(area_code, levels = codes))
246+
253247
btd |>
254248
dplyr::filter(unit == "tonnes") |>
255249
dplyr::select(-unit) |>
250+
dplyr::mutate(
251+
from_code = factor(from_code, levels = codes),
252+
to_code = factor(to_code, levels = codes),
253+
) |>
256254
.filter_only_items_in_cbs(cbs) |>
257255
tidyr::nest(
258256
bilateral_trade = c(from_code, to_code, value),
259257
.by = c(year, item)
260258
) |>
261-
dplyr::left_join(.get_nested_cbs(cbs, codes), c("year", "item")) |>
262-
dplyr::mutate(
263-
bilateral_trade = purrr::map(
264-
bilateral_trade,
265-
~ .build_trade_matrix(.x, codes)
266-
)
267-
)
259+
dplyr::left_join(.get_nested_cbs(cbs, codes), c("year", "item"))
268260
}
269261

270262
.get_nested_cbs <- function(cbs, codes) {
271263
cbs |>
264+
dplyr::mutate(item = as.factor(item)) |>
265+
.complete_total_trade(codes) |>
266+
dplyr::group_by(year, item) |>
267+
.balance_total_trade() |>
268+
dplyr::ungroup() |>
272269
tidyr::nest(
273-
total_trade = c(area_code, export, import),
270+
total_trade = c(
271+
area_code, export, import, balanced_export, balanced_import
272+
),
274273
.by = c(year, item)
275-
) |>
276-
dplyr::mutate(
277-
total_trade = purrr::map(
278-
total_trade,
279-
~ .x |>
280-
.complete_total_trade(codes) |>
281-
.balance_total_trade()
282-
)
283274
)
284275
}
285276

286277
.complete_total_trade <- function(total_trade, codes) {
278+
df_codes <- tibble::tibble(area_code = codes)
279+
combs <- total_trade |>
280+
dplyr::distinct(year, item) |>
281+
dplyr::cross_join(df_codes)
282+
287283
total_trade |>
288-
tidyr::complete(
289-
area_code = codes,
290-
fill = list(export = 0, import = 0)
291-
) |>
292-
dplyr::arrange(area_code)
284+
dplyr::right_join(combs, by = c("year", "item", "area_code")) |>
285+
tidyr::replace_na(list(export = 0, import = 0))
293286
}
294287

295288
.filter_only_items_in_cbs <- function(btd, cbs) {
@@ -317,13 +310,18 @@ get_bilateral_trade <- function(file_path) {
317310
dplyr::pull(cbs, area_code)
318311
) |>
319312
unique() |>
320-
sort()
313+
sort() |>
314+
as.factor()
321315
}
322316

323317
.build_trade_matrix <- function(btd, codes) {
324318
btd |>
325-
tidyr::complete(from_code = codes, to_code = codes) |>
326-
tidyr::pivot_wider(names_from = to_code, values_from = value) |>
319+
tidyr::pivot_wider(
320+
names_from = to_code,
321+
values_from = value,
322+
names_expand = TRUE
323+
) |>
324+
tidyr::complete(from_code = codes) |>
327325
tibble::column_to_rownames(var = "from_code") |>
328326
as.matrix()
329327
}

R/commodity_balance_sheet.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ get_wide_cbs <- function(file_path) {
5454
readr::read_csv(show_col_types = FALSE) |>
5555
tidyr::pivot_wider(names_from = Element, values_from = Value) |>
5656
dplyr::rename_with(tolower) |>
57-
dplyr::mutate(stock_retrieval = -stock_variation, .keep = "unused")
57+
dplyr::mutate(
58+
stock_retrieval = -stock_variation,
59+
dplyr::across(c(year, area_code), as.integer),
60+
.keep = "unused"
61+
)
5862
}
5963

6064
#' Processed products share factors

R/input_files.R

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
#'
33
#' @description
44
#' If the requested file doesn't exist locally, it is downloaded from a public
5-
#' Google Drive link and stored in a cache directory obtained using
6-
#' `tools::R_user_dir`.
5+
#' link and stored in a cache directory obtained using `tools::R_user_dir`.
76
#'
87
#' @param file_alias Alias of the requested file. For now the possible
98
#' values are:
@@ -47,13 +46,13 @@ get_file_path <- function(file_alias, force_download = FALSE) {
4746
tryCatch(
4847
{
4948
message(stringr::str_glue("Downloading {file_info$alias}..."))
50-
.download_from_drive(file_info$drive_file_id, destfile)
49+
.download_from_somewhere(file_info, destfile)
5150
},
5251
error = function(cond) {
5352
if (file.exists(destfile)) {
5453
file.remove(destfile)
5554
}
56-
stop("File was not downloaded correctly. Try again.")
55+
stop(cond)
5756
}
5857
)
5958
destfile
@@ -70,6 +69,21 @@ get_file_path <- function(file_alias, force_download = FALSE) {
7069
)
7170
}
7271

72+
.download_from_somewhere <- function(file_info, destfile) {
73+
if (!is.na(file_info$drive_file_id)) {
74+
.download_from_drive(file_info$drive_file_id, destfile)
75+
} else {
76+
.download_from_any_url(file_info$file_url, destfile)
77+
}
78+
}
79+
80+
.download_from_any_url <- function(file_url, destfile) {
81+
file_url |>
82+
httr2::request() |>
83+
httr2::req_progress() |>
84+
httr2::req_perform(destfile)
85+
}
86+
7387
.download_from_drive <- function(drive_file_id, destfile) {
7488
googledrive::drive_deauth()
7589
googledrive::drive_download(
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
extension,alias,drive_file_id
2-
csv,commodity_balance_sheet,1N6N4DI3PDowKvWwreFbcSe3YwYjV24k9
3-
csv,bilateral_trade,1WzMpkBzou8sCJZg1SWh-Z-6iwsrVoWtw
4-
csv,processing_coefs,1VrZ8rJxMcFZfw5PBjnEHYiTRa3usWb0H
1+
extension,alias,drive_file_id,file_url
2+
csv,commodity_balance_sheet,,"https://saco.csic.es/public.php/dav/files/nrJ3JGPZyZeQMW8/Sources%20(proposed%20by%20alvaro)/Model%20inputs/New%20FABIO/commodity_balance_sheet.csv"
3+
csv,bilateral_trade,,"https://saco.csic.es/public.php/dav/files/nrJ3JGPZyZeQMW8/Sources%20(proposed%20by%20alvaro)/Model%20inputs/New%20FABIO/bilateral_trade.csv"
4+
csv,processing_coefs,,"https://saco.csic.es/public.php/dav/files/nrJ3JGPZyZeQMW8/Sources%20(proposed%20by%20alvaro)/Model%20inputs/New%20FABIO/processing_coefs.csv"
5+
csv,example_cbs_drive,1N6N4DI3PDowKvWwreFbcSe3YwYjV24k9,

man/get_file_path.Rd

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)