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

Remove FILTERED #77

Merged
merged 8 commits into from
Jul 26, 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
18 changes: 7 additions & 11 deletions R/CDISCFilteredData.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,29 +97,25 @@ CDISCFilteredData <- R6::R6Class( # nolint

dataset <- self$get_filtered_dataset(dataname)

filtered_dataname <- dataset$get_filtered_dataname()
filtered_dataname_alone <- dataset$get_filtered_dataname(suffix = "_FILTERED_ALONE")
filtered_parentname <- dataset$get_filtered_dataname(dataname = parent_dataname)

premerge_call <- Filter(
f = Negate(is.null),
x = lapply(
dataset$get_filter_states(),
function(x) x$get_call()
)
)
premerge_call[[1]][[2]] <- as.name(filtered_dataname_alone)

merge_call <- call(
"<-",
as.name(filtered_dataname),
as.name(dataname),
call_with_colon(
"dplyr::inner_join",
x = as.name(filtered_dataname_alone),
x = as.name(dataname),
y = if (length(parent_keys) == 0) {
as.name(filtered_parentname)
as.name(parent_dataname)
} else {
call_extract_array(
dataname = filtered_parentname,
dataname = parent_dataname,
column = parent_keys,
aisle = call("=", as.name("drop"), FALSE)
)
Expand Down Expand Up @@ -256,12 +252,12 @@ CDISCFilteredData <- R6::R6Class( # nolint
private$reactive_data[[dataname]] <- reactive({
env <- new.env(parent = parent.env(globalenv()))
env[[dataname]] <- self$get_filtered_dataset(dataname)$get_dataset()
env[[paste0(private$parents[[dataname]], "_FILTERED")]] <-
env[[private$parents[[dataname]]]] <-
private$reactive_data[[private$parents[[dataname]]]]()

filter_call <- self$get_call(dataname)
eval_expr_with_msg(filter_call, env)
get(x = self$get_filtered_dataset(dataname)$get_filtered_dataname(), envir = env)
get(x = dataname, envir = env)
})
}

Expand Down
2 changes: 1 addition & 1 deletion R/FilterStates.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#' rf <- teal.slice:::init_filter_states(
#' data = df,
#' input_dataname = "DF",
#' output_dataname = "DF_FILTERED",
#' output_dataname = "DF_OUTPUT",
#' varlabels = c(
#' "character variable", "numeric variable", "date variable", "datetime variable"
#' )
Expand Down
6 changes: 1 addition & 5 deletions R/FilteredData.R
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ FilteredData <- R6::R6Class( # nolint
env[[dataname]] <- self$get_filtered_dataset(dataname)$get_dataset()
filter_call <- self$get_call(dataname)
eval_expr_with_msg(filter_call, env)
get(x = self$get_filtered_dataset(dataname)$get_filtered_dataname(), envir = env)
get(x = dataname, envir = env)
})

invisible(self)
Expand Down Expand Up @@ -975,10 +975,6 @@ FilteredData <- R6::R6Class( # nolint
})

return(invisible(NULL))
},
filtered_dataname = function(dataname) {
checkmate::assert_string(dataname)
sprintf("%s_FILTERED", dataname)
}
)
)
Expand Down
19 changes: 6 additions & 13 deletions R/FilteredDataset.R
Original file line number Diff line number Diff line change
Expand Up @@ -300,17 +300,6 @@ FilteredDataset <- R6::R6Class( # nolint
colnames(self$get_dataset())
},

#' @description
#' Gets the suffixed dataname
#' Used when filtering the data to get `<dataname>_FILTERED`,
#' `<dataname>_FILTERED_ALONE` or any other name.
#' @param dataname (`character(1)`) dataname
#' @param suffix (`character(1)`) string to be putted after dataname
#' @return `character(1)`
get_filtered_dataname = function(dataname = self$get_dataname(), suffix = "_FILTERED") {
paste0(dataname, suffix)
},

