Skip to content

Commit

Permalink
Merge pull request #570 from yjunechoe/extract-used-columns-lazy
Browse files Browse the repository at this point in the history
Used columns in `col_vals_expr()` are extracted lazily
  • Loading branch information
yjunechoe authored Sep 14, 2024
2 parents 8287cc4 + 63ff883 commit 4688c5f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
6 changes: 1 addition & 5 deletions R/col_vals_expr.R
Original file line number Diff line number Diff line change
Expand Up @@ -339,10 +339,6 @@ col_vals_expr <- function(
}
}

# Extract columns from expr
data_cols <- colnames(apply_preconditions_for_cols(x, preconditions))
columns <- all_data_vars(expr, data_cols)

# Resolve segments into list
segments_list <-
resolve_segments(
Expand Down Expand Up @@ -404,7 +400,7 @@ col_vals_expr <- function(
assertion_type = "col_vals_expr",
i_o = i_o,
columns_expr = NA_character_,
column = columns,
column = NA_character_,
values = expr,
preconditions = preconditions,
seg_expr = segments,
Expand Down
7 changes: 7 additions & 0 deletions R/interrogate.R
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,13 @@ interrogate <- function(
assertion_type = assertion_type
)

if (assertion_type == "col_vals_expr") {
# Extract columns from expr and update validation set with used columns
expr <- get_values_at_idx(agent = agent, idx = i)[[1]]
columns <- all_data_vars(expr, data_cols = colnames(table))
agent$validation_set[[i, "column"]] <- list(columns)
}

} else if (assertion_type == "conjointly") {

validation_formulas <- get_values_at_idx(agent = agent, idx = i)
Expand Down
25 changes: 25 additions & 0 deletions tests/testthat/test-interrogate_simple.R
Original file line number Diff line number Diff line change
Expand Up @@ -2155,3 +2155,28 @@ test_that("class preserved in `value`", {
expect_true(agent$validation_set$eval_error)

})

test_that("col_vals_expr extracts used columns lazily", {

agent_uninterrogated <- create_agent(tbl = iris) |>
col_vals_expr(~ Petal.Length > Petal.Width)
expect_equal(
agent_uninterrogated$validation_set$column[[1]],
NA_character_
)

agent_interrogated <- agent_uninterrogated %>%
interrogate()
expect_equal(
agent_interrogated$validation_set$column[[1]],
c("Petal.Length", "Petal.Width")
)

agent_inactive <- create_agent(tbl = iris) |>
col_vals_expr(~ Petal.Length > Petal.Width, active = FALSE)
expect_equal(
agent_inactive$validation_set$column[[1]],
NA_character_
)

})

0 comments on commit 4688c5f

Please sign in to comment.