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

Used columns in col_vals_expr() are extracted lazily #570

Merged
merged 2 commits into from
Sep 14, 2024
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
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_
)

})
Loading