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

Adds ys_prune #97

Merged
merged 6 commits into from
Jan 3, 2022
Merged
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
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export(ys_mild_sanitize)
export(ys_namespace)
export(ys_project)
export(ys_project_file)
export(ys_prune)
export(ys_rename)
export(ys_sanitize)
export(ys_select)
Expand Down
52 changes: 52 additions & 0 deletions R/class-yspec.R
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,58 @@ ys_add_labels <- function(data,spec,fun=label.ycol) {
data
}


#' Prune a data frame, keeping columns in a yspec object
#'
#' Use this to scavenge a data frame for columns that you want to keep. Do not
#' use this for final column selection; use [dplyr::select()] instead.
#'
#' @param data a data frame with at least one column that is found in `spec`
#' @param spec a `yspec` object
#' @param report if `TRUE`, report missing columns
#'
#' @examples
#' data <- ys_help$data()
#' spec <- ys_help$spec()
#' data$STUDY <- NULL
#'
#' head(ys_prune(data, spec))
#' head(ys_prune(data, spec, report = TRUE))
#'
#' # Use this for final subsetting
#' # It will fail if all the columns aren't there
#' data <- ys_help$data()
#' head(dplyr::select(data, names(spec)))
#'
#' @details
#' An error is generated if there are no columns in common between `data` and
#' `spec`.
#'
#' @return
#' A data frame with common columns with `spec`, in the order they appear
#' in `spec`.
#'
#' @md
#' @export
ys_prune <- function(data, spec, report = FALSE) {
assert_that(is.data.frame(data))
assert_that(is_yspec(spec))
# spec positions for matching names in the data set
igrab <- sort(match(names(data), names(spec)), na.last = NA)
if(length(igrab)==0) {
stop("there are no names common between `data` and `spec`", call. = FALSE)
}
# convert igrab to names in spec, ordered by spec; this is what we'll take
grab <- names(spec)[igrab]
if(isTRUE(report)) {
missing <- setdiff(names(spec), names(data))
for(col in missing) {
message("Column not found: ", col)
}
}
data[, grab, drop = FALSE]
}

as_spec_list <- function(...) {
x <- list(...)
names(x) <- map_chr(map(x,get_meta),"name")
Expand Down
41 changes: 41 additions & 0 deletions man/ys_prune.Rd

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

34 changes: 34 additions & 0 deletions tests/testthat/test-prune.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
library(yspec)
library(testthat)

context("test-prune")

test_that("ys_prune selects available columns", {
data <- ys_help$data()
spec <- ys_help$spec()
data$STUDY <- NULL
data$TAD <- NULL
data$FOO <- 1
data$BAR <- 2
set.seed(1103)
data <- data[, sample(names(data)), drop = FALSE]
ans <- ys_prune(data, spec)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kylebaron

should this test have a data frame with more columns than the spec?

For example

  • data has columns: A, B, C
  • spec has columns: A, B

The current test has the reverse, and therefore:

names(ys_prune(data, spec)) == names(data)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes; I was intending fewer columns. Can add more columns too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added two columns matching nothing.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kylebaron

One other question - if I add:

data <- data[, rev(names(data)), drop = FALSE]

after line 12 (data$BAR <- 2) but before line 13 (ans <- ys_prune(data, spec))), the test then fails.

Is this OK?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @andersone1 ; yes, there was something wrong in the logic for matching / ordering the names.

  • Fixed the logic in ys_prune
  • Randomized the names in data in the test
  • Added expectations for reporting columns not there

Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reverted that one expectation; I do think we want this to be the check:

  expect_equal(names(ans), names(spec2))

The names in the final answer are identical (including order) to the names in the spec, minus the columns that we dropped.

spec2 <- ys_select(spec, -STUDY, -TAD)
expect_identical(class(data), class(ans))
expect_true(is.data.frame(ans))
expect_equal(names(ans), names(spec2))
expect_error(
ys_prune(data.frame(a = 2), spec),
regexp = "there are no names common between"
)
expect_message(
ans <- ys_prune(data, spec, report = TRUE),
regexp = "Column not found: STUDY",
all = FALSE, fixed = TRUE
)
expect_message(
ans <- ys_prune(data, spec, report = TRUE),
regexp = "Column not found: TAD",
all = FALSE, fixed = TRUE
)
})