#' Intercept the warnings sometimes generated by `readr::read_csv` and repair
#' problems we're aware of.
#'
#' IPEDS CSVs aren't always perfectly well-formed, so sometimes readr reports
#' parsing failures that actually are fine--and we want to be able to reassure
#' the user when this is the case.
#'
#' @importFrom magrittr %>%
#' @param parsed A tibble of data as returned by `readr::read_csv`.
#' @param csv_filename A string filename for the CSV that was read.
#' @return A tibble of data with known problems corrected.
resolve_problems <- function(parsed, csv_filename) {
problems <- parsed %>%
readr::problems()
remaining_problems <- tibble::tribble(
~row, ~col, ~expected, ~actual, ~file)
# TODO: Step through each file we know about. Correct what we do know about
# (in `parsed`), and bind everything else to `remaining_problems` like so:
remaining_problems <- remaining_problems %>%
dplyr::bind_rows(problems)
warn_problems(remaining_problems, csv_filename)
parsed
}
#' Generate logging messages to replace the warnings sometimes generated by
#' `readr::read_csv`.
#'
#' @importFrom magrittr %>%
#' @param problems A tibble of data as returned by `readr::problems`.
#' @param csv_filename A string filename for the CSV that was read.
#' @return NA
warn_problems <- function(problems, csv_filename) {
problem_count <- problems %>%
nrow()
if (problem_count > 0) {
problems <- problems %>%
dplyr::mutate(file = csv_filename)
# Formatting borrowed from readr::warn_problems:
# <https://github.com/tidyverse/readr/blob/v1.3.1/R/problems.R#L69-L97>
warning_output <- problems %>%
as.data.frame() %>%
format(justify = "left")
warning_output <- rbind(names(problems), warning_output) %>%
lapply(format, justify = "right")
warning_output <- do.call(paste, c(warning_output, list(
sep = " ", collapse = "\n")))
warning(
problem_count, " unexpected parsing failure",
ifelse(problem_count > 1, "s", ""), ".\n", warning_output, "\n\n",
"Please file an issue here so we can fix this for others:\n",
"https://github.com/associatedpress/ipedals/issues",
call. = FALSE, immediate. = TRUE, noBreaks. = TRUE)
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.