|
12 | 12 | #'
|
13 | 13 | #' @return A `tibble` with the following columns:
|
14 | 14 | #'
|
15 |
| -#' * `data_year`: the year of the data point. |
16 | 15 | #' * `geo`: code for the (NUTS) region at the requested level.
|
17 | 16 | #' * `geo_name`: name of the (NUTS) region at the requested level.
|
18 | 17 | #' * `geo_source`: source (type) of the spatial units at the requested level.
|
19 | 18 | #' * `geo_year`: year of the (NUTS) region at the requested level.
|
20 |
| -#' * `data_year` (int): The year of the data point (if only `x_source` is provided). |
21 |
| -#' * `predictor_year` (int, optional): The year of the predictor variable (X), included in bivariate requests (only included when `y_source` is provided). |
22 |
| -#' * `outcome_year` (int, optional): The year of the outcome variable (Y), included in bivariate requests (only included when `y_source` is provided). |
| 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). |
23 | 21 | #' * `x`: the value of the univariate variable.
|
24 |
| -#' * `y`: the value of the y variable (for bivariate data when `y_source` is provided). |
| 22 | +#' * `y` (optional): the value of the y variable (only included when `y_source` is provided). |
25 | 23 | #'
|
26 | 24 | #' @export
|
27 | 25 | #'
|
@@ -126,5 +124,30 @@ mi_data <- function(
|
126 | 124 | response_data <- httr2::resp_body_json(response, simplifyVector = TRUE) |>
|
127 | 125 | tibble::as_tibble()
|
128 | 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 | + |
129 | 152 | return(response_data)
|
130 | 153 | }
|
0 commit comments