#' @description
#' Gets variable names for the filtering.
#'
Expand Down Expand Up @@ -543,7 +532,7 @@ DefaultFilteredDataset <- R6::R6Class( # nolint
filter_states = init_filter_states(
data = self$get_dataset(),
input_dataname = as.name(dataname),
output_dataname = as.name(sprintf("%s_FILTERED", dataname)),
output_dataname = as.name(dataname),
varlabels = self$get_varlabels(),
keys = self$get_keys()
),
Expand All @@ -563,13 +552,17 @@ DefaultFilteredDataset <- R6::R6Class( # nolint
#' applies to one argument (`...`) in `dplyr::filter` call.
#' @return filter `call` or `list` of filter calls
get_call = function() {
Filter(
filter_call <- Filter(
f = Negate(is.null),
x = lapply(
self$get_filter_states(),
function(x) x$get_call()
)
)
if (length(filter_call) == 0) {
return(NULL)
}
filter_call
},

#' @description
Expand Down
10 changes: 7 additions & 3 deletions R/MAEFilteredDataset.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ MAEFilteredDataset <- R6::R6Class( # nolint
filter_states = init_filter_states(
data = dataset,
input_dataname = as.name(dataname),
output_dataname = as.name(sprintf("%s_FILTERED", dataname)),
output_dataname = as.name(dataname),
varlabels = self$get_varlabels(),
datalabel = "subjects",
keys = self$get_keys()
Expand All @@ -49,7 +49,7 @@ MAEFilteredDataset <- R6::R6Class( # nolint
experiment_names,
function(experiment_name) {
input_dataname <- call_extract_list(
sprintf("%s_FILTERED", dataname),
dataname,
experiment_name,
dollar = FALSE
)
Expand Down Expand Up @@ -82,13 +82,17 @@ MAEFilteredDataset <- R6::R6Class( # nolint
#' }
#' @return filter `call` or `list` of filter calls
get_call = function() {
Filter(
filter_call <- Filter(
f = Negate(is.null),
x = lapply(
self$get_filter_states(),
function(x) x$get_call()
)
)
if (length(filter_call) == 0) {
return(NULL)
}
filter_call
},

#' @description
Expand Down
8 changes: 0 additions & 8 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ check_ellipsis <- function(..., stop = FALSE, allowed_args = character(0)) {
#'
#' @md
#'
#' @note
#' The suffix '_FILTERED' is reserved for filtered data and is not
#' allowed in the dataset name.
#'
#' @param name `character, single or vector` name to check
#' @keywords internal
#'
Expand All @@ -82,7 +78,6 @@ check_ellipsis <- function(..., stop = FALSE, allowed_args = character(0)) {
#' teal.slice:::check_simple_name("1a")
#' teal.slice:::check_simple_name("ADSL.modified")
#' teal.slice:::check_simple_name("a1...")
#' teal.slice:::check_simple_name("ADSL_FILTERED")
#' }
check_simple_name <- function(name) {
checkmate::assert_character(name, min.len = 1, any.missing = FALSE)
Expand All @@ -94,9 +89,6 @@ check_simple_name <- function(name) {
" and the first character must be an alphabetic character"
)
}
if (grepl("_FILTERED$", name, perl = TRUE)) {
stop("name '", name, "' cannot end with the special string '_FILTERED'")
}
}

#' Check that a given range is valid
Expand Down
1 change: 0 additions & 1 deletion man/DefaultFilteredDataset.Rd

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

28 changes: 0 additions & 28 deletions man/FilteredDataset.Rd

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

1 change: 0 additions & 1 deletion man/MAEFilteredDataset.Rd

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

5 changes: 0 additions & 5 deletions man/check_simple_name.Rd

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

2 changes: 1 addition & 1 deletion man/init_filter_states.Rd

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

11 changes: 3 additions & 8 deletions tests/testthat/test-CDISCFilteredData.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ testthat::test_that("set filter state", {
queue <- ds$get_filtered_dataset("ADSL")$get_filter_states(1)
queue$queue_push(filter_state_adsl, queue_index = 1L, element_id = "sex")

testthat::expect_identical(
testthat::expect_null(
isolate(queue$get_call()),
quote(ADSL_FILTERED <- ADSL) # nolint
)
})

Expand All @@ -57,13 +56,9 @@ testthat::test_that("get_call for child dataset includes filter call for parent

# no filtering as AESEQ filter does not filter any calls
testthat::expect_equal(
deparse1(adae_call[[1]]), "ADAE_FILTERED_ALONE <- ADAE"
)

testthat::expect_equal(
deparse1(adae_call[[2]]),
deparse1(adae_call[[1]]),
paste0(
"ADAE_FILTERED <- dplyr::inner_join(x = ADAE_FILTERED_ALONE, y = ADSL_FILTERED[, ",
"ADAE <- dplyr::inner_join(x = ADAE, y = ADSL[, ",
"c(\"STUDYID\", \"USUBJID\"), drop = FALSE], by = c(\"STUDYID\", ",
"\"USUBJID\"))"
)
Expand Down
24 changes: 14 additions & 10 deletions tests/testthat/test-DefaultFilteredDataset.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@ testthat::test_that("The constructor accepts a data.frame object with a dataname
), NA)
})

testthat::test_that("get_call returns a list of calls", {
testthat::test_that("get_call returns a list of calls or NULL", {
filtered_dataset <- DefaultFilteredDataset$new(
dataset = head(iris), dataname = "iris"
dataset = iris, dataname = "iris"
)
testthat::expect_null(filtered_dataset$get_call())
fs <- list(
Sepal.Length = c(5.1, 6.4),
Species = c("setosa", "versicolor")
)
checkmate::expect_list(filtered_dataset$get_call(), types = "<-")
filtered_dataset$set_filter_state(state = fs)

checkmate::expect_list(isolate(filtered_dataset$get_call()), types = "<-")
})

testthat::test_that(
Expand All @@ -25,7 +32,7 @@ testthat::test_that(
isolate(dataset$get_call()),
list(
filter = quote(
iris_FILTERED <- dplyr::filter( # nolint
iris <- dplyr::filter(
iris,
Sepal.Length >= 5.1 & Sepal.Length <= 6.4 &
Species %in% c("setosa", "versicolor")
Expand Down Expand Up @@ -63,7 +70,7 @@ testthat::test_that(
isolate(dataset$get_call()),
list(
filter = quote(
iris_FILTERED <- dplyr::filter( # nolint
iris <- dplyr::filter(
iris,
Sepal.Length >= 5.1 & Sepal.Length <= 6.4
)
Expand Down Expand Up @@ -97,11 +104,8 @@ testthat::test_that(
dataset$set_filter_state(state = fs)
dataset$remove_filter_state(c("Species", "Sepal.Length"))

testthat::expect_equal(
isolate(dataset$get_call()),
list(
filter = quote(iris_FILTERED <- iris) # nolint
)
testthat::expect_null(
isolate(dataset$get_call())
)
}
)
Expand Down
Loading