diff --git a/DESCRIPTION b/DESCRIPTION index 2504f8eb30d51ac90cff6992317483df73342c49..6fe4f960de6ae721ada65964f0fcd689a4d653f4 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -20,8 +20,10 @@ Imports: data.table, jsonlite Suggests: + ggplot2, knitr, rmarkdown, - tinytest + tinytest, + xts VignetteBuilder: knitr RoxygenNote: 7.1.1 diff --git a/NAMESPACE b/NAMESPACE index 64bf2c01c514e9c8d5bc5d1c23866a983a61bfb8..22472dee279a00e2b8320a225f374642eb16084e 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -7,7 +7,9 @@ export(rdb_datasets) export(rdb_dimensions) export(rdb_last_updates) export(rdb_providers) +export(rdb_rename_xts) export(rdb_series) +export(rdb_to_xts) import(curl) import(data.table) import(jsonlite) diff --git a/NEWS.md b/NEWS.md index 2948e66814cb6b969ff7dae1e2dc32bed5ec3a15..22a38a15782038688576c576f2cc323fccdbe1fd 100644 --- a/NEWS.md +++ b/NEWS.md @@ -3,6 +3,8 @@ * New badge in README. * Correction of .gitlab-ci.yml with pkg-config. * Correction of `rdb()` examples in the doc and README. +* Simplification of README. +* New fonctions `rdb_to_xts()` and `rdb_rename_xts()` that were in README. # rdbnomics 0.6.2 diff --git a/R/rdb.R b/R/rdb.R index 378b37910a830e200b2b1d14667b16143f05a094..618f560c0e7a9ca380734cd18762a9967589259e 100644 --- a/R/rdb.R +++ b/R/rdb.R @@ -174,7 +174,7 @@ #' ## Apply filter(s) to the series #' # One filter #' df1 <- rdb( -#' ids = c("IMF/WEO/ABW.BCA", "IMF/WEO/ABW.BCA_NGDPD"), +#' ids = c("IMF/WEO/ABW.BCA.us_dollars", "IMF/WEO/ABW.BCA_NGDPD.pcent_gdp"), #' filters = list( #' code = "interpolate", #' parameters = list(frequency = "daily", method = "spline") @@ -183,7 +183,7 @@ #' #' # Two filters #' df1 <- rdb( -#' ids = c("IMF/WEO/ABW.BCA", "IMF/WEO/ABW.BCA_NGDPD"), +#' ids = c("IMF/WEO/ABW.BCA.us_dollars", "IMF/WEO/ABW.BCA_NGDPD.pcent_gdp"), #' filters = list( #' list( #' code = "interpolate", diff --git a/R/rdb_rename_xts.R b/R/rdb_rename_xts.R new file mode 100644 index 0000000000000000000000000000000000000000..04c2d6f3c8f5e7c63f6c100ef0152bd4f749a0e4 --- /dev/null +++ b/R/rdb_rename_xts.R @@ -0,0 +1,46 @@ +#' Rename the xts object columns +#' +#' In the \code{xts} object returned by the function \code{rdb_to_xts}, the +#' series codes are used as column names. If you prefer the series names +#' (or apply a function to them), the function \code{rdb_rename_xts} is here for +#' that. +#' +#' @param x \code{xts} object. The \code{xts} object returned by the function +#' \code{rdb_to_xts}. +#' @param fun function (default NULL). The function to apply to the column names. +#' @param ... Arguments for the function \code{fun}. +#' @return A \code{xts} object. +#' @examples +#' \dontrun{ +#' library(xts) +#' library(data.table) +#' library(rdbnomics) +#' +#' df <- rdb("IMF", "BOP", mask = "A.FR+ES.BCA_BP6_EUR") +#' df <- rdb_to_xts(df) +#' rdb_rename_xts(df) +#' } +#' @seealso \code{\link{rdb}}, \code{\link{rdb_to_xts}} +#' @author Sebastien Galais +#' @export +rdb_rename_xts <- function(x, fun = NULL, ...) { + code <- paste( + " xts_ok <- try(utils::packageVersion('xts'), silent = TRUE)", + " if (inherits(xts_ok, 'try-error')) {", + " stop(", + " 'Please install the package xts to use rdb_to_xts().',", + " call. = FALSE", + " )", + " }", + " nm <- xts::xtsAttributes(x)$codename", + " cols <- nm$series_name[match(names(x), nm$series_code)]", + " if (is.null(fun)) {", + " names(x) <- cols", + " } else {", + " names(x) <- sapply(X = cols, FUN = fun, ..., USE.NAMES = FALSE)", + " }", + " x", + sep = "\n" + ) + eval(parse(text = code)) +} diff --git a/R/rdb_to_xts.R b/R/rdb_to_xts.R new file mode 100644 index 0000000000000000000000000000000000000000..7d80e6bf25e4aa3c5a8091b3d31c56e88d94acc0 --- /dev/null +++ b/R/rdb_to_xts.R @@ -0,0 +1,94 @@ +#' Transform the data.table object into a xts object +#' +#' For some analysis, it is more convenient to have a \code{xts} object +#' instead of a \code{data.table} object. +#' +#' @param x \code{data.table}. The \code{data.table} returned by the \code{rdb} +#' function. +#' @param needed_columns Vector of character strings (default +#' \code{c("period", "series_code", "series_name", "value")}). Vector of column +#' names which are needed to transform the \code{data.table} into a \code{xts} +#' object. +#' @param series_columns Vector of character strings (default +#' \code{c("series_code", "series_name")}). Vector of series column +#' names. +#' @return A \code{xts} object. +#' @examples +#' \dontrun{ +#' library(xts) +#' library(data.table) +#' library(rdbnomics) +#' +#' df <- rdb("IMF", "BOP", mask = "A.FR+ES.BCA_BP6_EUR") +#' rdb_to_xts(df) +#' } +#' @seealso \code{\link{rdb}}, \code{\link{rdb_rename_xts}} +#' @author Sebastien Galais +#' @export +rdb_to_xts <- function( + x, + needed_columns = c("period", "series_code", "series_name", "value"), + series_columns = c("series_code", "series_name") +) { + code <- paste( + " xts_ok <- try(utils::packageVersion('xts'), silent = TRUE)", + " if (inherits(xts_ok, 'try-error')) {", + " stop(", + " 'Please install the package xts to use rdb_to_xts().',", + " call. = FALSE", + " )", + " }", + " if (is.null(x)) {", + " return(NULL)", + " }", + " all_cols <- length(setdiff(needed_columns, colnames(x))) != 0", + " if (all_cols) {", + " stop(", + " paste0(", + " 'To export as a xts object, some columns are missing. Needed columns ',", + " 'are \u0022', paste0(needed_columns, collapse = '\u0022, \u0022'),", + " '\u0022'", + " ),", + " call. = FALSE", + " )", + " }", + " x <- x[, .SD, .SDcols = needed_columns]", + " data.table::setcolorder(x, needed_columns)", + " attr_names <- NULL", + " if (!is.null(series_columns)) {", + " attr_names <- unique(x[, .SD, .SDcols = series_columns])", + " }", + " if (nrow(x) > 0) {", + " x <- data.table::dcast.data.table(", + " x, period ~ series_code,", + " value.var = 'value'", + " )", + " } else {", + " orig <- Sys.Date() - as.numeric(Sys.Date())", + " x <- data.table(", + " period = as.Date(numeric(), origin = orig),", + " no_code = numeric()", + " )", + " }", + " if (!inherits(x[[1]], 'Date')) {", + " stop(", + " paste0(", + " 'The first needed column ', needed_columns[1], ' is not of class ',", + " 'Date which is a problem for data.table::as.xts.data.table().'", + " ),", + " '\n',", + " paste0(", + " 'Please check with packageVersion(\u0022rdbnomics\u0022) that your version is ',", + " 'greater or equal to 0.5.2. Otherwise update rdbnomics or contact ',", + " 's915.stem@gmail.com.'", + " ),", + " call. = FALSE", + " )", + " }", + " x <- data.table::as.xts.data.table(x)", + " xts::xtsAttributes(x) <- list(codename = attr_names)", + " x", + sep = "\n" + ) + eval(parse(text = code)) +} diff --git a/R/sysdata.rda b/R/sysdata.rda index 8ef293f2913bdd7889e9a058dfb3c0dee61f092a..1d3c1c78fd28f654223b25759eb5b0692a24ee1c 100644 Binary files a/R/sysdata.rda and b/R/sysdata.rda differ diff --git a/README.md b/README.md index 1fbed6e1d0d3f25021f82c90f70aa042129778a4..3c60f1509d8a7a8a5713e91737cc548c7042954c 100644 --- a/README.md +++ b/README.md @@ -27,384 +27,28 @@ remotes::install_github("dbnomics/rdbnomics", build_vignettes = TRUE, force = TR library(rdbnomics) ``` -After installation, a vignette is available to the user: -```r -vignette("rdbnomics") -``` - -## Examples -### 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") - -# 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/BOP/A.FR.BCA_BP6_EUR")) -``` - -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") -``` - -### Fetch time series by `mask` : -```r -# Fetch one series from dataset 'Balance of Payments' (BOP) of IMF: -df1 <- rdb("IMF", "BOP", mask = "A.FR.BCA_BP6_EUR") - -# Fetch two series from dataset 'Balance of Payments' (BOP) of IMF: -df2 <- rdb("IMF", "BOP", mask = "A.FR+ES.BCA_BP6_EUR") - -# Fetch all series along one dimension from dataset 'Balance of Payments' (BOP) of IMF: -df3 <- rdb("IMF", "BOP", mask = "A..BCA_BP6_EUR") - -# Fetch series along multiple dimensions from dataset 'Balance of Payments' (BOP) of IMF: -df4 <- rdb("IMF", "BOP", mask = "A.FR.BCA_BP6_EUR+IA_BP6_EUR") -``` - -In the event that you only use the arguments `provider_code`, `dataset_code` and `mask`, you can drop the name `mask` and run: -```r -df4 <- rdb("IMF", "BOP", "A.FR.BCA_BP6_EUR") -``` - -### 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")) -# 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: -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"]}') -``` - -### 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(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") -``` - -In the event that you only use the argument `api_link`, you can drop the name and run: -```r -df1 <- rdb("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") -``` - -## Fetch the available datasets of a provider -```r -# Example with the IMF datasets: -df_datasets <- rdb_datasets(provider_code = "IMF") - -# Example with the IMF and BDF datasets: -df_datasets <- rdb_datasets(provider_code = c("IMF", "BDF")) -``` - -In the event that you only request the datasets for one provider, if you define -`simplify = TRUE`, then the result will be a `data.table` not a named list. -```r -df_datasets <- rdb_datasets(provider_code = "IMF", simplify = TRUE) -``` - -## Fetch the possible dimensions of available datasets of a provider -```r -# Example for the dataset WEO of the IMF: -df_dimensions <- rdb_dimensions(provider_code = "IMF", dataset_code = "WEO") -``` - -In the event that you only request the dimensions for one dataset for one -provider, if you define `simplify = TRUE`, then the result will be a named list -`data.table` not a nested named list. -```r -df_dimensions <- rdb_dimensions(provider_code = "IMF", dataset_code = "WEO", simplify = TRUE) -``` - -## Fetch the number of series of available datasets of a provider -```r -# Example for the dataset WEOAGG of the IMF: -df_series <- rdb_series(provider_code = "IMF", dataset_code = "WEOAGG") - -# With dimensions -df_series <- rdb_series("IMF", "WEO", dimensions = list(`weo-country` = "AGO")) -df_series <- rdb_series("IMF", "WEO", dimensions = list(`weo-subject` = "NGDP_RPCH"), simplify = TRUE) - -# With a query -df_series <- rdb_series("IMF", "WEO", query = "ARE") -df_series <- rdb_series("IMF", c("WEO", "WEOAGG"), query = "NGDP_RPCH") -``` - -:warning: We ask the user to use this function parsimoniously because there are a huge amount -of series per dataset. Please only fetch for one dataset if you need it or -visit the website [https://db.nomics.world](https://db.nomics.world). - -## Proxy configuration or connection error `Could not resolve host` -When using the function `rdb`, you may come across the following error: -```r -Error in open.connection(con, "rb") : - Could not resolve host: api.db.nomics.world -``` -To get round this situation, you have two possibilities: - -1. configure **curl** to use a specific and authorized proxy. +After installation, a vignette is available to the user with: -2. use the default R internet connection i.e. the Internet Explorer proxy defined in *internet2.dll*. - -### Configure **curl** to use a specific and authorized proxy -In **rdbnomics**, by default the function `curl_fetch_memory` (of the package **curl**) is used to fetch the data. If a specific proxy must be used, it is possible to define it permanently with the package option `rdbnomics.curl_config` or on the fly through the argument `curl_config`. Because the object is a named list, its elements are passed to the connection (the `curl_handle` object created internally with `new_handle()`) with `handle_setopt()` before using `curl_fetch_memory`. - -To see the available parameters, run `names(curl_options())` in *R* or visit the website <a href="https://curl.haxx.se/libcurl/c/curl_easy_setopt.html" target="_blank">https://curl.haxx.se/libcurl/c/curl_easy_setopt.html</a>. Once they are chosen, you define the curl object as follows: -```r -h <- list( - proxy = "<proxy>", - proxyport = <port>, - proxyusername = "<username>", - proxypassword = "<password>" -) -``` - -#### Set the connection up for a session -The curl connection can be set up for a session by modifying the following package option: -```r -options(rdbnomics.curl_config = h) -``` -When fetching the data, the following command is executed: -```r -hndl <- curl::new_handle() -curl::handle_setopt(hndl, .list = getOption("rdbnomics.curl_config")) -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") -``` -This option of the package can be disabled with: -```r -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 function `rdb`: -```r -df1 <- rdb(ids = "AMECO/ZUTN/EA19.1.0.0.0.ZUTN", curl_config = h) -``` - -### Use the default R internet connection -To retrieve the data with the default R internet connection, **rdbnomics** will use the base function `readLines`. - -#### Set the connection up for a session -To activate this feature for a session, you need to enable an option of the package: -```r -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") -``` -This configuration can be disabled with: -```r -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 function `rdb`: -```r -df1 <- rdb(ids = "AMECO/ZUTN/EA19.1.0.0.0.ZUTN", use_readLines = TRUE) -``` - -## Transform time series with filters -The **rdbnomics** package can interact with the *Time Series Editor* of DBnomics to transform time series by applying filters to them. -Available filters are listed on the filters page [https://editor.nomics.world/filters](https://editor.nomics.world/filters). - -Here is an example of how to proceed to interpolate two annual time series with a monthly frequency, using a spline interpolation: +* many examples, +* how to configure a proxy, +* how to use default R internet connection, +* transform time series with filters, +* ... +Just follow the link <a href="https://cran.r-project.org/web/packages/rdbnomics/vignettes/rdbnomics.html" target="_blank">rdbnomics</a> or run: ```r -filters <- list( - code = "interpolate", - parameters = list(frequency = "monthly", method = "spline") -) - -df <- rdb( - ids = c("AMECO/ZUTN/EA19.1.0.0.0.ZUTN", "AMECO/ZUTN/DNK.1.0.0.0.ZUTN"), - filters = filters -) -``` - -If you want to apply more than one filter, the `filters` argument will be a list of valid filters: - -```r -filters <- list( - list( - code = "interpolate", - parameters = list(frequency = "monthly", method = "spline") - ), - list( - code = "aggregate", - parameters = list(frequency = "bi-annual", method = "end_of_period") - ) -) - -df <- rdb( - ids = c("AMECO/ZUTN/EA19.1.0.0.0.ZUTN", "AMECO/ZUTN/DNK.1.0.0.0.ZUTN"), - filters = filters -) -``` - -The `data.table` columns change a little bit when filters are used. There are two new columns: - -- `period_middle_day`: the middle day of `original_period` (can be useful when you compare graphically interpolated series and original ones). -- `filtered` (boolean): `TRUE` if the series is filtered, `FALSE` otherwise. - -The content of two columns are modified: - -- `series_code`: same as before for original series, but the suffix `_filtered` is added for filtered series. -- `series_name`: same as before for original series, but the suffix ` (filtered)` is added for filtered series. - -## Transform the `data.table` object into a `xts` object -For some analysis, it is more convenient to have a `xts` object instead of a `data.table` object. To transform -it, you can use the following functions: -```r -library(xts) -library(data.table) -library(rdbnomics) - -to_xts <- function( - x, - needed_columns = c("period", "series_code", "series_name", "value"), - series_columns = c("series_code", "series_name") -) { - if (is.null(x)) { - return(NULL) - } - - all_cols <- length(setdiff(needed_columns, colnames(x))) != 0 - if (all_cols) { - stop( - paste0( - "To export as a xts object, some columns are missing. Needed columns ", - "are \u0022", paste0(needed_columns, collapse = "\u0022, \u0022"), - "\u0022" - ), - call. = FALSE - ) - } - - x <- x[, .SD, .SDcols = needed_columns] - data.table::setcolorder(x, needed_columns) - - attr_names <- NULL - if (!is.null(series_columns)) { - attr_names <- unique(x[, .SD, .SDcols = series_columns]) - } - - if (nrow(x) > 0) { - x <- data.table::dcast.data.table( - x, period ~ series_code, - value.var = "value" - ) - } else { - orig <- Sys.Date() - as.numeric(Sys.Date()) - x <- data.table( - period = as.Date(numeric(), origin = orig), - no_code = numeric() - ) - } - - if (!inherits(x[[1]], "Date")) { - stop( - paste0( - "The first needed column '", needed_columns[1], "' is not of class ", - "'Date' which is a problem for data.table::as.xts.data.table()." - ), - "\n", - paste0( - "Please check with packageVersion(\"rdbnomics\") that your version is ", - "greater or equal to '0.5.2'. Otherwise update rdbnomics or contact ", - "s915.stem@gmail.com." - ), - call. = FALSE - ) - } - - x <- data.table::as.xts.data.table(x) - xts::xtsAttributes(x) <- list(codename = attr_names) - - x -} - -rdb("IMF", "BOP", mask = "A.FR+ES.BCA_BP6_EUR") -#> ... original_value period provider_code REF_AREA Reference Area series_code ... -#> 1: NA 1940-01-01 IMF ES Spain A.ES.BCA_BP6_EUR -#> 2: NA 1941-01-01 IMF ES Spain A.ES.BCA_BP6_EUR -#> --- ... ... ... ... ... ... -#> 159: -15136.8 2018-01-01 IMF FR France A.FR.BCA_BP6_EUR -#> 160: NA 2019-01-01 IMF FR France A.FR.BCA_BP6_EUR - -to_xts(rdb("IMF", "BOP", mask = "A.FR+ES.BCA_BP6_EUR")) -#> A.ES.BCA_BP6_EUR A.FR.BCA_BP6_EUR -#> 1940-01-01 NA NA -#> 1941-01-01 NA NA -#> 1942-01-01 NA NA -#> ... ... ... -#> 2017-01-01 31086 -16397.700 -#> 2018-01-01 23283 -15136.800 -#> 2019-01-01 NA NA +vignette("rdbnomics") ``` - -In the `xts` object, the series codes are used as column names. If you prefer -the series names (or apply a function to them), you can utilize the function: +A lot of examples are available in the functions' documentations: ```r -library(magrittr) - -rdb_rename_xts <- function(x, fun = NULL, ...) { - nm <- xts::xtsAttributes(x)$codename - cols <- nm$series_name[match(names(x), nm$series_code)] - if (is.null(fun)) { - names(x) <- cols - } else { - names(x) <- sapply(X = cols, FUN = fun, ..., USE.NAMES = FALSE) - } - x -} - -rdb("IMF", "BOP", mask = "A.FR+ES.BCA_BP6_EUR") %>% - to_xts() %>% - rdb_rename_xts() -#> Annual – Spain – Current Account, Total, Net, Euros Annual – France – Current Account, Total, Net, Euros -#> 1940-01-01 NA NA -#> 1941-01-01 NA NA -#> 1942-01-01 NA NA -#> ... ... ... -#> 2017-01-01 31086 -16397.700 -#> 2018-01-01 23283 -15136.800 -#> 2019-01-01 NA NA - -rdb("IMF", "BOP", mask = "A.FR+ES.BCA_BP6_EUR") %>% - to_xts() %>% - rdb_rename_xts(stringr::word, start = 3) -#> Spain France -#> 1940-01-01 NA NA -#> 1941-01-01 NA NA -#> 1942-01-01 NA NA -#> ... ... ... -#> 2017-01-01 31086 -16397.700 -#> 2018-01-01 23283 -15136.800 -#> 2019-01-01 NA NA +?rdb +?rdb_datasets +?rdb_dimensions +?rdb_series +?rdb_providers +?rdb_last_updates +?rdb_to_xts +?rdb_rename_xts ``` ## P.S. diff --git a/man/rdb.Rd b/man/rdb.Rd index c6c065fa26a488ae58532e2ecda4a020b5b94d95..0d3388e49a4ce0e60212422b60a6beb354e48c00 100644 --- a/man/rdb.Rd +++ b/man/rdb.Rd @@ -209,7 +209,7 @@ df1 <- rdb(ids = "AMECO/ZUTN/EA19.1.0.0.0.ZUTN", use_readLines = TRUE) ## Apply filter(s) to the series # One filter df1 <- rdb( - ids = c("IMF/WEO/ABW.BCA", "IMF/WEO/ABW.BCA_NGDPD"), + ids = c("IMF/WEO/ABW.BCA.us_dollars", "IMF/WEO/ABW.BCA_NGDPD.pcent_gdp"), filters = list( code = "interpolate", parameters = list(frequency = "daily", method = "spline") @@ -218,7 +218,7 @@ df1 <- rdb( # Two filters df1 <- rdb( - ids = c("IMF/WEO/ABW.BCA", "IMF/WEO/ABW.BCA_NGDPD"), + ids = c("IMF/WEO/ABW.BCA.us_dollars", "IMF/WEO/ABW.BCA_NGDPD.pcent_gdp"), filters = list( list( code = "interpolate", diff --git a/man/rdb_rename_xts.Rd b/man/rdb_rename_xts.Rd new file mode 100644 index 0000000000000000000000000000000000000000..6e7423290170e3a3f54b29126c47d4761cdc1f90 --- /dev/null +++ b/man/rdb_rename_xts.Rd @@ -0,0 +1,42 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/rdb_rename_xts.R +\name{rdb_rename_xts} +\alias{rdb_rename_xts} +\title{Rename the xts object columns} +\usage{ +rdb_rename_xts(x, fun = NULL, ...) +} +\arguments{ +\item{x}{\code{xts} object. The \code{xts} object returned by the function +\code{rdb_to_xts}.} + +\item{fun}{function (default NULL). The function to apply to the column names.} + +\item{...}{Arguments for the function \code{fun}.} +} +\value{ +A \code{xts} object. +} +\description{ +In the \code{xts} object returned by the function \code{rdb_to_xts}, the +series codes are used as column names. If you prefer the series names +(or apply a function to them), the function \code{rdb_rename_xts} is here for +that. +} +\examples{ +\dontrun{ +library(xts) +library(data.table) +library(rdbnomics) + +df <- rdb("IMF", "BOP", mask = "A.FR+ES.BCA_BP6_EUR") +df <- rdb_to_xts(df) +rdb_rename_xts(df) +} +} +\seealso{ +\code{\link{rdb}}, \code{\link{rdb_to_xts}} +} +\author{ +Sebastien Galais +} diff --git a/man/rdb_to_xts.Rd b/man/rdb_to_xts.Rd new file mode 100644 index 0000000000000000000000000000000000000000..d0654f31427d5e38218d76354bb3fd57fbe9ec63 --- /dev/null +++ b/man/rdb_to_xts.Rd @@ -0,0 +1,48 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/rdb_to_xts.R +\name{rdb_to_xts} +\alias{rdb_to_xts} +\title{Transform the data.table object into a xts object} +\usage{ +rdb_to_xts( + x, + needed_columns = c("period", "series_code", "series_name", "value"), + series_columns = c("series_code", "series_name") +) +} +\arguments{ +\item{x}{\code{data.table}. The \code{data.table} returned by the \code{rdb} +function.} + +\item{needed_columns}{Vector of character strings (default +\code{c("period", "series_code", "series_name", "value")}). Vector of column +names which are needed to transform the \code{data.table} into a \code{xts} +object.} + +\item{series_columns}{Vector of character strings (default +\code{c("series_code", "series_name")}). Vector of series column +names.} +} +\value{ +A \code{xts} object. +} +\description{ +For some analysis, it is more convenient to have a \code{xts} object +instead of a \code{data.table} object. +} +\examples{ +\dontrun{ +library(xts) +library(data.table) +library(rdbnomics) + +df <- rdb("IMF", "BOP", mask = "A.FR+ES.BCA_BP6_EUR") +rdb_to_xts(df) +} +} +\seealso{ +\code{\link{rdb}}, \code{\link{rdb_rename_xts}} +} +\author{ +Sebastien Galais +} diff --git a/vignettes/rdbnomics.Rmd b/vignettes/rdbnomics.Rmd index 15ce0dba41fe2ab93778dc9359d10803b1f58b05..354abaff571200005fe83431a9bb81177f775709 100644 --- a/vignettes/rdbnomics.Rmd +++ b/vignettes/rdbnomics.Rmd @@ -237,7 +237,7 @@ mtext( ## Fetch two series from different datasets of different providers ```{r, eval = FALSE} -df <- rdb(ids = c("AMECO/ZUTN/EA19.1.0.0.0.ZUTN", "Eurostat/une_rt_q/Q.SA.TOTAL.PC_ACT.T.EA19")) +df <- rdb(ids = c("AMECO/ZUTN/EA19.1.0.0.0.ZUTN", "Eurostat/une_rt_q/Q.SA.Y15-24.PC_ACT.T.EA19")) df <- df[!is.na(value)] ``` ```{r, eval = TRUE, echo = FALSE}