/
pxweb_query.R
401 lines (365 loc) · 12.5 KB
/
pxweb_query.R
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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
151
152
153
154
155
156
157
158
159
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
200
201
202
203
204
205
206
207
208
209
210
211
212
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#' Create a PXWEB query
#'
#' @description
#' Creates a pxweb query object from either a list with named values,
#' a json query file or json query string. See examples below.
#'
#' @param x an object to cast as a pxweb_query object.
#'
#' @seealso \code{\link{pxweb_query_as_json}}, \code{\link{pxweb_query_as_rcode}}
#'
#' @examples
#' dims <- list(
#' Alue = c("*"),
#' "Asuntokunnan koko" = c("*"),
#' Talotyyppi = c("S"),
#' Vuosi = c("*")
#' )
#' pxq1 <- pxweb_query(dims)
#'
#' json_query <- file.path(
#' system.file(package = "pxweb"),
#' "extdata", "examples", "json_query_example.json"
#' )
#' pxq2 <- pxweb_query(json_query)
#'
#' @export
pxweb_query <- function(x) {
UseMethod("pxweb_query")
}
#' @rdname pxweb_query
#' @keywords internal
#' @export
pxweb_query.character <- function(x) {
obj <- jsonlite::fromJSON(x, simplifyDataFrame = FALSE)
if (is.character(obj)) {
stop("jsonlite::fromJSON() cannot parse the PXWEB (JSON) query. Please check your (JSON) query.", call. = FALSE)
}
class(obj) <- c("pxweb_query", "list")
assert_pxweb_query(obj, check_response_format = FALSE)
if (tolower(obj$response$format) == "json") {
obj$response$format <- "json"
} else if (tolower(obj$response$format) %in% c("json-stat", "jsonstat")) {
obj$response$format <- "json-stat"
} else if (tolower(obj$response$format) %in% pxweb_file_response_formats()) {
} else {
warning(paste0("'", obj$response$format, "' is not a valid query response format, set to 'json'."))
obj$response$format <- "json"
}
assert_pxweb_query(obj)
obj
}
pxweb_file_response_formats <- function() c("px", "sdmx")
#' @rdname pxweb_query
#' @keywords internal
#' @export
pxweb_query.json <- function(x) {
pxweb_query(x = as.character(x))
}
#' @rdname pxweb_query
#' @keywords internal
#' @export
pxweb_query.pxweb_query <- function(x) {
return(x)
}
#' @rdname pxweb_query
#' @keywords internal
#' @export
pxweb_query.list <- function(x) {
checkmate::assert_named(x)
obj <- list(
query = list(),
response = list(format = "json")
)
for (i in seq_along(x)) {
obj$query[[i]] <- list(
code = names(x)[i],
selection = list(
filter = "item",
values = x[[i]]
)
)
if (x[[i]][1] == "*") {
obj$query[[i]]$selection$filter <- "all"
}
}
class(obj) <- c("pxweb_query", "list")
assert_pxweb_query(obj)
obj
}
#' @rdname pxweb_query
#' @keywords internal
#' @export
pxweb_query.response <- function(x) {
if (is.null(x$request$options$postfields)) {
return(NULL)
}
pxweb_query(x = readBin(x$request$options$postfields, what = "character"))
}
#' @rdname pxweb_query
#' @keywords internal
#' @export
pxweb_query.pxweb_explorer <- function(x) {
checkmate::assert_true(pxe_position_is_full_query(x))
md_ch <- pxe_metadata_choices(x)
mdo <- pxe_pxobj_at_position(x)
mdo_vnm <- pxweb_metadata_dim(mdo)
obj <- list()
for (i in seq_along(mdo$variables)) {
var_nm <- mdo$variables[[i]]$code
if (md_ch[[var_nm]][1] == "eliminate") {
next
}
obj[[var_nm]] <- mdo$variables[[i]]$values[md_ch[[var_nm]]]
}
pxq <- pxweb_query(obj)
pxweb_validate_query_with_metadata(pxq, mdo)
pxq
}
#' Assert a pxweb_query object
#'
#' @param x an object to assert conferms to the structure of an pxweb_query object.
#'
#' @keywords internal
assert_pxweb_query <- function(x, check_response_format = TRUE) {
checkmate::assert_class(x, c("pxweb_query", "list"), .var.name = "pxweb_query")
checkmate::assert_names(names(x), must.include = c("query", "response"), .var.name = "names(pxweb_query)")
checkmate::assert_names(names(x$response), must.include = c("format"))
if (check_response_format) {
checkmate::assert_choice(x$response$format, c("json", "json-stat", pxweb_file_response_formats()))
}
checkmate::assert_named(x$query, "unnamed")
for (i in seq_along(x$query)) {
checkmate::assert_character(x$query[[i]]$code, any.missing = FALSE, min.len = 1)
checkmate::assert_names(names(x$query[[i]]$selection), must.include = c("filter", "values"))
checkmate::assert_character(x$query[[i]]$selection$filter, len = 1, any.missing = FALSE)
checkmate::assert_character(x$query[[i]]$selection$values)
checkmate::assert_named(x$query[[i]]$selection$values, type = "unnamed")
}
# Assert filter values
for (i in seq_along(x$query)) {
filter_aggr <- FALSE
if (grepl(x = x$query[[i]]$selection$filter, "^[Vv]s:.*")) {
filter_aggr <- TRUE
}
if (grepl(x = x$query[[i]]$selection$filter, "^[Aa]gg:.*")) {
filter_aggr <- TRUE
}
if (!filter_aggr) {
checkmate::assert_choice(x$query[[i]]$selection$filter, choices = c("item", "all", "top", "agg:[aggregated values]", "vs:[other value set]"))
if (x$query[[i]]$selection$filter %in% c("all", "top")) {
checkmate::assert_character(x$query[[i]]$selection$values, len = 1, .var.name = paste0("x$query[[", i, "]]$selection$values"))
}
}
}
}
#' @export
print.pxweb_query <- function(x, ...) {
cat("PXWEB QUERY\n")
cat("query:\n")
for (i in seq_along(x$query)) {
cat(" [[", i, "]] ", x$query[[i]]$code, " (", x$query[[i]]$selection$filter, "):\n", sep = "")
cat(" ", paste(x$query[[i]]$selection$values, collapse = ", "), "\n", sep = "")
}
if (x$response$format == "json-stat") {
cat("return: json-stat\n")
}
}
#' Validate a \code{pxweb_query} with a \code{pxweb_metadata} object
#'
#' @details
#' Validate a query with a metadata object to asses that the query can be used to
#' query the table.
#'
#' @param pxq a \code{pxweb_query} object.
#' @param pxmd a \code{pxweb_metadata} object.
#'
#' @examples
#' \dontrun{
#' url <- "https://api.scb.se/OV0104/v1/doris/sv/ssd/BE/BE0101/BE0101A/BefolkningNy"
#' json_query <- file.path(
#' system.file(package = "pxweb"),
#' "extdata", "examples", "json_query_example.json"
#' )
#' pxq <- pxweb_query(json_query)
#' pxweb_validate_query_with_metadata(pxq, pxweb_get(url))
#' }
#'
#' @export
pxweb_validate_query_with_metadata <- function(pxq, pxmd) {
checkmate::assert_class(pxq, "pxweb_query")
checkmate::assert_class(pxmd, "pxweb_metadata")
pxweb_metadata_variables <- unlist(lapply(pxmd$variables, function(x) x$code))
query_variables <- character(length(pxq$query))
for (i in seq_along(pxq$query)) {
query_variables[i] <- pxq$query[[i]]$code
pxweb_query_variable_code <- pxq$query[[i]]$code
checkmate::assert_choice(pxweb_query_variable_code, choices = pxweb_metadata_variables)
if (tolower(pxq$query[[i]]$selection$filter) == "item") {
pxweb_query_variable_values <- pxq$query[[i]]$selection$values
meta_idx <- which(pxweb_metadata_variables %in% pxweb_query_variable_code)
pxweb_metadata_variable_values <- pxmd$variables[[meta_idx]]$values
if (!is.null(pxweb_metadata_variable_values)) {
checkmate::assert_subset(pxweb_query_variable_values, choices = pxweb_metadata_variable_values, .var.name = pxq$query[[i]]$code)
}
}
}
mandatory_variables <- character(0)
for (i in seq_along(pxmd$variables)) {
if (!pxmd$variables[[i]]$elimination) {
mandatory_variables <- c(mandatory_variables, pxmd$variables[[i]]$code)
}
}
if (!all(mandatory_variables %in% query_variables)) {
mandatory_variables_missing <- mandatory_variables[!mandatory_variables %in% query_variables]
stop("Mandatory variable(s) '", paste0(mandatory_variables_missing, collapse = "', '"), "' is missing in the query.", call. = FALSE)
}
}
#' Add and remove metadata to query
#'
#' @details
#' Add metadata values to variables with a query with filter "all".
#'
#' @param pxq a \code{pxweb_query} object.
#' @param pxmd a \code{pxweb_metadata} object.
#'
#' @keywords internal
pxweb_add_metadata_to_query <- function(pxq, pxmd) {
checkmate::assert_class(pxq, "pxweb_query")
checkmate::assert_class(pxmd, "pxweb_metadata")
# Set values to filter "all"
pxweb_metadata_variables <- unlist(lapply(pxmd$variables, function(x) x$code))
for (i in seq_along(pxq$query)) {
pxweb_query_variable_code <- pxq$query[[i]]$code
checkmate::assert_choice(pxweb_query_variable_code, choices = pxweb_metadata_variables)
if (tolower(pxq$query[[i]]$selection$filter) == "all") {
px_pattern <- pxq$query[[i]]$selection$values
px_pattern <- paste0("^", gsub(pattern = "\\*", replacement = "\\.\\+", px_pattern))
meta_data_values <- pxmd$variables[[which(pxweb_metadata_variables %in% pxweb_query_variable_code)]]$values
if (!is.null(meta_data_values)) {
meta_data_values <- meta_data_values[grepl(x = meta_data_values, pattern = px_pattern)]
pxq$query[[i]]$selection$values <- meta_data_values
pxq$query[[i]]$selection$filter <- "item"
}
}
}
assert_pxweb_query(pxq)
pxq
}
#' @rdname pxweb_add_metadata_to_query
#' @keywords internal
pxweb_remove_metadata_from_query <- function(pxq, pxmd) {
checkmate::assert_class(pxq, "pxweb_query")
checkmate::assert_class(pxmd, "pxweb_metadata")
# set full queries to all *
pxweb_metadata_variables <- unlist(lapply(pxmd$variables, function(x) x$code))
for (i in seq_along(pxq$query)) {
query_size <- length(pxq$query[[i]]$selection$values)
pxweb_query_variable_code <- pxq$query[[i]]$code
meta_data_size <- length(pxmd$variables[[which(pxweb_metadata_variables %in% pxweb_query_variable_code)]]$values)
if (query_size >= meta_data_size & query_size > 1) {
pxq$query[[i]]$selection$filter <- "all"
pxq$query[[i]]$selection$values <- "*"
}
}
assert_pxweb_query(pxq)
pxq
}
#' Compue the dimension of the query
#'
#' @param pxq a \code{pxweb_query} object.
#'
#' @keywords internal
pxweb_query_dim <- function(pxq) {
checkmate::assert_class(pxq, "pxweb_query")
dim_res <- numeric(length(pxq$query))
for (i in seq_along(pxq$query)) {
names(dim_res)[i] <- pxq$query[[i]]$code
if (tolower(pxq$query[[i]]$selection$filter) == "top") {
dim_res[i] <- as.numeric(pxq$query[[i]]$selection$values)
} else if (tolower(pxq$query[[i]]$selection$filter) == "all") {
warning("Cannot compute the dimension for a variable with filter 'all', set to 1.", call. = FALSE)
dim_res[i] <- 1
} else {
dim_res[i] <- length(pxq$query[[i]]$selection$values)
}
}
dim_res
}
#' Get query filter
#'
#' @param pxq a \code{pxweb_query} object.
#'
#' @return query variable selection values as a named list of character vectors.
#'
#' @keywords internal
pxweb_query_values <- function(pxq) {
checkmate::assert_class(pxq, "pxweb_query")
res <- lapply(pxq$query, function(x) x$selection$values)
names(res) <- unlist(lapply(pxq$query, function(x) x$code))
res
}
#' Get query values
#'
#' @param pxq a \code{pxweb_query} object.
#'
#' @return query variable selection filters as a named character vector.
#'
#' @keywords internal
pxweb_query_filter <- function(pxq) {
checkmate::assert_class(pxq, "pxweb_query")
res <- unlist(lapply(pxq$query, function(x) x$selection$filter))
names(res) <- unlist(lapply(pxq$query, function(x) x$code))
res
}
#' Convert a \code{pxweb_query} object to a \code{json} string
#'
#' @param pxq a \code{pxweb_query} object.
#' @param ... further argument to \code{jsonlite::toJSON()}.
#'
#' @seealso \code{\link{pxweb_query}}, \code{\link{pxweb_query_as_rcode}}
#'
#' @examples
#' json_query <- file.path(
#' system.file(package = "pxweb"),
#' "extdata", "examples", "json_query_example.json"
#' )
#' pxq <- pxweb_query(json_query)
#' json <- pxweb_query_as_json(pxq, pretty = TRUE)
#'
#' @export
pxweb_query_as_json <- function(pxq, ...) {
checkmate::assert_class(pxq, "pxweb_query")
pxq$response$format <- jsonlite::unbox(pxq$response$format)
for (i in seq_along(pxq$query)) {
pxq$query[[i]]$code <- jsonlite::unbox(pxq$query[[i]]$code)
pxq$query[[i]]$selection$filter <- jsonlite::unbox(pxq$query[[i]]$selection$filter)
}
jsonlite::toJSON(pxq, ...)
}
#' Print a \code{pxweb_query} object as R code
#'
#' @param pxq a \code{pxweb_query} object.
#'
#' @seealso \code{\link{pxweb_query_as_json}}, \code{\link{pxweb_query}}
#'
#' @export
pxweb_query_as_rcode <- function(pxq) {
checkmate::assert_class(pxq, "pxweb_query")
res <- character(length(pxq$query))
for (i in seq_along(pxq$query)) {
res[i] <- paste0("\"", pxq$query[[i]]$code, "\"=c(", paste(paste0("\"", pxq$query[[i]]$selection$values, "\""), collapse = ","), ")")
if (i == 1) {
res[i] <- paste0(" list(", res[i])
} else {
res[i] <- paste0(" ", res[i])
}
}
res[length(res)] <- paste0(res[length(res)], ")")
if (length(res) > 1) {
res[-length(res)] <- paste0(res[-length(res)], ",")
}
res <- c("pxweb_query_list <- ", res)
cat(res, sep = "\n")
invisible(res)
}