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

feat: added formatting function for filter panel classes #28

Merged
merged 12 commits into from
May 17, 2022
92 changes: 90 additions & 2 deletions R/FilterState.R
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,25 @@ FilterState <- R6::R6Class( # nolint
return(invisible(NULL))
},

#' @description
#' Returns a formatted string representing this `FilterState`.
#'
#' @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 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
#' Returns reproducible condition call for current selection relevant
#' for selected variable type.
Expand All @@ -370,7 +389,7 @@ FilterState <- R6::R6Class( # nolint
#' @return (`name` or `character(1)`)
get_dataname = function(deparse = TRUE) {
if (isTRUE(deparse)) {
deparse(private$input_dataname)
deparse1(private$input_dataname)
} else {
private$input_dataname
}
Expand All @@ -397,7 +416,7 @@ FilterState <- R6::R6Class( # nolint
#' @return (`name` or `character(1)`)
get_varname = function(deparse = FALSE) {
if (isTRUE(deparse)) {
deparse(private$varname)
deparse1(private$varname)
} else {
private$varname
}
Expand All @@ -424,6 +443,15 @@ FilterState <- R6::R6Class( # nolint
)
},

#' @description
#' Prints this `FilterState` object
#'
#' @param ... additional arguments to this method
print = function(...) {
cat(shiny::isolate(self$format()))
cat("\n")
kpagacz marked this conversation as resolved.
Show resolved Hide resolved
},

#' @description
#' Set if `NA` should be kept
#' @param value (`logical(1)`)\cr
Expand Down Expand Up @@ -1271,6 +1299,26 @@ RangeFilterState <- R6::R6Class( # nolint
return(invisible(self))
},

#' @description
#' Returns a formatted string representing this `LogicalFilterState`.
#'
#' @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())
)
Comment on lines +1311 to +1318
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RangeFilterState handles also Inf

Suggested change
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())
)
sprintf(
"%sFiltering on: %s\n%1$s Selected range: %s - %s\n%1$s Include missing values: %s\n%1$s Include infinite 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()),
format(self$get_keep_inf())
)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's consequential for a human readable output to be honest. It was a conscious decision.

},

#' @description
#' Answers the question of whether the current settings and values selected actually filters out any values.
#' @return logical scalar
Expand Down Expand Up @@ -2048,6 +2096,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()[1], nsmall = 3),
format(self$get_selected()[2], nsmall = 3),
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 @@ -2346,6 +2414,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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should these be assert_integerish ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The whole method doesn't throw even when passed a fraction but good point. Probably yes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's because format implicitly casts width to an integer XD


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
132 changes: 119 additions & 13 deletions R/FilterStates.R
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,24 @@ FilterStates <- R6::R6Class( # nolint
logger::log_trace("Instantiated { class(self)[1] }, dataname: { deparse1(private$input_dataname) }")
invisible(self)
},

#' @description
#' Returns the label of the dataset
#' @return (`character(1)`) the label
get_datalabel = function() {
private$datalabel
},

#' @description
#' Returns the formatted string representing this `FilterStates` object.
#'
#' @param indent (`numeric(1)`) the number of spaces before each line of the representation
#' @return `character(1)` the formatted string
#'
format = function(indent) {
stop("Pure virtual method")
},

#' @description
#' Filter call
#'
Expand Down Expand Up @@ -276,6 +294,15 @@ FilterStates <- R6::R6Class( # nolint
}
},

#' @description
#' Prints this `FilterStates` object
#'
#' @param ... additional arguments to this method
print = function(...) {
cat(shiny::isolate(self$format()))
cat("\n")
kpagacz marked this conversation as resolved.
Show resolved Hide resolved
},

#' @description
#' Gets the name of the function used to filter the data in this `FilterStates`.
#'
Expand Down Expand Up @@ -647,14 +674,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 @@ -689,6 +717,22 @@ DFFilterStates <- R6::R6Class( # nolint
)
},

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

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")
},

#' @description
#' Get function name
#'
#' Get function name used to create filter call.
Expand Down Expand Up @@ -1041,13 +1085,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 @@ -1082,11 +1127,24 @@ MAEFilterStates <- R6::R6Class( # nolint
return(invisible(self))
},

#' Get function name
#' @description
#' Returns the formatted string representing this `MAEFilterStates` object.
#'
#' Get function name used to create filter call.
#' For `MAEFilterStates`
#' `MultiAssayExperiment::subsetByColData` is used.
#' @param indent (`numeric(1)`) the number of spaces before each line of the representation
#' @return `character(1)` the formatted string
format = function(indent = 0) {
checkmate::assert_number(indent, finite = TRUE, lower = 0)

if (length(self$queue_get(1L)) > 0) {
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")
}
},

#' @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 @@ -1195,7 +1253,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 @@ -1403,13 +1462,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 All @@ -1435,6 +1495,36 @@ SEFilterStates <- R6::R6Class( # nolint
)
},

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

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:"))
for (state in self$queue_get(queue_index = "subset")) {
formatted_states <- c(formatted_states, state$format(indent = indent + 4))
}
}

if (!is.null(self$queue_get(queue_index = "select"))) {
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))
}
}

if (length(formatted_states) > 0) {
formatted_states <- c(paste0(whitespace_indent, "Assay ", self$get_datalabel(), " filters:"), formatted_states)
paste(formatted_states, collapse = "\n")
}
},

#' @description
#' Server module
#' @param id (`character(1)`)\cr
Expand Down Expand Up @@ -1942,13 +2032,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 @@ -1970,6 +2061,21 @@ MatrixFilterStates <- R6::R6Class( # nolint
)
},

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

formatted_states <- c()
for (state in self$queue_get(queue_index = "subset")) {
formatted_states <- c(formatted_states, state$format(indent = indent + 2))
}
paste(formatted_states, collapse = "\n")
},

#' @description
#' Server module
#' @param id (`character(1)`)\cr
Expand Down
Loading