rdb_dimensions.R 8.05 KB
Newer Older
Sébastien Galais's avatar
Sébastien Galais committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#' Download list of dimensions for datasets of DBnomics providers.
#'
#' \code{rdb_dimensions} downloads the list of dimensions (if they exist) for
#' available datasets of a selection of providers from
#' \href{https://db.nomics.world/}{DBnomics}.
#'
#' By default, the function returns a nested named list of \code{data.table}s
#' containing the dimensions of datasets for providers from
#' \href{https://db.nomics.world/}{DBnomics}.
#' 
#' @param provider_code Character string (default \code{NULL}). DBnomics code
#' of one or multiple providers. If \code{NULL}, the providers are firstly
#' dowloaded with the function \code{\link{rdb_providers}} and then the
#' datasets are requested.
#' @param dataset_code Character string (default \code{NULL}). DBnomics code
#' of one or multiple datasets of a provider. If \code{NULL}, the datasets
#' codes are dowloaded with the function \code{\link{rdb_datasets}} and then
#' the dimensions are requested.
#' @param use_readLines Logical (default \code{FALSE}). If \code{TRUE}, then
#' the data are requested and read with the base function \code{readLines} i.e.
#' through the default R internet connection. This can be used to get round the
#' error \code{Could not resolve host: api.db.nomics.world}.
#' @param curl_config Named list (default \code{NULL}). If not
#' \code{NULL}, it is used to configure a proxy connection. This
#' configuration is passed to the function \code{curl_fetch_memory} of the package
#' \pkg{curl}. A temporary \code{curl_handle} object is created internally
#' with arguments equal to the provided list in \code{curl_config}.\cr
#' For \code{curl_fetch_memory} arguments see \code{\link[curl]{curl_fetch}}.
#' For available curl options see \code{\link[curl]{curl_options}},
#' \code{names(curl_options())} and
#' \href{https://curl.haxx.se/libcurl/c/curl_easy_setopt.html}{libcurl}.
#' @param simplify Logical (default \code{FALSE}). If \code{TRUE}, when the
#' dimensions are requested for only one provider and one dataset then a
#' named list of \code{data.table}s is returned, not a nested named list of
#' \code{data.table}s.
36
#' @param ... Additionals arguments.
Sébastien Galais's avatar
Sébastien Galais committed
37
38
39
40
#' @return A nested named list of \code{data.table}s or a named list of
#' \code{data.table}s.
#' @examples
#' \dontrun{
41
#' rdb_dimensions(provider_code = "IMF", dataset_code = "WEO:2019-10")
Sébastien Galais's avatar
Sébastien Galais committed
42
#' 
43
#' rdb_dimensions(provider_code = "IMF", dataset_code = "WEO:2019-10", simplify = TRUE)
Sébastien Galais's avatar
Sébastien Galais committed
44
45
46
47
48
49
50
51
52
#' 
#' rdb_dimensions(provider_code = "IMF")
#' 
#' # /!\ It is very long !
#' options(rdbnomics.progress_bar_dimensions = TRUE)
#' rdb_dimensions()
#' options(rdbnomics.progress_bar_dimensions = FALSE)
#' 
#' rdb_dimensions(
53
#'   provider_code = "IMF", dataset_code = "WEO:2019-10",
Sébastien Galais's avatar
Sébastien Galais committed
54
55
56
57
#'   use_readLines = TRUE
#' )
#' 
#' rdb_dimensions(
58
#'   provider_code = "IMF", dataset_code = "WEO:2019-10",
Sébastien Galais's avatar
Sébastien Galais committed
59
60
61
62
#'   curl_config = list(proxy = "<proxy>", proxyport = <port>)
#' )
#' }
#' @seealso \code{\link{rdb_providers}}, \code{\link{rdb_last_updates}},
63
#' \code{\link{rdb_datasets}}, \code{\link{rdb_series}}
Sébastien Galais's avatar
Sébastien Galais committed
64
65
66
67
68
69
#' @author Sebastien Galais
#' @export
rdb_dimensions <- function(
  provider_code = NULL, dataset_code = NULL,
  use_readLines = getOption("rdbnomics.use_readLines"),
  curl_config = getOption("rdbnomics.curl_config"),
70
71
  simplify = FALSE,
  ...
Sébastien Galais's avatar
Sébastien Galais committed
72
) {
73
  # Additionals arguments
74
  progress_bar <- ellipsis_default("progress_bar", list(...), TRUE)
75
  check_argument(progress_bar, "logical")
76

Sébastien Galais's avatar
Sébastien Galais committed
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
  # All providers
  if (is.null(provider_code) & !is.null(dataset_code)) {
    stop(
      "If you give datasets codes, please give also a provider code.",
      call. = FALSE
    )
  }

  if (is.null(provider_code)) {
    provider_code <- rdb_providers(
      code = TRUE,
      use_readLines = use_readLines, curl_config = curl_config
    )
  }
  check_argument(provider_code, "character", len = FALSE)

  if (is.null(dataset_code)) {
    dataset_code <- rdb_datasets(
      provider_code = provider_code,
      use_readLines = use_readLines, curl_config = curl_config,
97
      simplify = FALSE, progress_bar = FALSE
Sébastien Galais's avatar
Sébastien Galais committed
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
    )
    dataset_code <- sapply(dataset_code, `[[`, "code", simplify = FALSE)
  } else {
    check_argument(dataset_code, "character", len = FALSE)
    dataset_code <- list(dataset_code)
    dataset_code <- stats::setNames(dataset_code, provider_code)
  }

  # Checking arguments
  check_argument(use_readLines, "logical")
  check_argument(simplify, "logical")

  # Setting API url
  api_base_url <- getOption("rdbnomics.api_base_url")
  check_argument(api_base_url, "character")

  # Setting API version
  api_version <- getOption("rdbnomics.api_version")
  check_argument(api_version, c("numeric", "integer"))
  authorized_version(api_version)

  # Fetching all datasets
  dimensions <- sapply(provider_code, function(pc) {
121
    if (getOption("rdbnomics.progress_bar_dimensions") & progress_bar) {
Sébastien Galais's avatar
Sébastien Galais committed
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
      pb <- utils::txtProgressBar(
        min = 0, max = length(dataset_code[[pc]]), style = 3
      )
    }
    
    tmp_dim <- sapply(seq_along(dataset_code[[pc]]), function(i) {
      tryCatch({
        dc <- dataset_code[[pc]][i]

        tmp <- paste0(api_base_url, "/v", api_version, "/datasets/", pc, "/", dc)
        tmp <- get_data(tmp, use_readLines, curl_config)

        tmp1 <- tmp$datasets$docs$dimensions_labels
        if (is.null(tmp1)) {  
          tmp1 <- try(
            tmp$datasets[[paste0(pc, "/", dc)]]$dimensions_labels,
            silent = TRUE
          )
          if (inherits(tmp1, "try-error")) {
            tmp1 <- NULL
          }
        }
        if (is.null(tmp1)) {
          # Sometimes "dimensions_labels" is missing
          tmp1 <- data.table::data.table(A = character(), B = character())
        } else {
          tmp1 <- as.list(tmp1)
          tmp1 <- data.table::data.table(A = unlist(tmp1), B = names(tmp1))
          tmp1 <- unique(tmp1)
151
152
153
154
155
156
157
158
159
          # Normally column B is in capital letters
          if (nrow(tmp1) > 0) {
            tmp1[, EQUAL := as.numeric(A == B)]
            tmp1[
              EQUAL == 1,
              A := ifelse(A == capital_first(A), toupper(A), capital_first(A))
            ]
            tmp1[, EQUAL := NULL]
          }
Sébastien Galais's avatar
Sébastien Galais committed
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
        }

        tmp2 <- tmp$datasets$docs$dimensions_values_labels
        if (is.null(tmp2)) {  
          tmp2 <- try(
            tmp$datasets[[paste0(pc, "/", dc)]]$dimensions_values_labels,
            silent = TRUE
          )
          if (inherits(tmp2, "try-error")) {
            tmp2 <- NULL
          }
        }
        
        tmp3 <- sapply(names(tmp2), function(nm) {
          z <- tmp2[[nm]]
          if (
            is.list(z) & !is.data.frame(z) &
            !data.table::is.data.table(z)
          ) {
            # "z" is actually a list with a matrix
            z <- z[[1]]
            z <- as.data.table(z)
          } else {
            if (is.matrix(z)) {
              # "z" is actually a matrix
              z <- as.data.table(z)
            } else {
              z <- as.data.table(z)
              z <- as.list(z)
              z <- data.table::data.table(V1 = names(z), V2 = unlist(z))
            }
          }
          data.table::setnames(z, "V1", nm)
          new_name <- tmp1[B == nm]$A
          data.table::setnames(
            z, "V2", ifelse(length(new_name) <= 0, new_title(nm), new_name)
          )
          z
        }, simplify = FALSE)

200
        if (getOption("rdbnomics.progress_bar_dimensions") & progress_bar) {
Sébastien Galais's avatar
Sébastien Galais committed
201
202
203
204
205
206
207
208
209
          utils::setTxtProgressBar(pb, i)
        }

        tmp3
      }, error = function(e) {
        NULL
      })
    }, simplify = FALSE)

210
211
212
    if (getOption("rdbnomics.progress_bar_dimensions") & progress_bar) {
      close(pb)
    }
Sébastien Galais's avatar
Sébastien Galais committed
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237

    tmp_dim <- stats::setNames(tmp_dim, dataset_code[[pc]])
    Filter(Negate(is.null), tmp_dim)
  }, simplify = FALSE)
  dimensions <- Filter(Negate(is.null), dimensions)
  # We remove the empty lists, the empty data.tables, etc.
  dimensions <- check_dimensions(dimensions, 2)

  if (length(dimensions) <= 0) {
    warning(
      "Error when fetching the dimensions.",
      call. = FALSE
    )
    return(NULL)
  }

  if (simplify) {
    len <- sapply(dimensions, length)
    if (length(dimensions) == 1 & len[1] == 1) {
      return(dimensions[[1]][[1]])
    }
  }

  dimensions
}