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

Corrects mapping of parent join_keys columns when merging 2 data frames #503

Merged
merged 4 commits into from
Dec 22, 2023
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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Imports:
shinycssloaders (>= 1.0.0),
shinyjs,
shinyWidgets (>= 0.6.2),
teal.data (>= 0.3.0.9018),
teal.data (>= 0.3.0.9029),
teal.logger (>= 0.1.1),
teal.widgets (>= 0.4.0),
utils
Expand Down
7 changes: 3 additions & 4 deletions R/FilteredDatasetDataframe.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ DataframeFilteredDataset <- R6::R6Class( # nolint

if (!identical(parent_dataname, character(0))) {
join_keys <- private$join_keys
parent_keys <- names(join_keys)
dataset_keys <- unname(join_keys)
parent_keys <- unname(join_keys)
dataset_keys <- names(join_keys)

y_arg <- if (length(parent_keys) == 0L) {
parent_dataname
Expand Down Expand Up @@ -240,11 +240,10 @@ DataframeFilteredDataset <- R6::R6Class( # nolint
# Gets filter overview subjects number and returns a list
# of the number of subjects of filtered/non-filtered datasets
subject_keys <- if (length(private$parent_name) > 0) {
private$join_keys
names(private$join_keys)
} else {
self$get_keys()
}

dataset <- self$get_dataset()
data_filtered <- self$get_dataset(TRUE)
if (length(subject_keys) == 0) {
Expand Down
37 changes: 29 additions & 8 deletions tests/testthat/test-FilteredData.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ testthat::test_that("filtered_data$datanames returns character vector of dataset

testthat::test_that("datanames are ordered topologically from parent to child", {
jk <- teal.data::join_keys(teal.data::join_key("parent", "child", c("id" = "id")))
teal.data::parents(jk) <- list(child = "parent")
iris2 <- transform(iris, id = seq_len(nrow(iris)))
filtered_data <- FilteredData$new(
list(
Expand Down Expand Up @@ -94,7 +93,6 @@ testthat::test_that("set_datasets creates FilteredDataset object linked with par
)
)
jk <- teal.data::join_keys(teal.data::join_key("parent", "child", c("id" = "id")))
teal.data::parents(jk) <- list(child = "parent")
iris2 <- transform(iris, id = seq_len(nrow(iris)))
filtered_data <- test_class$new(data_objects = list(), join_keys = jk)
filtered_data$set_dataset(data = head(iris), dataname = "parent")
Expand Down Expand Up @@ -269,10 +267,9 @@ testthat::test_that("get_data returns an object filtered by set filters", {

testthat::test_that("get_data of the child is dependent on the ancestor filter", {
jk <- teal.data::join_keys(
teal.data::join_key("child", "parent", c("id" = "id")),
teal.data::join_key("grandchild", "child", c("id" = "id"))
teal.data::join_key("parent", "child", c("id" = "id")),
teal.data::join_key("child", "grandchild", c("id" = "id"))
)
teal.data::parents(jk) <- list(child = "parent", grandchild = "child")
iris2 <- transform(iris, id = seq_len(nrow(iris)))
filtered_data <- FilteredData$new(
list(
Expand All @@ -292,6 +289,31 @@ testthat::test_that("get_data of the child is dependent on the ancestor filter",
)
})

testthat::test_that("get_data of the child is dependent on the ancestor filter (mismatched columns)", {
jk <- teal.data::join_keys(
teal.data::join_key("parent", "child", c("pk" = "id")),
teal.data::join_key("child", "grandchild", c("id" = "id"))
)
iris2 <- transform(iris, id = seq_len(nrow(iris)))
iris_parent <- dplyr::rename(iris2, pk = "id")
filtered_data <- FilteredData$new(
list(
grandchild = list(dataset = head(iris2)),
child = list(dataset = head(iris2)),
parent = list(dataset = head(iris_parent))
),
join_keys = jk
)
filtered_data$set_filter_state(teal_slices(
teal_slice(dataname = "parent", varname = "pk", selected = c(1, 1), keep_na = FALSE, keep_inf = FALSE)
))

testthat::expect_identical(
shiny::isolate(filtered_data$get_data("grandchild", filtered = TRUE)),
dplyr::filter(iris2, id == 1)
)
})

# get_filter_state ----
testthat::test_that("get_filter_state returns `teal_slices` with features identical to those in input", {
datasets <- FilteredData$new(
Expand Down Expand Up @@ -603,10 +625,9 @@ testthat::test_that("get_filter_overview returns overview data.frame with filter

testthat::test_that("get_filter_overview return counts based on reactive filtering by ancestors", {
jk <- teal.data::join_keys(
teal.data::join_key("child", "parent", c("id" = "id")),
teal.data::join_key("grandchild", "child", c("id" = "id"))
teal.data::join_key("parent", "child", c("id" = "id")),
teal.data::join_key("child", "grandchild", c("id" = "id"))
)
teal.data::parents(jk) <- list(child = "parent", grandchild = "child")
iris2 <- transform(iris, id = seq_len(nrow(iris)))
filtered_data <- FilteredData$new(
list(
Expand Down