Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for loading missing values in resources as additional cols #161

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ Imports:
purrr,
readr (>= 2.1.0),
utils,
yaml
yaml,
tidyselect,
rlang
Suggests:
hms,
knitr,
Expand Down
12 changes: 9 additions & 3 deletions R/read_resource.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
#' @param col_select Character vector of the columns to include in the result,
#' in the order provided.
#' Selecting columns can improve read speed.
#' @param channels A character vector representing the data channels to load.
#' Available channels are `"values"` and `"missing"`. Use named vectors to
#' control column suffixes, e.g.
#' `c(values = "__values", missing = "__missing")`.
#' @return A [tibble()] data frame with the Data Resource's tabular data.
#' If there are parsing problems, a warning will alert you.
#' You can retrieve the full details by calling [problems()] on your data
Expand Down Expand Up @@ -199,7 +203,8 @@
#'
#' # Read data from the resource "deployments" with column selection
#' read_resource(package, "deployments", col_select = c("latitude", "longitude"))
read_resource <- function(package, resource_name, col_select = NULL) {
read_resource <- function(package, resource_name, col_select = NULL,
channels = c("values")) {
# Get resource, includes check_package()
resource <- get_resource(package, resource_name)

Expand Down Expand Up @@ -361,10 +366,11 @@ read_resource <- function(package, resource_name, col_select = NULL) {
} else if (resource$read_from == "path" || resource$read_from == "url") {
dataframes <- list()
for (i in seq_along(paths)) {
data <- readr::read_delim(
data <- read_delim_ext(
file = paths[i],
delim = replace_null(dialect$delimiter, ","),
quote = replace_null(dialect$quoteChar, "\""),
channels = channels,
escape_backslash = ifelse(
replace_null(dialect$escapeChar, "not set") == "\\", TRUE, FALSE
),
Expand All @@ -385,7 +391,7 @@ read_resource <- function(package, resource_name, col_select = NULL) {
trim_ws = replace_null(dialect$skipInitialSpace, FALSE),
# Skip header row when present
skip = ifelse(replace_null(dialect$header, TRUE), 1, 0),
skip_empty_rows = TRUE
skip_empty_rows = TRUE,
)
dataframes[[i]] <- data
}
Expand Down
111 changes: 111 additions & 0 deletions R/utils_ext.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#' Standardize channel names / suffixes from channel options
#'
#' @param channels A character vector representing the data channels to load.
#' Available channels are `"values"` and `"missing"`. Use named vectors to
#' control column suffixes, e.g.
#' `c(values = "_values", missing = "_missing")`.
#' @return A named list. Names are channel names, values are suffixes.
#' @family helper functions
#' @noRd
channel_opt_standardize <- function(channels) {
default_suffixes <- c(
values = "_values",
missing = "_missing"
)

if (is.null(names(channels))) {
names(channels) <- rlang::rep_along(channels, "")
}

if (length(channels) == 1 && names(channels) == "") {
channel_suffixes <- ""
} else {
channel_suffixes <- purrr::map2_vec(
channels,
names(channels),
\(x, n) if (n == "") { default_suffixes[[x]] } else { x }

Check warning on line 26 in R/utils_ext.R

View check run for this annotation

Codecov / codecov/patch

R/utils_ext.R#L23-L26

Added lines #L23 - L26 were not covered by tests
)
}

channel_names <- purrr::map2_vec(
channels,
names(channels),
\(x, n) dplyr::if_else(n == "", x, n)
)

purrr::set_names(channel_suffixes, channel_names)
}

#' Proposed extension of readr::read_delim
#'
#' Adds a `channels` argument that facilitates the loading of missing values
#'
#'
#' @inheritParams readr::read_delim
#' @param channels A character vector representing the data channels to load.
#' Available channels are `"values"` and `"missing"`. Use named vectors to
#' control column suffixes, e.g.
#' `c(values = "_values", missing = "_missing")`.
#' @return A `tibble()` of loaded values.
#' @family helper functions
#' @noRd
read_delim_ext <- function(file, delim, na = c("", "NA"), col_types = NULL,
col_select = NULL, channels = "values", ...) {

channels <- channel_opt_standardize(channels)

if (is.null(col_select)) {
selected_col_types <- col_types
} else {
selected_col_types <- col_types[tidyselect::eval_select(
rlang::enquo(col_select),
col_types,
)]
}

string_df <- readr::read_delim(
file,
delim,
col_select = {{col_select}},
col_types = readr::cols(.default=readr::col_character()),
...
)

result <- NULL

if ("values" %in% names(channels)) {
# Commenting out because https://github.com/tidyverse/readr/issues/1526
#values_df <- string_df |>
# readr::type_convert(
# col_types=selected_col_types,
# na=na,
# ) |>
# dplyr::rename_with(\(x) paste0(x, channels[["values"]]))

# Until the issue is fixed, let's just re-read the csv...
values_df <- readr::read_delim(
file,
delim,
col_types=selected_col_types,
col_select = {{col_select}},
na=na,
...
) |>
dplyr::rename_with(\(x) paste0(x, channels[["values"]]))

result <- dplyr::bind_cols(result, values_df)
}

if ("missing" %in% names(channels)) {
missing_df <- string_df |>
dplyr::mutate(
dplyr::across(
dplyr::everything(),
\(x) dplyr::if_else(x %in% na, x, NA_character_)

Check warning on line 104 in R/utils_ext.R

View check run for this annotation

Codecov / codecov/patch

R/utils_ext.R#L100-L104

Added lines #L100 - L104 were not covered by tests
)
) |>
dplyr::rename_with(\(x) paste0(x, channels[["missing"]]))
result <- dplyr::bind_cols(result, values_df)

Check warning on line 108 in R/utils_ext.R

View check run for this annotation

Codecov / codecov/patch

R/utils_ext.R#L107-L108

Added lines #L107 - L108 were not covered by tests
}
result
}
12 changes: 11 additions & 1 deletion man/read_resource.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.