1212# '
1313# ' @return A `tibble` with the following columns:
1414# '
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).
2021# ' * `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).
2923# '
3024# ' @export
3125# '
@@ -76,7 +70,7 @@ mi_data <- function(
7670 conditions = x_conditions
7771 )
7872 x_json_string <- jsonlite :: toJSON(x_json , auto_unbox = TRUE )
79-
73+
8074 # Check if it's bivariate (Y filters are provided)
8175 if (! is.null(y_source ) && ! is.null(y_filters )) {
8276 y_conditions <- lapply(names(y_filters ), function (name ) {
@@ -100,14 +94,20 @@ mi_data <- function(
10094 # Prepare query parameters
10195 query_params <- list (
10296 `_level` = level ,
103- `_year` = as.character(year ),
104- `X_JSON` = x_json_string ,
10597 `limit` = limit
10698 )
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 )
109109 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 )
111111 }
112112
113113 # Perform API request
@@ -124,5 +124,30 @@ mi_data <- function(
124124 response_data <- httr2 :: resp_body_json(response , simplifyVector = TRUE ) | >
125125 tibble :: as_tibble()
126126
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+
127152 return (response_data )
128153}
0 commit comments