Commit 9a199aab authored by Sébastien Galais's avatar Sébastien Galais
Browse files

Use of 'metadata' from the API to download less data. Change email of creator. CRAN release v0.4.7.

parent da822b5f
Pipeline #39042 canceled with stage
in 0 seconds
Package: rdbnomics
Type: Package
Title: Download DBnomics Data
Version: 0.4.6
Version: 0.4.7
Authors@R: c(person("Thomas", "Brand", role = c("aut"),
email = "thomas.brand@cepremap.org"),
person("Sebastien", "Galais", role = c("cre", "ctb"),
email = "Sebastien.GALAIS@banque-france.fr"))
email = "s915.stem@gmail.com"))
Description: R access to hundreds of millions data series from DBnomics API
(<https://db.nomics.world/>).
Depends:
......
# rdbnomics 0.4.7
* Use of `metadata` from the API to download less data.
# rdbnomics 0.4.6
* Internal function `deploy` better handles recursive lists.
......
......@@ -29,7 +29,7 @@
#' @param mask Character string (default \code{NULL}). DBnomics code of one or
#' several masks in the specified provider and dataset.
#' @param verbose Logical (default \code{TRUE}). Show warnings of the function.
#' @param ... Arguments to be passed to \code{\link{rdb_by_api_link}}. These
#' @param ... Arguments to be passed to \code{\link{rdb_by_api_link}}. These
#' arguments concern connection configuration. See \code{\link{rdb_by_api_link}}
#' for details.
#' @return A \code{data.frame} or a \code{data.table}.
......@@ -40,25 +40,25 @@
#' df1 <- rdb(ids = 'AMECO/ZUTN/EA19.1.0.0.0.ZUTN')
#' # or when no argument names are given (provider_code -> ids)
#' df1 <- rdb('AMECO/ZUTN/EA19.1.0.0.0.ZUTN')
#'
#'
#' # Fetch two series from dataset 'Unemployment rate' (ZUTN) of AMECO provider :
#' df2 <- rdb(ids = c('AMECO/ZUTN/EA19.1.0.0.0.ZUTN', 'AMECO/ZUTN/DNK.1.0.0.0.ZUTN'))
#'
#'
#' # Fetch two series from different datasets of different providers :
#' df3 <- rdb(ids = c('AMECO/ZUTN/EA19.1.0.0.0.ZUTN', 'IMF/CPI/A.AT.PCPIT_IX'))
#'
#'
#'
#'
#' ## By dimensions
#' # Fetch one value of one dimension from dataset 'Unemployment rate' (ZUTN) of AMECO provider :
#' df1 <- rdb('AMECO', 'ZUTN', dimensions = list(geo = "ea12"))
#' # or
#' df1 <- rdb('AMECO', 'ZUTN', dimensions = '{"geo": ["ea12"]}')
#'
#'
#' # Fetch two values of one dimension from dataset 'Unemployment rate' (ZUTN) of AMECO provider :
#' df2 <- rdb('AMECO', 'ZUTN', dimensions = list(geo = c("ea12", "dnk")))
#' # or
#' df2 <- rdb('AMECO', 'ZUTN', dimensions = '{"geo": ["ea12", "dnk"]}')
#'
#'
#' # Fetch several values of several dimensions from dataset 'Doing business' (DB) of World Bank :
#' dim <- list(
#' country = c("DZ", "PE"),
......@@ -71,24 +71,24 @@
#' '"indicator": ["ENF.CONT.COEN.COST.ZS", "IC.REG.COST.PC.FE.ZS"]}'
#' )
#' df3 <- rdb('WB', 'DB', dimensions = dim)
#'
#'
#'
#'
#' ## By mask
#' # Fetch one series from dataset 'Consumer Price Index' (CPI) of IMF :
#' df1 <- rdb('IMF', 'CPI', mask = 'M.DE.PCPIEC_WT')
#' # or when no argument names are given except provider_code and dataset_code (ids -> mask)
#' df1 <- rdb('IMF', 'CPI', 'M.DE.PCPIEC_WT')
#'
#'
#' # Fetch two series from dataset 'Consumer Price Index' (CPI) of IMF :
#' df2 <- rdb('IMF', 'CPI', mask = 'M.DE+FR.PCPIEC_WT')
#'
#'
#' # Fetch all series along one dimension from dataset 'Consumer Price Index' (CPI) of IMF :
#' df3 <- rdb('IMF', 'CPI', mask = 'M..PCPIEC_WT')
#'
#'
#' # Fetch series along multiple dimensions from dataset 'Consumer Price Index' (CPI) of IMF :
#' df4 <- rdb('IMF', 'CPI', mask = 'M..PCPIEC_IX+PCPIA_IX')
#'
#'
#'
#'
#' ## Use a specific proxy to fetch the data
#' # Fetch one series from dataset 'Unemployment rate' (ZUTN) of AMECO provider :
#' h <- curl::new_handle(
......@@ -101,8 +101,8 @@
#' df1 <- rdb(ids = 'AMECO/ZUTN/EA19.1.0.0.0.ZUTN')
#' # or to use once
#' df1 <- rdb(ids = 'AMECO/ZUTN/EA19.1.0.0.0.ZUTN', curl_config = h)
#'
#'
#'
#'
#' ## Use R default connection to avoid a proxy failure (in some cases)
#' # Fetch one series from dataset 'Unemployment rate' (ZUTN) of AMECO provider :
#' options(rdbnomics.use_readLines = TRUE)
......@@ -130,6 +130,12 @@ rdb <- function(
check_argument(api_version, c("numeric", "integer"))
authorized_version(api_version)
# Setting API metadata
metadata <- getOption("rdbnomics.metadata")
check_argument(metadata, "logical")
metadata <- as.numeric(metadata)
# Building API base url
api_base_url <- paste0(api_base_url, "/v", api_version, "/series")
# Checking arguments
......@@ -141,10 +147,10 @@ rdb <- function(
dimensions_null <- is.null(dimensions)
dimensions_not_null <- !dimensions_null
mask_null <- is.null(mask)
mask_not_null <- !mask_null
ids_null <- is.null(ids)
ids_not_null <- !ids_null
......@@ -179,10 +185,10 @@ rdb <- function(
if (modif_arg) {
mask <- ids
ids <- NULL
mask_null <- FALSE
mask_not_null <- !mask_null
ids_null <- TRUE
ids_not_null <- !ids_null
}
......@@ -213,7 +219,7 @@ rdb <- function(
} else if (api_version == 22) {
link <- paste0(
api_base_url, "/", provider_code, "/", dataset_code,
"?observations=1&dimensions=", dimensions
"?metadata=", metadata, "&observations=1&dimensions=", dimensions
)
} else {
stop(
......@@ -249,7 +255,7 @@ rdb <- function(
} else if (api_version == 22) {
link <- paste0(
api_base_url, "/", provider_code, "/", dataset_code,
"/", mask, "?observations=1"
"/", mask, "?metadata=", metadata, "&observations=1"
)
} else {
stop(
......@@ -284,10 +290,11 @@ rdb <- function(
if (api_version == 21) {
link <- paste0(
api_base_url, "?series_ids=", paste(ids, collapse = ",")
)
)
} else if (api_version == 22) {
link <- paste0(
api_base_url, "?observations=1&series_ids=", paste(ids, collapse = ",")
api_base_url, "?metadata=", metadata, "&observations=1&series_ids=",
paste(ids, collapse = ",")
)
} else {
stop(
......
......@@ -30,7 +30,7 @@
#' @examples
#' \dontrun{
#' rdb_last_updates()
#'
#'
#' rdb_last_updates(all = TRUE)
#' }
#' @seealso \code{\link{rdb_providers}}
......
......@@ -11,7 +11,7 @@ get_data <- function(x, userl, curl_args, run = 0) {
check_argument(sys_sleep, c("integer", "numeric"))
Sys.sleep(sys_sleep)
}
tryCatch({
if (userl) {
# Only readLines
......@@ -20,7 +20,7 @@ get_data <- function(x, userl, curl_args, run = 0) {
suppressMessages(suppressWarnings(utils::setInternet2(TRUE)))
}
}
verb_warn_rl <- getOption("rdbnomics.verbose_warning_readLines")
check_argument(verb_warn_rl, "logical")
if (verb_warn_rl) {
......@@ -80,7 +80,7 @@ get_data <- function(x, userl, curl_args, run = 0) {
}, error = function(e) {
try_run <- getOption("rdbnomics.try_run")
check_argument(try_run, c("integer", "numeric"))
myerror <- try(
grepl("'curl_config'", e$message) |
grepl("BAD[[:blank:]]+REQUEST", toupper(e$message)),
......@@ -134,7 +134,7 @@ deploy <- function(DT, columns = NULL, reference_column = "value") {
if (length(v) == 1) {
# New col
y[[iv]] <- paste0(trim(v), ",")
} else if ((length(v) == to_list_length + 1) | (length(v) == 2)) {
} else if ( (length(v) == to_list_length + 1) | (length(v) == 2) ) {
# New col
y[[iv]] <- paste0(trim(v[1]), ",", utils::tail(v, -1))
} else if (length(v) != to_list_length) {
......@@ -239,7 +239,7 @@ authorized_version <- function(x) {
if (length(versions) <= 0) {
stop(paste0(name, " must be of length greater than 0."), call. = FALSE)
}
if (x %notin% versions) {
stop(
paste0(
......
......@@ -32,7 +32,8 @@
rdbnomics.timestamp_tz = "GMT",
rdbnomics.http_ok = "200[[:blank:]]+OK$",
rdbnomics.curl_config = NULL,
rdbnomics.rdb_no_arg = TRUE
rdbnomics.rdb_no_arg = TRUE,
rdbnomics.metadata = FALSE
)
opts <- append(
opts,
......@@ -43,6 +44,6 @@
op <- options()
op.rdbnomics <- opts
toset <- !(names(op.rdbnomics) %in% names(op))
if(any(toset)) options(op.rdbnomics[toset])
if (any(toset)) options(op.rdbnomics[toset])
invisible()
}
# rdbnomics <img src="man/figures/logo.png" align="right" width="120" />
[![pipeline status](https://git.nomics.world/dbnomics/rdbnomics/badges/master/pipeline.svg)](https://git.nomics.world/dbnomics/rdbnomics/commits/master)
[![CRAN\_Status\_Badge](http://www.r-pkg.org/badges/version/rdbnomics)](https://cran.r-project.org/package=rdbnomics)
[![pipeline status](https://git.nomics.world/dbnomics/rdbnomics/badges/master/pipeline.svg)](https://git.nomics.world/dbnomics/rdbnomics/commits/master)
[![status](https://tinyverse.netlify.com/badge/rdbnomics)](https://CRAN.R-project.org/package=rdbnomics)
## DBnomics R client
......
......@@ -28,7 +28,7 @@ several masks in the specified provider and dataset.}
\item{verbose}{Logical (default \code{TRUE}). Show warnings of the function.}
\item{...}{Arguments to be passed to \code{\link{rdb_by_api_link}}. These
\item{...}{Arguments to be passed to \code{\link{rdb_by_api_link}}. These
arguments concern connection configuration. See \code{\link{rdb_by_api_link}}
for details.}
}
......
context("Right arguments for rdb_last_updates")
library(rdbnomics)
test_that("all is logical and of length one", {
test_that("all is logical and of length one", {
expect_error(rdb_last_updates(all = logical()))
expect_error(rdb_last_updates(all = "TRUE"))
})
context("Right arguments for rdb_providers")
library(rdbnomics)
test_that("code is logical and of length one", {
test_that("code is logical and of length one", {
expect_error(rdb_providers(code = logical()))
expect_error(rdb_providers(code = "TRUE"))
})
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment