Skip to content

Commit

Permalink
feat: added human-readable string represtations of filtering classes
Browse files Browse the repository at this point in the history
* minor improvements to docs

Related to insightsengineering/teal.reporter#8
  • Loading branch information
kpagacz committed May 12, 2022
1 parent 49ab696 commit 0ede485
Show file tree
Hide file tree
Showing 18 changed files with 452 additions and 237 deletions.
69 changes: 57 additions & 12 deletions R/FilterState.R
Original file line number Diff line number Diff line change
Expand Up @@ -362,13 +362,15 @@ FilterState <- R6::R6Class( # nolint
#' @return `character(1)` the formatted string
#'
format = function(indent = 0) {
checkmate::assert_number(indent, finite = TRUE)
checkmate::assert_number(indent, finite = TRUE, lower = 0)

whitespace_indent <- paste0(rep(" ", indent), collapse = "")
formatted <- c(paste0(whitespace_indent, "Filtering on: ", self$get_varname()))
selected <- paste0(format(self$get_selected(), nsmall = 3), collapse = " ")
formatted <- c(formatted, paste0(whitespace_indent, " ", "Selected: ", selected))
paste(formatted, collapse = "\n")
sprintf(
"%sFiltering on: %s\n%1$s Selected values: %s\n%1$s Include missing values: %s",
format("", width = indent),
self$get_varname(deparse = TRUE),
paste0(format(self$get_selected(), nsmall = 3), collapse = " "),
format(self$get_keep_na())
)
},

#' @description
Expand Down Expand Up @@ -1296,13 +1298,16 @@ RangeFilterState <- R6::R6Class( # nolint
#' @return `character(1)` the formatted string
#'
format = function(indent = 0) {
checkmate::assert_number(indent, finite = TRUE)
checkmate::assert_number(indent, finite = TRUE, lower = 0)

whitespace_indent <- paste0(rep(" ", indent), collapse = "")
formatted <- c(paste0(whitespace_indent, "Filtering on: ", self$get_varname()))
selected <- format(self$get_selected(), nsmall = 3)
formatted <- c(formatted, paste0(whitespace_indent, " ", "Range: ", selected[1], " - ", selected[2]))
paste(formatted, collapse = "\n")
sprintf(
"%sFiltering on: %s\n%1$s Selected range: %s - %s\n%1$s Include missing values: %s",
format("", width = indent),
self$get_varname(deparse = TRUE),
format(self$get_selected(), nsmall = 3)[1],
format(self$get_selected(), nsmall = 3)[2],
format(self$get_keep_na())
)
},

#' @description
Expand Down Expand Up @@ -2082,6 +2087,26 @@ DateFilterState <- R6::R6Class( # nolint
return(invisible(self))
},

#' @description
#' Returns a formatted string representing this `DateFilterState`.
#'
#' @param indent (`numeric(1)`) the number of spaces before after each new line character of the formatted string.
#' Default: 0
#' @return `character(1)` the formatted string
#'
format = function(indent = 0) {
checkmate::assert_number(indent, finite = TRUE, lower = 0)

sprintf(
"%sFiltering on: %s\n%1$s Selected range: %s - %s\n%1$s Include missing values: %s",
format("", width = indent),
self$get_varname(deparse = TRUE),
format(self$get_selected(), nsmall = 3)[1],
format(self$get_selected(), nsmall = 3)[2],
format(self$get_keep_na())
)
},

#' @description
#' Answers the question of whether the current settings and values selected actually filters out any values.
#' @return logical scalar
Expand Down Expand Up @@ -2380,6 +2405,26 @@ DatetimeFilterState <- R6::R6Class( # nolint
return(invisible(self))
},

#' @description
#' Returns a formatted string representing this `DatetimeFilterState`.
#'
#' @param indent (`numeric(1)`) the number of spaces before after each new line character of the formatted string.
#' Default: 0
#' @return `character(1)` the formatted string
#'
format = function(indent = 0) {
checkmate::assert_number(indent, finite = TRUE, lower = 0)

sprintf(
"%sFiltering on: %s\n%1$s Selected range: %s - %s\n%1$s Include missing values: %s",
format("", width = indent),
self$get_varname(deparse = TRUE),
format(self$get_selected(), nsmall = 3)[1],
format(self$get_selected(), nsmall = 3)[2],
format(self$get_keep_na())
)
},

#' @description
#' Answers the question of whether the current settings and values selected actually filters out any values.
#' @return logical scalar
Expand Down
61 changes: 28 additions & 33 deletions R/FilterStates.R
Original file line number Diff line number Diff line change
Expand Up @@ -665,14 +665,15 @@ FilterStates <- R6::R6Class( # nolint
)
)

#' Specialization of `FilterStates` for a base `data.frame`.
#' @title DFFFilterStates
#' @description Specialization of `FilterStates` for a base `data.frame`.
#'
#' @keywords internal
DFFilterStates <- R6::R6Class( # nolint
classname = "DFFilterStates",
inherit = FilterStates,
public = list(
#' Initializes `DFFilterStates` object
#' @description Initializes `DFFilterStates` object
#'
#' Initializes `DFFilterStates` object by setting `input_dataname`,
#' `output_dataname` and initializing `ReactiveQueue`. This class contains a
Expand Down Expand Up @@ -712,14 +713,13 @@ DFFilterStates <- R6::R6Class( # nolint
#'
#' @param indent (`numeric(1)`) the number of spaces before each line of the representation
#' @return `character(1)` the formatted string
#' @examples
#'
format = function(indent = 0) {
formatted_states <- c()
checkmate::assert_number(indent, finite = TRUE, lower = 0)

for (state in self$queue_get(1L)) {
formatted_states <- c(formatted_states, paste0(state$format(indent = indent)))
}
formatted_states <- vapply(
self$queue_get(1L), function(state) state$format(indent = indent),
USE.NAMES = FALSE, FUN.VALUE = character(1)
)
paste(formatted_states, collapse = "\n")
},

Expand Down Expand Up @@ -1076,13 +1076,14 @@ DFFilterStates <- R6::R6Class( # nolint
)


#' Specialization of `FilterStates` for `MultiAssayExperiment`.
#' @title MAEFilterStates
#' @description Specialization of `FilterStates` for `MultiAssayExperiment`.
#' @keywords internal
MAEFilterStates <- R6::R6Class( # nolint
classname = "MAEFilterStates",
inherit = FilterStates,
public = list(
#' Initialize `MAEFilterStates` object
#' @description Initializes `MAEFilterStates` object
#'
#' Initialize `MAEFilterStates` object
#'
Expand Down Expand Up @@ -1122,24 +1123,19 @@ MAEFilterStates <- R6::R6Class( # nolint
#'
#' @param indent (`numeric(1)`) the number of spaces before each line of the representation
#' @return `character(1)` the formatted string
#' @examples
#'
format = function(indent = 0) {
checkmate::assert_number(indent, finite = TRUE)
checkmate::assert_number(indent, finite = TRUE, lower = 0)

if (length(self$queue_get(1L)) > 0) {
whitespace_indent <- paste0(rep(" ", indent), collapse = "")
formatted_states <- c(paste0(whitespace_indent, "Subject filters:"))
formatted_states <- sprintf("%sSubject filters:", format("", width = indent))
for (state in self$queue_get(1L)) formatted_states <- c(formatted_states, state$format(indent = indent + 2))
paste(formatted_states, collapse = "\n")
}
},

#' Get function name
#'
#' Get function name used to create filter call.
#' For `MAEFilterStates`
#' `MultiAssayExperiment::subsetByColData` is used.
#' @description
#' Returns function name used to create filter call.
#' For `MAEFilterStates` `MultiAssayExperiment::subsetByColData` is used.
#' @return `character(1)`
get_fun = function() {
return("MultiAssayExperiment::subsetByColData")
Expand Down Expand Up @@ -1248,7 +1244,8 @@ MAEFilterStates <- R6::R6Class( # nolint
NULL
},

#' @description Remove a variable from the `ReactiveQueue` and its corresponding UI element.
#' @description
#' Removes a variable from the `ReactiveQueue` and its corresponding UI element.
#'
#' @param element_id (`character(1)`)\cr name of `ReactiveQueue` element.
#'
Expand Down Expand Up @@ -1456,13 +1453,14 @@ MAEFilterStates <- R6::R6Class( # nolint
)
)

#' Specialization of `FilterStates` for `SummaryExperiment`.
#' @title SEFilterStates
#' @description Specialization of `FilterStates` for `SummaryExperiment`.
#' @keywords internal
SEFilterStates <- R6::R6Class( # nolint
classname = "SEFilterStates",
inherit = FilterStates,
public = list(
#' Initialize `SEFilterStates` object
#' @description Initialize `SEFilterStates` object
#'
#' Initialize `SEFilterStates` object
#'
Expand Down Expand Up @@ -1493,12 +1491,10 @@ SEFilterStates <- R6::R6Class( # nolint
#'
#' @param indent (`numeric(1)`) the number of spaces before each line of the representation
#' @return `character(1)` the formatted string
#' @examples
#'
format = function(indent = 0) {
checkmate::assert_number(indent, finite = TRUE)
checkmate::assert_number(indent, finite = TRUE, lower = 0)

whitespace_indent <- paste0(rep(" ", indent), collapse = "")
whitespace_indent <- format("", width = indent)
formatted_states <- c()
if (!is.null(self$queue_get(queue_index = "subset"))) {
formatted_states <- c(formatted_states, paste0(whitespace_indent, " Subsetting:"))
Expand All @@ -1508,7 +1504,7 @@ SEFilterStates <- R6::R6Class( # nolint
}

if (!is.null(self$queue_get(queue_index = "select"))) {
formatted_states <- c(formatted_states, paste0(whitespace_indent, "Selecting:"))
formatted_states <- c(formatted_states, paste0(whitespace_indent, " Selecting:"))
for (state in self$queue_get(queue_index = "select")) {
formatted_states <- c(formatted_states, state$format(indent = indent + 4))
}
Expand Down Expand Up @@ -2027,13 +2023,14 @@ SEFilterStates <- R6::R6Class( # nolint
)
)

#' Specialization of `FilterStates` for a base matrix.
#' @title MatrixFilterStates
#' @description Specialization of `FilterStates` for a base matrix.
#' @keywords internal
MatrixFilterStates <- R6::R6Class( # nolint
classname = "MatrixFilterStates",
inherit = FilterStates,
public = list(
#' Initialize `MatrixFilterStates` object
#' @description Initialize `MatrixFilterStates` object
#'
#' Initialize `MatrixFilterStates` object
#'
Expand All @@ -2060,10 +2057,8 @@ MatrixFilterStates <- R6::R6Class( # nolint
#'
#' @param indent (`numeric(1)`) the number of spaces before each line of the representation
#' @return `character(1)` the formatted string
#' @examples
#'
format = function(indent = 0) {
checkmate::assert_number(indent, finite = TRUE)
checkmate::assert_number(indent, finite = TRUE, lower = 0)

formatted_states <- c()
whitespace_indent <- paste0(rep(" ", indent), collapse = "")
Expand Down
2 changes: 1 addition & 1 deletion R/FilteredDataset.R
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ FilteredDataset <- R6::R6Class( # nolint
#' @return `character(1)` the formatted string representing the filter state
#'
get_formatted_filter_state = function() {
out <- c(paste0("Filters for dataset: ", self$get_dataname()))
out <- paste0("Filters for dataset: ", self$get_dataname())
for (states in self$get_filter_states()) out <- c(out, states$format(indent = 2))
paste(out, collapse = "\n")
},
Expand Down
64 changes: 8 additions & 56 deletions man/DFFilterStates.Rd

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

Loading

0 comments on commit 0ede485

Please sign in to comment.