Skip to content

Commit fbb5d78

Browse files
committed
guarantee column order and unify column names
1 parent 6587531 commit fbb5d78

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

R/data.R

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,14 @@
1212
#'
1313
#' @return A `tibble` with the following columns:
1414
#'
15-
#' * `data_year`: the year of the data point.
1615
#' * `geo`: code for the (NUTS) region at the requested level.
1716
#' * `geo_name`: name of the (NUTS) region at the requested level.
1817
#' * `geo_source`: source (type) of the spatial units at the requested level.
1918
#' * `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).
2321
#' * `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).
2523
#'
2624
#' @export
2725
#'
@@ -126,5 +124,30 @@ mi_data <- function(
126124
response_data <- httr2::resp_body_json(response, simplifyVector = TRUE) |>
127125
tibble::as_tibble()
128126

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+
129152
return(response_data)
130153
}

man/mi_data.Rd

Lines changed: 3 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)