Last active
August 17, 2021 15:28
-
-
Save sharlagelfand/4d2713ca2cdd106bdecfca0130007a5f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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