-
-
Notifications
You must be signed in to change notification settings - Fork 6
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
Changes from 6 commits
3020d22
e37e73c
a0ab58b
5d2986f
49ab696
0ede485
ee4f770
7e62953
8205487
967a751
02d3b9c
99999c0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
@@ -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 | ||
} | ||
|
@@ -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 | ||
} | ||
|
@@ -1271,6 +1290,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()) | ||
) | ||
}, | ||
|
||
#' @description | ||
#' Answers the question of whether the current settings and values selected actually filters out any values. | ||
#' @return logical scalar | ||
|
@@ -2048,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], | ||
kpagacz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
|
@@ -2346,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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should these be There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's because format implicitly casts |
||
|
||
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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -82,7 +82,6 @@ FilteredData <- R6::R6Class( # nolint | |
names(private$filtered_datasets) | ||
}, | ||
|
||
|
||
#' Gets data label for the dataset | ||
#' | ||
#' Useful to display in `Show R Code`. | ||
|
@@ -315,7 +314,6 @@ FilteredData <- R6::R6Class( # nolint | |
intersect(self$datanames(), datanames) | ||
}, | ||
|
||
|
||
#' @description | ||
#' Adds a `TealDataset` object to this `FilteredData` | ||
#' | ||
|
@@ -377,6 +375,40 @@ FilteredData <- R6::R6Class( # nolint | |
Filter(function(x) length(x) > 0, states) | ||
}, | ||
|
||
#' @description | ||
#' Returns the filter state formatted for printing to an `IO` device. | ||
#' | ||
#' @return `character` the pre-formatted filter state | ||
#' @examples | ||
#' datasets <- teal.slice:::FilteredData$new() | ||
#' datasets$set_dataset(teal.data::dataset("iris", iris)) | ||
#' utils::data(miniACC, package = "MultiAssayExperiment") | ||
#' datasets$set_dataset(teal.data::dataset("mae", miniACC)) | ||
#' fs <- list( | ||
#' iris = list( | ||
#' Sepal.Length = list(selected = c(5.1, 6.4), keep_na = TRUE, keep_inf = FALSE), | ||
#' Species = list(selected = c("setosa", "versicolor"), keep_na = FALSE) | ||
#' ), | ||
#' mae = list( | ||
#' subjects = list( | ||
#' years_to_birth = list(selected = c(30, 50), keep_na = TRUE, keep_inf = FALSE), | ||
#' vital_status = list(selected = "1", keep_na = FALSE), | ||
#' gender = list(selected = "female", keep_na = TRUE) | ||
#' ), | ||
#' RPPAArray = list( | ||
#' subset = list(ARRAY_TYPE = list(selected = "", keep_na = TRUE)) | ||
#' ) | ||
#' ) | ||
#' ) | ||
#' datasets$set_filter_state(state = fs) | ||
#' cat(shiny::isolate(datasets$get_formatted_filter_state())) | ||
#' | ||
get_formatted_filter_state = function() { | ||
out <- c() | ||
for (filtered_dataset in self$get_filtered_dataset()) out <- c(out, filtered_dataset$get_formatted_filter_state()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wouldn't
work as well? And in other places? Or is it a deliberate decision to use for loops There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They don't return a character(1) always. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Plus, I usually append things to the out object (not always) before looping and apply stuff doesn't capture by reference from their lexical scopes, so I opted to use for loops everywhere. Plus, for loops in R stopped being less performant than apply stuff ages ago, so tbh there's no particularly good reason to use them except for 'list or dict comprehension'-style programming. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fair enough just askin' ;) |
||
paste(out, collapse = "\n") | ||
}, | ||
|
||
#' @description | ||
#' Sets active filter states. | ||
#' @param state (`named list`)\cr | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RangeFilterState handles also
Inf
There was a problem hiding this comment.
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.