Skip to content
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

Batch download of series #23

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export(bde_parse_dates)
export(bde_rose_pal)
export(bde_series_full_load)
export(bde_series_load)
export(bde_themes_download)
export(bde_vivid_pal)
export(scale_color_bde_c)
export(scale_color_bde_d)
Expand Down
139 changes: 139 additions & 0 deletions R/thematic.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
#' Download thematic classified files
#'
#' Download several series based on the thematic classification of BdE.
#'
#' @export
#'
#' @family series
#'
#'
#' @encoding UTF-8
#'
#' @param themes A single value indicating the themes to be downloaded
#' or `"ALL"` as a shorthand. See **Details**.
#'
#'
#' @inheritParams bde_series_full_load
#'
#'
#' @return Invisible. This function is called for its side effects.
#'
#' @source [Time-series bulk data download](https://www.bde.es/webbde/en/estadis/infoest/descarga_series_temporales.html)
#'
#' @description
#'
#' By the time, this function just batch-download all the series included
#' in the
#' [Theme-based classification](https://www.bde.es/webbde/en/estadis/infoest/descarga_series_temporales.html)
#' provided by Bank of Spain. However, this is useful as the function
#' [bde_series_load_all()] and [bde_series_load()] would search also in the
#' downloaded files, providing a faster response to the end user.
#'
#' @seealso [bde_series_load_all()]
#'
#'
#' @details
#' Accepted values for `themes` are:
#'
#' ```{r, echo=FALSE}
#'
#' t <- tibble::tribble(
#' ~themes, ~Description,
#' "**TE_TIPOS**", "Interest rate statistics",
#' "**TE_TIPOSCAM**", "Exchange rate statistics",
#' "**TE_CF**", "Financial accounts statistics",
#' "**TE_IFINA**", "Financial corporations statistics",
#' "**TE_DEU**", "General government statistics",
#' "**TE_CENBAL**", "Non-Financial corporations statistics",
#' "**TE_SECEXT**", "External statistics",
#' "**TE_MERCA**", "Financial market statistics",
#' "**TE_ESTECON**", "General economic statistics",
#' )
#'
#'
#' knitr::kable(t)

#'
#'
#' ```
#'
#' Use `"ALL"` as a shorthand for updating all the catalogs at a glance.
#'
#' If this function has been called, [bde_series_load_all()] and
#' [bde_series_load()] would try to find the series on the downloaded files.
#'
#' @examplesIf bde_check_access()
#' \dontrun{
#' bde_themes_download()
#' }
bde_themes_download <- function(themes = "ALL", cache_dir = NULL,
verbose = FALSE) {
# Validate
valid_themes <- c(
"TE_TIPOS", "TE_TIPOSCAM", "TE_CF",
"TE_IFINA", "TE_DEU", "TE_CENBAL",
"TE_SECEXT", "TE_MERCA", "TE_ESTECON",
"ALL"
)
stopifnot(
themes %in% valid_themes,
length(themes) == 1,
is.null(cache_dir) || is.character(cache_dir)
)

# nocov start
if (!bde_check_access()) {
message("tidyBdE> Offline")
return(invisible())
}
# nocov end

if (themes == "ALL") {
themes_batch <- setdiff(valid_themes, "ALL")
} else {
themes_batch <- themes
}

themes_batch <- paste0(themes_batch, ".zip")

# Helper function to download and unzip files
unzip_themes <- function(x, cache_dir = cache_dir,
verbose = verbose) {
baseurl <- "https://www.bde.es/webbde/es/estadis/infoest/series/"
fullurl <- paste0(baseurl, x)
cache_dir <- bde_hlp_cachedir(cache_dir)
local_zip <- file.path(cache_dir, x)


bde_hlp_download(fullurl, local_zip, verbose = verbose)

# Extract all files
exdir <- bde_hlp_cachedir(cache_dir = cache_dir, suffix = "TE")
if (verbose) {
message("tidyBdE> Extracting ", x, " content on ", exdir)
}
unzip(local_zip, exdir = exdir, junkpaths = FALSE)

if (verbose) {
message("tidyBdE> Removing ", local_zip)
}
unlink(local_zip)
return(NULL)
}

# Apply to all
r <- lapply(themes_batch, unzip_themes,
cache_dir = cache_dir,
verbose = verbose
)


if (verbose) {
exdir <- bde_hlp_cachedir(cache_dir, suffix = "TE")
nfiles <- length(list.files(exdir))

message("tidyBdE> Sucess! ", nfiles, " files available in ", exdir)
}

return(invisible())
}
17 changes: 8 additions & 9 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
@@ -1,41 +1,40 @@
Banco
BdE
CENBAL
CMD
CatastRo
Catastro
DESCRIPCIÓN
DEU
DOI
DTCCBCEGBPEUR
Datos
Descripcion
ESTECON
Económicos
España
Euribor
Eurostat
FRECUENCIA
GBP
Geográfico
Herrero
IFINA
IGN
INE
Indicadores
Instituto
LABORABLE
MERCA
MMM
MMMMYYYY
MicroDatosEs
NOMBRE
Nacional
Numero
NÚMERO
ORCID
Precompile
SECUENCIAL
SERIE
SECEXT
SSM
Sede
TIPOS
TIPOSCAM
Tipo
UNIDADES
YYYY
bde
cambio
Expand Down
3 changes: 2 additions & 1 deletion man/bde_series_full_load.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/bde_series_load.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 70 additions & 0 deletions man/bde_themes_download.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 18 additions & 18 deletions tests/testthat/test-catalogs.R
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
test_that("Catalogs", {
skip_on_cran()
skip_if_bde_offline()

# Test load catalogs----
expect_error(bde_catalog_load("aa"))
expect_error(bde_catalog_search("xxxxxxx", catalog = "IE"))


expect_message(bde_catalog_load("TC", cache_dir = tempdir(), verbose = TRUE))
expect_message(bde_catalog_load("TC", cache_dir = NULL, verbose = TRUE))
expect_message(bde_catalog_load(
"TC",
cache_dir = file.path(tempdir(), "aa"),
verbose = TRUE
))

expect_silent(bde_catalog_load("ALL"))



# Test update catalogs----

expect_error(bde_catalog_update("aa"))


expect_message(bde_catalog_update("TC",
cache_dir = tempdir(),
verbose = TRUE
cache_dir = tempdir(),
verbose = TRUE
))
expect_silent(bde_catalog_update("ALL", cache_dir = tempdir()))
expect_silent(bde_catalog_update("TC", cache_dir = tempdir()))

# Testing options cache dir
init_cache_dir <- getOption("bde_cache_dir")
options(bde_cache_dir = file.path(tempdir(), "test"))
expect_message(bde_catalog_update("TC", verbose = TRUE))
# Reset
options(bde_cache_dir = init_cache_dir)


# Test bde_catalog_search----

expect_error(bde_catalog_search())


expect_silent(bde_catalog_search("Euro", catalog = "TC"))
})

Expand Down
17 changes: 17 additions & 0 deletions tests/testthat/test-thematic.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
test_that("Thematic", {
skip_on_cran()
skip_if_bde_offline()

# Test load catalogs----
expect_error(bde_themes_download("aa"))

expect_message(bde_themes_download("TE_DEU",
cache_dir = tempdir(),
verbose = TRUE
))
expect_message(bde_themes_download("TE_DEU",
cache_dir = NULL,
verbose = TRUE
))
expect_silent(bde_themes_download("ALL"))
})