12
12
# '
13
13
# ' @return A `tibble` with the following columns:
14
14
# '
15
- # ' **For univariate data** (when `y_source` is not provided):
16
- # '
17
- # ' * `best_year`: the best available year, closest to the requested year.
18
- # ' * `geo`: code for the NUTS region at the requested level.
19
- # ' * `geo_name`: name of the NUTS region at the requested level.
15
+ # ' * `geo`: code for the (NUTS) region at the requested level.
16
+ # ' * `geo_name`: name of the (NUTS) region at the requested level.
17
+ # ' * `geo_source`: source (type) of the spatial units at the requested level.
18
+ # ' * `geo_year`: year of the (NUTS) region at the requested level.
19
+ # ' * `x_year`: The year of the predictor variable (X), included in bivariate requests.
20
+ # ' * `y_year` (optional): The year of the outcome variable (Y), included in bivariate requests (only included when `y_source` is provided).
20
21
# ' * `x`: the value of the univariate variable.
21
- # '
22
- # ' **For bivariate data** (when `y_source` is provided):
23
- # '
24
- # ' * `best_year`: the best available year, closest to the requested year (same for both x and y variables).
25
- # ' * `geo`: code for the NUTS region at the requested level.
26
- # ' * `geo_name`: name of the NUTS region at the requested level.
27
- # ' * `x`: the value of the x variable.
28
- # ' * `y`: the value of the y variable.
22
+ # ' * `y` (optional): the value of the y variable (only included when `y_source` is provided).
29
23
# '
30
24
# ' @export
31
25
# '
@@ -76,7 +70,7 @@ mi_data <- function(
76
70
conditions = x_conditions
77
71
)
78
72
x_json_string <- jsonlite :: toJSON(x_json , auto_unbox = TRUE )
79
-
73
+
80
74
# Check if it's bivariate (Y filters are provided)
81
75
if (! is.null(y_source ) && ! is.null(y_filters )) {
82
76
y_conditions <- lapply(names(y_filters ), function (name ) {
@@ -100,14 +94,20 @@ mi_data <- function(
100
94
# Prepare query parameters
101
95
query_params <- list (
102
96
`_level` = level ,
103
- `_year` = as.character(year ),
104
- `X_JSON` = x_json_string ,
105
97
`limit` = limit
106
98
)
107
-
108
- # Add Y_JSON to query parameters if bivariate
99
+
100
+ if (is.null(y_source )) {
101
+ query_params $ `_year` <- as.character(year )
102
+ } else {
103
+ query_params $ `_predictor_year` <- as.character(year )
104
+ query_params $ `_outcome_year` <- as.character(year )
105
+ }
106
+
107
+ # Add JSON parameters as proper strings without URL encoding issues
108
+ query_params $ `X_JSON` <- I(x_json_string )
109
109
if (! is.null(y_source ) && ! is.null(y_filters )) {
110
- query_params $ `Y_JSON` <- y_json_string
110
+ query_params $ `Y_JSON` <- I( y_json_string )
111
111
}
112
112
113
113
# Perform API request
@@ -124,5 +124,30 @@ mi_data <- function(
124
124
response_data <- httr2 :: resp_body_json(response , simplifyVector = TRUE ) | >
125
125
tibble :: as_tibble()
126
126
127
+ # Define expected columns based on whether y_source is specified
128
+ if (is.null(y_source )) {
129
+ expected_columns <- c(" geo" , " geo_name" , " geo_source" , " geo_year" , " data_year" , " x" )
130
+ } else {
131
+ expected_columns <- c(" geo" , " geo_name" , " geo_source" , " geo_year" ,
132
+ " predictor_year" , " outcome_year" , " x" , " y" )
133
+ }
134
+
135
+ # Check for missing expected columns
136
+ missing_columns <- setdiff(expected_columns , colnames(response_data ))
137
+
138
+ if (length(missing_columns ) > 0 ) {
139
+ stop(" The following expected columns are missing from the response: " , paste(missing_columns , collapse = " , " ), " . The API may be down or might have changed. Please try again later. If the error persists, please open an issue on GitHub at <https://github.com/e-kotov/mapineqr/issues>." )
140
+ }
141
+
142
+ # Select and reorder columns using dplyr
143
+ response_data <- response_data | >
144
+ dplyr :: select(dplyr :: all_of(expected_columns )) | >
145
+ dplyr :: rename_with(~ dplyr :: case_when(
146
+ .x == " predictor_year" ~ " x_year" ,
147
+ .x == " data_year" & ! " predictor_year" %in% colnames(response_data ) ~ " x_year" ,
148
+ .x == " outcome_year" ~ " y_year" ,
149
+ TRUE ~ .x
150
+ ), .cols = dplyr :: any_of(c(" predictor_year" , " outcome_year" , " data_year" )))
151
+
127
152
return (response_data )
128
153
}
0 commit comments