Commit 96b5ff0e authored by Sébastien Galais's avatar Sébastien Galais

New argument 'query' for function rdb()

parent 5ce7cb96
Pipeline #105051 passed with stage
in 12 minutes and 47 seconds
Package: rdbnomics
Type: Package
Title: Download DBnomics Data
Version: 0.5.0
Version: 0.5.1
Authors@R: c(person("Sebastien", "Galais", role = c("cre", "ctb"),
email = "s915.stem@gmail.com"),
person("Thomas", "Brand", role = c("aut"),
......
# Generated by roxygen2: do not edit by hand
export(dbnomics)
export(rdb)
export(rdb_by_api_link)
export(rdb_last_updates)
......
# rdbnomics 0.5.1
* New argument 'query' for function `rdb()`.
* Small class correction for R 3.1.
# rdbnomics 0.5.0
* New filters tool from <https://editor.nomics.world/filters>.
......
#' DBnomics ggplot2 theme
#'
#' \code{dbnomics} is a simple ggplot2 theme for drawing nicer graphs. We do not
#' recommend to use it. It has been included in the package to avoid errors
#' from the vignette examples.
#'
#' @param ... Arguments to be passed to \code{theme}.
#'
#' @examples
#' \dontrun{
#' library(ggplot2)
#'
#' rdb("IMF", "WEO", query = "France current account balance percent") %>%
#' ggplot(aes(x = period, y = value, color = series_name)) +
#' geom_line(size = 1.2) +
#' geom_point(size = 2) +
#' dbnomics()
#' }
#' @export
dbnomics <- function(...) {
list(
ggplot2::scale_x_date(expand = c(0, 0)),
ggplot2::scale_y_continuous(labels = function(x) { format(x, big.mark = " ") }),
ggplot2::xlab(""),
ggplot2::ylab(""),
ggplot2::theme_bw(),
ggplot2::theme(
legend.position = "bottom", legend.direction = "vertical",
legend.background = ggplot2::element_rect(fill = "transparent", colour = NA),
legend.key = ggplot2::element_blank(),
panel.background = ggplot2::element_rect(fill = "transparent", colour = NA),
plot.background = ggplot2::element_rect(fill = "transparent", colour = NA),
legend.title = ggplot2::element_blank()
),
ggplot2::theme(...),
ggplot2::annotate(
geom = "text", label = "DBnomics <https://db.nomics.world>",
x = structure(Inf, class = "Date"), y = -Inf,
hjust = 1.1, vjust = -0.4, col = "grey",
fontface = "italic"
)
)
}
\ No newline at end of file
......@@ -28,6 +28,8 @@
#' package \pkg{jsonlite}) is applied to generate the json object.
#' @param mask Character string (default \code{NULL}). DBnomics code of one or
#' several masks in the specified provider and dataset.
#' @param query Character string (default \code{NULL}). A query to
#' filter/select series from a provider's dataset.
#' @param filters List (default \code{NULL}). This argument must be a named
#' list for one filter because the function \code{toJSON} of the package \pkg{jsonlite}
#' is used before sending the request to the server. For multiple filters,
......@@ -44,58 +46,65 @@
#' \dontrun{
#' ## By ids
#' # Fetch one series from dataset 'Unemployment rate' (ZUTN) of AMECO provider :
#' df1 <- rdb(ids = 'AMECO/ZUTN/EA19.1.0.0.0.ZUTN')
#' 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')
#' 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'))
#' 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'))
#' 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"))
#' df1 <- rdb("AMECO", "ZUTN", dimensions = list(geo = "ea12"))
#' # or
#' df1 <- rdb('AMECO', 'ZUTN', dimensions = '{"geo": ["ea12"]}')
#' 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")))
#' df2 <- rdb("AMECO", "ZUTN", dimensions = list(geo = c("ea12", "dnk")))
#' # or
#' df2 <- rdb('AMECO', 'ZUTN', dimensions = '{"geo": ["ea12", "dnk"]}')
#' 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"),
#' indicator = c("ENF.CONT.COEN.COST.ZS", "IC.REG.COST.PC.FE.ZS")
#' )
#' df3 <- rdb('WB', 'DB', dimensions = dim)
#' df3 <- rdb("WB", "DB", dimensions = dim)
#' # or
#' dim <- paste0(
#' '{"country": ["DZ", "PE"],',
#' '"indicator": ["ENF.CONT.COEN.COST.ZS", "IC.REG.COST.PC.FE.ZS"]}'
#' )
#' df3 <- rdb('WB', 'DB', dimensions = dim)
#' 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')
#' 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')
#' 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')
#' 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')
#' 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')
#' df4 <- rdb("IMF", "CPI", mask = "M..PCPIEC_IX+PCPIA_IX")
#'
#'
#' ## By query
#' # Fetch one series from dataset 'WEO by countries' (WEO) from IMF :
#' df1 <- rdb("IMF", "WEO", query = "France current account balance percent")
#' # Fetch series from dataset 'WEO by countries' (WEO) from IMF :
#' df2 <- rdb("IMF", "WEO", query = "current account balance percent")
#'
#'
#' ## Use a specific proxy to fetch the data
#' # Fetch one series from dataset 'Unemployment rate' (ZUTN) of AMECO provider :
#' h <- list(
......@@ -105,18 +114,18 @@
#' proxypassword = "<password>"
#' )
#' options(rdbnomics.curl_config = h)
#' df1 <- rdb(ids = 'AMECO/ZUTN/EA19.1.0.0.0.ZUTN')
#' df1 <- rdb(ids = "AMECO/ZUTN/EA19.1.0.0.0.ZUTN")
#' # or to use once
#' options(rdbnomics.curl_config = NULL)
#' df1 <- rdb(ids = 'AMECO/ZUTN/EA19.1.0.0.0.ZUTN', curl_config = h)
#' 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)
#' df1 <- rdb(ids = 'AMECO/ZUTN/EA19.1.0.0.0.ZUTN')
#' 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', use_readLines = TRUE)
#' df1 <- rdb(ids = "AMECO/ZUTN/EA19.1.0.0.0.ZUTN", use_readLines = TRUE)
#'
#'
#' ## Apply filter(s) to the series
......@@ -148,7 +157,7 @@
#' @export
rdb <- function(
provider_code = NULL, dataset_code = NULL,
ids = NULL, dimensions = NULL, mask = NULL,
ids = NULL, dimensions = NULL, mask = NULL, query = NULL,
filters = getOption("rdbnomics.filters"),
verbose = getOption("rdbnomics.verbose_warning"),
...
......@@ -188,6 +197,9 @@ rdb <- function(
ids_null <- is.null(ids)
ids_not_null <- !ids_null
query_null <- is.null(query)
query_not_null <- !query_null
# provider_code is considered as ids in some cases
if (
provider_code_not_null & dataset_code_null & dimensions_null & mask_null &
......@@ -345,8 +357,51 @@ rdb <- function(
return(rdb_by_api_link(api_link = link, filters = filters, ...))
}
# By query
if (query_not_null) {
if (provider_code_null | dataset_code_null) {
stop(
paste0(
"When you filter with a 'query', you must specifiy 'provider_code' ",
"and 'dataset_code' as arguments of the function."
),
call. = FALSE
)
}
check_argument(query, "character", not_null = FALSE)
if (verbose) {
if (query == "") {
warning(
paste0(
"Your 'query' is empty, the entire dataset ",
provider_code, "/", dataset_code,
" will be downloaded. It can be long !"
)
)
}
}
if (api_version == 22) {
link <- paste0(
api_base_url, "/", provider_code, "/", dataset_code,
"?q=", utils::URLencode(query),
ifelse(metadata, "&", paste0("&metadata=", as.numeric(metadata), "&")),
"observations=1"
)
} else {
stop(
paste0("Don't know what to do for API version ", api_version, "."),
call. = FALSE
)
}
return(rdb_by_api_link(api_link = link, filters = filters, ...))
}
stop(
"Please provide correct 'dimensions', 'mask', 'ids' or 'filters'.",
"Please provide correct 'dimensions', 'mask', 'ids', 'query' or 'filters'.",
call. = FALSE
)
}
......@@ -36,17 +36,17 @@
#' # Fetch two series from different datasets of different providers :
#' df1 <- rdb_by_api_link(
#' paste0(
#' 'https://api.db.nomics.world/v22/',
#' 'series?observations=1&series_ids=AMECO/ZUTN/EA19.1.0.0.0.ZUTN,IMF/CPI/A.AT.PCPIT_IX'
#' "https://api.db.nomics.world/v22/",
#' "series?observations=1&series_ids=AMECO/ZUTN/EA19.1.0.0.0.ZUTN,IMF/CPI/A.AT.PCPIT_IX"
#' )
#' )
#'
#' # Fetch one series from the dataset 'Doing Business' of WB provider :
#' df2 <- rdb_by_api_link(
#' paste0(
#' 'https://api.db.nomics.world/v22/series/WB/DB?dimensions=%7B%22',
#' 'indicator%22%3A%5B%22IC.REG.PROC.FE.NO%22%5D%7D&q=Doing%20Business',
#' '&observations=1&format=json&align_periods=1&offset=0&facets=0'
#' "https://api.db.nomics.world/v22/series/WB/DB?dimensions=%7B%22",
#' "indicator%22%3A%5B%22IC.REG.PROC.FE.NO%22%5D%7D&q=Doing%20Business",
#' "&observations=1&format=json&align_periods=1&offset=0&facets=0"
#' )
#' )
#'
......@@ -62,17 +62,17 @@
#' options(rdbnomics.curl_config = h)
#' df2 <- rdb_by_api_link(
#' paste0(
#' 'https://api.db.nomics.world/v22/series/WB/DB?dimensions=%7B%22',
#' 'indicator%22%3A%5B%22IC.REG.PROC.FE.NO%22%5D%7D&q=Doing%20Business',
#' '&observations=1&format=json&align_periods=1&offset=0&facets=0'
#' "https://api.db.nomics.world/v22/series/WB/DB?dimensions=%7B%22",
#' "indicator%22%3A%5B%22IC.REG.PROC.FE.NO%22%5D%7D&q=Doing%20Business",
#' "&observations=1&format=json&align_periods=1&offset=0&facets=0"
#' )
#' )
#' # or to use once
#' df2 <- rdb_by_api_link(
#' paste0(
#' 'https://api.db.nomics.world/v22/series/WB/DB?dimensions=%7B%22',
#' 'indicator%22%3A%5B%22IC.REG.PROC.FE.NO%22%5D%7D&q=Doing%20Business',
#' '&observations=1&format=json&align_periods=1&offset=0&facets=0'
#' "https://api.db.nomics.world/v22/series/WB/DB?dimensions=%7B%22",
#' "indicator%22%3A%5B%22IC.REG.PROC.FE.NO%22%5D%7D&q=Doing%20Business",
#' "&observations=1&format=json&align_periods=1&offset=0&facets=0"
#' ),
#' curl_config = h
#' )
......@@ -83,17 +83,17 @@
#' options(rdbnomics.use_readLines = TRUE)
#' df2 <- rdb_by_api_link(
#' paste0(
#' 'https://api.db.nomics.world/v22/series/WB/DB?dimensions=%7B%22',
#' 'indicator%22%3A%5B%22IC.REG.PROC.FE.NO%22%5D%7D&q=Doing%20Business',
#' '&observations=1&format=json&align_periods=1&offset=0&facets=0'
#' "https://api.db.nomics.world/v22/series/WB/DB?dimensions=%7B%22",
#' "indicator%22%3A%5B%22IC.REG.PROC.FE.NO%22%5D%7D&q=Doing%20Business",
#' "&observations=1&format=json&align_periods=1&offset=0&facets=0"
#' )
#' )
#' # or to use once
#' df2 <- rdb_by_api_link(
#' paste0(
#' 'https://api.db.nomics.world/v22/series/WB/DB?dimensions=%7B%22',
#' 'indicator%22%3A%5B%22IC.REG.PROC.FE.NO%22%5D%7D&q=Doing%20Business',
#' '&observations=1&format=json&align_periods=1&offset=0&facets=0'
#' "https://api.db.nomics.world/v22/series/WB/DB?dimensions=%7B%22",
#' "indicator%22%3A%5B%22IC.REG.PROC.FE.NO%22%5D%7D&q=Doing%20Business",
#' "&observations=1&format=json&align_periods=1&offset=0&facets=0"
#' ),
#' use_readLines = TRUE
#' )
......@@ -102,7 +102,7 @@
#' ## Apply filter(s) to the series
#' # One filter
#' df3 <- rdb_by_api_link(
#' 'https://api.db.nomics.world/v22/series/IMF/WEO/ABW.BCA?observations=1',
#' "https://api.db.nomics.world/v22/series/IMF/WEO/ABW.BCA?observations=1",
#' filters = list(
#' code = "interpolate",
#' parameters = list(frequency = "daily", method = "spline")
......@@ -111,7 +111,7 @@
#'
#' # Two filters
#' df3 <- rdb_by_api_link(
#' 'https://api.db.nomics.world/v22/series/IMF/WEO/ABW.BCA?observations=1',
#' "https://api.db.nomics.world/v22/series/IMF/WEO/ABW.BCA?observations=1",
#' filters = list(
#' list(
#' code = "interpolate",
......@@ -487,6 +487,12 @@ rdb_by_api_link <- function(
DBdata[, filtered := FALSE]
DBlist[, filtered := TRUE]
# For R 3.1
diff_cols <- setdiff(colnames(DBlist), colnames(DBdata))
if (length(diff_cols) > 0) {
DBdata[, period_middle_day := as.Date(NA)]
}
DBdata <- list(DBdata, DBlist)
DBdata <- data.table::rbindlist(DBdata, use.names = TRUE, fill = TRUE)
}
......
......@@ -12,7 +12,8 @@ NULL
if (getRversion() >= "2.15.1") {
vars <- c(
".", ":=", "value", "dotI", "period", "period_start_day", "series_code",
"filtered", "original_period", "series_name", "original_value"
"filtered", "original_period", "series_name", "original_value",
"period_middle_day"
)
utils::globalVariables(unique(vars))
}
\ No newline at end of file
No preview for this file type
......@@ -35,61 +35,70 @@ vignette("rdbnomics")
Fetch time series by `ids`:
```r
# Fetch one series from dataset 'Unemployment rate' (ZUTN) of AMECO provider:
df1 <- rdb(ids = 'AMECO/ZUTN/EA19.1.0.0.0.ZUTN')
df1 <- rdb(ids = "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'))
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'))
df3 <- rdb(ids = c("AMECO/ZUTN/EA19.1.0.0.0.ZUTN", "IMF/CPI/A.AT.PCPIT_IX"))
```
In the event that you only use the argument `ids`, you can drop it and run:
```r
df <- rdb('AMECO/ZUTN/EA19.1.0.0.0.ZUTN')
df <- rdb("AMECO/ZUTN/EA19.1.0.0.0.ZUTN")
```
Fetch time series by `mask` :
```r
# Fetch one series from dataset 'Consumer Price Index' (CPI) of IMF:
df1 <- rdb('IMF', 'CPI', mask = 'M.DE.PCPIEC_WT')
df1 <- rdb("IMF", "CPI", mask = "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')
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')
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')
df4 <- rdb("IMF", "CPI", mask = "M..PCPIEC_IX+PCPIA_IX")
```
In the event that you only use the arguments `provider_code`, `dataset_code` and `mask`, you can drop the name `mask` and run:
```r
df <- rdb('IMF', 'CPI', 'M.DE.PCPIEC_WT')
df <- rdb("IMF", "CPI", "M.DE.PCPIEC_WT")
```
Fetch time series by `dimensions`:
```r
# Fetch one value of one dimension from dataset 'Unemployment rate' (ZUTN) of AMECO provider:
df1 <- rdb('AMECO', 'ZUTN', dimensions = list(geo = "ea12"))
df1 <- rdb("AMECO", "ZUTN", dimensions = list(geo = "ea12"))
# or
df1 <- rdb('AMECO', 'ZUTN', dimensions = '{"geo": ["ea12"]}')
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")))
df2 <- rdb("AMECO", "ZUTN", dimensions = list(geo = c("ea12", "dnk")))
# or
df2 <- rdb('AMECO', 'ZUTN', dimensions = '{"geo": ["ea12", "dnk"]}')
df2 <- rdb("AMECO", "ZUTN", dimensions = '{"geo": ["ea12", "dnk"]}')
# Fetch several values of several dimensions from dataset 'Doing business' (DB) of World Bank:
df3 <- rdb('WB', 'DB', dimensions = list(country = c("DZ", "PE"), indicator = c("ENF.CONT.COEN.COST.ZS", "IC.REG.COST.PC.FE.ZS")))
df3 <- rdb("WB", "DB", dimensions = list(country = c("DZ", "PE"), indicator = c("ENF.CONT.COEN.COST.ZS", "IC.REG.COST.PC.FE.ZS")))
# or
df3 <- rdb('WB', 'DB', dimensions = '{"country": ["DZ", "PE"], "indicator": ["ENF.CONT.COEN.COST.ZS", "IC.REG.COST.PC.FE.ZS"]}')
df3 <- rdb("WB", "DB", dimensions = '{"country": ["DZ", "PE"], "indicator": ["ENF.CONT.COEN.COST.ZS", "IC.REG.COST.PC.FE.ZS"]}')
```
Fetch time series with a `query`:
```r
# Fetch one series from dataset 'WEO by countries' (WEO) of IMF provider:
df1 <- rdb("IMF", "WEO", query = "France current account balance percent")
# Fetch series from dataset 'WEO by countries' (WEO) of IMF provider:
df2 <- rdb("IMF", "WEO", query = "current account balance percent")
```
Fetch one series from the dataset 'Doing Business' of WB provider with the link:
```r
df1 <- rdb_by_api_link('https://api.db.nomics.world/v22/series/WB/DB?dimensions=%7B%22country%22%3A%5B%22FR%22%2C%22IT%22%2C%22ES%22%5D%7D&q=IC.REG.PROC.FE.NO&observations=1&format=json&align_periods=1&offset=0&facets=0')
df1 <- rdb_by_api_link("https://api.db.nomics.world/v22/series/WB/DB?dimensions=%7B%22country%22%3A%5B%22FR%22%2C%22IT%22%2C%22ES%22%5D%7D&q=IC.REG.PROC.FE.NO&observations=1&format=json&align_periods=1&offset=0&facets=0")
```
## Proxy configuration or connection error `Could not resolve host`
......@@ -130,7 +139,7 @@ curl::curl_fetch_memory(url = <...>, handle = hndl)
```
After configuration, just use the standard functions of **rdbnomics** e.g.:
```r
df1 <- rdb(ids = 'AMECO/ZUTN/EA19.1.0.0.0.ZUTN')
df1 <- rdb(ids = "AMECO/ZUTN/EA19.1.0.0.0.ZUTN")
```
This option of the package can be disabled with:
```r
......@@ -140,7 +149,7 @@ options(rdbnomics.curl = NULL)
#### Use the connection only for a function call
If a complete configuration is not needed but just an "on the fly" execution, then use the argument `curl_config` of the functions `rdb` and `rdb_...`:
```r
df1 <- rdb(ids = 'AMECO/ZUTN/EA19.1.0.0.0.ZUTN', curl_config = h)
df1 <- rdb(ids = "AMECO/ZUTN/EA19.1.0.0.0.ZUTN", curl_config = h)
```
### Use the default R internet connection
......@@ -153,7 +162,7 @@ options(rdbnomics.use_readLines = TRUE)
```
And then use the standard function as follows:
```r
df1 <- rdb(ids = 'AMECO/ZUTN/EA19.1.0.0.0.ZUTN')
df1 <- rdb(ids = "AMECO/ZUTN/EA19.1.0.0.0.ZUTN")
```
This configuration can be disabled with:
```r
......@@ -163,7 +172,7 @@ options(rdbnomics.use_readLines = FALSE)
#### Use the connection only for a function call
If you just want to do it once, you may use the argument `use_readLines` of the functions `rdb` and `rdb_...`:
```r
df1 <- rdb(ids = 'AMECO/ZUTN/EA19.1.0.0.0.ZUTN', use_readLines = TRUE)
df1 <- rdb(ids = "AMECO/ZUTN/EA19.1.0.0.0.ZUTN", use_readLines = TRUE)
```
## Transform time series with filters
......
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/dbnomics.R
\name{dbnomics}
\alias{dbnomics}
\title{DBnomics ggplot2 theme}
\usage{
dbnomics(...)
}
\arguments{
\item{...}{Arguments to be passed to \code{theme}.}
}
\description{
\code{dbnomics} is a simple ggplot2 theme for drawing nicer graphs. We do not
recommend to use it. It has been included in the package to avoid errors
from the vignette examples.
}
\examples{
\dontrun{
library(ggplot2)
rdb("IMF", "WEO", query = "France current account balance percent") \%>\%
ggplot(aes(x = period, y = value, color = series_name)) +
geom_line(size = 1.2) +
geom_point(size = 2) +
dbnomics()
}
}
......@@ -5,7 +5,7 @@
\title{Download DBnomics data.}
\usage{
rdb(provider_code = NULL, dataset_code = NULL, ids = NULL,
dimensions = NULL, mask = NULL,
dimensions = NULL, mask = NULL, query = NULL,
filters = getOption("rdbnomics.filters"),
verbose = getOption("rdbnomics.verbose_warning"), ...)
}
......@@ -27,6 +27,9 @@ package \pkg{jsonlite}) is applied to generate the json object.}
\item{mask}{Character string (default \code{NULL}). DBnomics code of one or
several masks in the specified provider and dataset.}
\item{query}{Character string (default \code{NULL}). A query to
filter/select series from a provider's dataset.}
\item{filters}{List (default \code{NULL}). This argument must be a named
list for one filter because the function \code{toJSON} of the package \pkg{jsonlite}
is used before sending the request to the server. For multiple filters,
......@@ -66,56 +69,63 @@ last character string is automatically passed to \code{mask}.
\dontrun{
## By ids
# Fetch one series from dataset 'Unemployment rate' (ZUTN) of AMECO provider :
df1 <- rdb(ids = 'AMECO/ZUTN/EA19.1.0.0.0.ZUTN')
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')
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'))
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'))
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"))
df1 <- rdb("AMECO", "ZUTN", dimensions = list(geo = "ea12"))
# or
df1 <- rdb('AMECO', 'ZUTN', dimensions = '{"geo": ["ea12"]}')
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")))
df2 <- rdb("AMECO", "ZUTN", dimensions = list(geo = c("ea12", "dnk")))
# or
df2 <- rdb('AMECO', 'ZUTN', dimensions = '{"geo": ["ea12", "dnk"]}')
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"),
indicator = c("ENF.CONT.COEN.COST.ZS", "IC.REG.COST.PC.FE.ZS")
)
df3 <- rdb('WB', 'DB', dimensions = dim)
df3 <- rdb("WB", "DB", dimensions = dim)
# or
dim <- paste0(
'{"country": ["DZ", "PE"],',
'"indicator": ["ENF.CONT.COEN.COST.ZS", "IC.REG.COST.PC.FE.ZS"]}'
)
df3 <- rdb('WB', 'DB', dimensions = dim)
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')
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')
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')
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')
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')
df4 <- rdb("IMF", "CPI", mask = "M..PCPIEC_IX+PCPIA_IX")
## By query
# Fetch one series from dataset 'WEO by countries' (WEO) from IMF :
df1 <- rdb("IMF", "WEO", query = "France current account balance percent")
# Fetch series from dataset 'WEO by countries' (WEO) from IMF :
df2 <- rdb("IMF", "WEO", query = "current account balance percent")
## Use a specific proxy to fetch the data
......@@ -127,18 +137,18 @@ h <- list(
proxypassword = "<password>"
)
options(rdbnomics.curl_config = h)
df1 <- rdb(ids = 'AMECO/ZUTN/EA19.1.0.0.0.ZUTN')
df1 <- rdb(ids = "AMECO/ZUTN/EA19.1.0.0.0.ZUTN")
# or to use once
options(rdbnomics.curl_config = NULL)
df1 <- rdb(ids = 'AMECO/ZUTN/EA19.1.0.0.0.ZUTN', curl_config = h)
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)
df1 <- rdb(ids = 'AMECO/ZUTN/EA19.1.0.0.0.ZUTN')
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', use_readLines = TRUE)
df1 <- rdb(ids = "AMECO/ZUTN/EA19.1.0.0.0.ZUTN", use_readLines = TRUE)
## Apply filter(s) to the series
......
......@@ -54,17 +54,17 @@ The API link is given on the
# Fetch two series from different datasets of different providers :
df1 <- rdb_by_api_link(
paste0(
'https://api.db.nomics.world/v22/',
'series?observations=1&series_ids=AMECO/ZUTN/EA19.1.0.0.0.ZUTN,IMF/CPI/A.AT.PCPIT_IX'
"https://api.db.nomics.world/v22/",
"series?observations=1&series_ids=AMECO/ZUTN/EA19.1.0.0.0.ZUTN,IMF/CPI/A.AT.PCPIT_IX"
)
)
# Fetch one series from the dataset 'Doing Business' of WB provider :
df2 <- rdb_by_api_link(
paste0(
'https://api.db.nomics.world/v22/series/WB/DB?dimensions=\%7B\%22',
'indicator\%22\%3A\%5B\%22IC.REG.PROC.FE.NO\%22\%5D\%7D&q=Doing\%20Business',
'&observations=1&format=json&align_periods=1&offset=0&facets=0'
"https://api.db.nomics.world/v22/series/WB/DB?dimensions=\%7B\%22",
"indicator\%22\%3A\%5B\%22IC.REG.PROC.FE.NO\%22\%5D\%7D&q=Doing\%20Business",
"&observations=1&format=json&align_periods=1&offset=0&facets=0"
)
)
......@@ -80,17 +80,17 @@ h <- list(
options(rdbnomics.curl_config = h)
df2 <- rdb_by_api_link(
paste0(
'https://api.db.nomics.world/v22/series/WB/DB?dimensions=\%7B\%22',
'indicator\%22\%3A\%5B\%22IC.REG.PROC.FE.NO\%22\%5D\%7D&q=Doing\%20Business',
'&observations=1&format=json&align_periods=1&offset=0&facets=0'
"https://api.db.nomics.world/v22/series/WB/DB?dimensions=\%7B\%22",
"indicator\%22\%3A\%5B\%22IC.REG.PROC.FE.NO\%22\%5D\%7D&q=Doing\%20Business",
"&observations=1&format=json&align_periods=1&offset=0&facets=0"
)
)
# or to use once
df2 <- rdb_by_api_link(
paste0(
'https://api.db.nomics.world/v22/series/WB/DB?dimensions=\%7B\%22',
'indicator\%22\%3A\%5B\%22IC.REG.PROC.FE.NO\%22\%5D\%7D&q=Doing\%20Business',
'&observations=1&format=json&align_periods=1&offset=0&facets=0'
"https://api.db.nomics.world/v22/series/WB/DB?dimensions=\%7B\%22",
"indicator\%22\%3A\%5B\%22IC.REG.PROC.FE.NO\%22\%5D\%7D&q=Doing\%20Business",
"&observations=1&format=json&align_periods=1&offset=0&facets=0"
),
curl_config = h
)
......@@ -101,17 +101,17 @@ df2 <- rdb_by_api_link(
options(rdbnomics.use_readLines = TRUE)
df2 <- rdb_by_api_link(
paste0(
'https://api.db.nomics.world/v22/series/WB/DB?dimensions=\%7B\%22',
'indicator\%22\%3A\%5B\%22IC.REG.PROC.FE.NO\%22\%5D\%7D&q=Doing\%20Business',
'&observations=1&format=json&align_periods=1&offset=0&facets=0'
"https://api.db.nomics.world/v22/series/WB/DB?dimensions=\%7B\%22",
"indicator\%22\%3A\%5B\%22IC.REG.PROC.FE.NO\%22\%5D\%7D&q=Doing\%20Business",
"&observations=1&format=json&align_periods=1&offset=0&facets=0"
)
)
# or to use once
df2 <- rdb_by_api_link(
paste0(
'https://api.db.nomics.world/v22/series/WB/DB?dimensions=\%7B\%22',
'indicator\%22\%3A\%5B\%22IC.REG.PROC.FE.NO\%22\%5D\%7D&q=Doing\%20Business',
'&observations=1&format=json&align_periods=1&offset=0&facets=0'
"https://api.db.nomics.world/v22/series/WB/DB?dimensions=\%7B\%22",
"indicator\%22\%3A\%5B\%22IC.REG.PROC.FE.NO\%22\%5D\%7D&q=Doing\%20Business",
"&observations=1&format=json&align_periods=1&offset=0&facets=0"
),
use_readLines = TRUE
)
......@@ -120,7 +120,7 @@ df2 <- rdb_by_api_link(
## Apply filter(s) to the series
# One filter
df3 <- rdb_by_api_link(
'https://api.db.nomics.world/v22/series/IMF/WEO/ABW.BCA?observations=1',
"https://api.db.nomics.world/v22/series/IMF/WEO/ABW.BCA?observations=1",
filters = list(
code = "interpolate",
parameters = list(frequency = "daily", method = "spline")
......@@ -129,7 +129,7 @@ df3 <- rdb_by_api_link(
# Two filters
df3 <- rdb_by_api_link(
'https://api.db.nomics.world/v22/series/IMF/WEO/ABW.BCA?observations=1',
"https://api.db.nomics.world/v22/series/IMF/WEO/ABW.BCA?observations=1",
filters = list(
list(
code = "interpolate",
......
......@@ -99,7 +99,7 @@ scale_colour_discrete <- function(...) {
knitr::opts_chunk$set(dev.args = list(bg = "transparent"))
dbnomics <- function() {
dbnomics <- function(...) {
list(
scale_x_date(expand = c(0, 0)),
scale_y_continuous(labels = function(x) { format(x, big.mark = " ") }),
......@@ -114,6 +114,7 @@ dbnomics <- function() {
plot.background = element_rect(fill = "transparent", colour = NA),
legend.title = element_blank()
),
theme(...),
annotate(
geom = "text", label = "DBnomics <https://db.nomics.world>",
x = structure(Inf, class = "Date"), y = -Inf,
......@@ -148,7 +149,7 @@ display_table <- function(DT) {
```
```{r, eval = FALSE}
df <- rdb(ids = 'AMECO/ZUTN/EA19.1.0.0.0.ZUTN') %>%
df <- rdb(ids = "AMECO/ZUTN/EA19.1.0.0.0.ZUTN") %>%
filter(!is.na(value))
```
```{r, eval = TRUE, echo = FALSE}
......@@ -189,13 +190,13 @@ ggplot(df, aes(x = period, y = value, color = series_code)) +
In the event that you only use the argument `ids`, you can drop it and run:
```{r, eval = FALSE}
df <- rdb('AMECO/ZUTN/EA19.1.0.0.0.ZUTN')
df <- rdb("AMECO/ZUTN/EA19.1.0.0.0.ZUTN")