Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
sharla_diff <- function(df, expected_df) {
data_as_expected <- dplyr::all_equal(expected_df, df)
if (!isTRUE(data_as_expected)) {
data_diffs <- janitor::compare_df_cols(expected_df, df)
cols_mismatch <- dplyr::filter(data_diffs, is.na(expected_df) | is.na(df))
extra_cols <- cols_mismatch %>%
dplyr::filter(is.na(expected_df)) %>%
dplyr::pull(column_name)
missing_cols <- cols_mismatch %>%
dplyr::filter(is.na(df)) %>%
dplyr::pull(column_name)
type_mismatch <- dplyr::filter(data_diffs, expected_df != df)
if (length(extra_cols) > 0) {
warning("`", deparse(substitute(df)), "`", " contains extra column(s): ",
glue::glue_collapse(extra_cols, sep = ", "), ".",
call. = FALSE
)
}
if (length(missing_cols) > 0) {
stop("`", deparse(substitute(df)), "`", " is missing column(s): ", glue::glue_collapse(missing_cols, sep = ", "), ".",
call. = FALSE
)
}
if (nrow(type_mismatch) > 0) {
type_mismatch_errors <- type_mismatch %>%
dplyr::mutate(error = glue::glue("{column_name} should be class {expected_df}, is {df}")) %>%
dplyr::pull(error) %>%
as.character() %>%
paste0("\n")
stop("`", deparse(substitute(df)), "`", " column types are not as expected. These columns have issues:\n", type_mismatch_errors,
call. = FALSE
)
}
}
df
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.