Skip to content

Commit

Permalink
fix r cmd check
Browse files Browse the repository at this point in the history
- don't export get_filter_overview
  • Loading branch information
gogonzo committed Oct 22, 2024
1 parent cd445c4 commit 4efca91
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 127 deletions.
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
S3method(c,teal_slices)
S3method(format,teal_module)
S3method(format,teal_modules)
S3method(get_filter_overview,default)
S3method(join_keys,tdata)
S3method(print,teal_module)
S3method(print,teal_modules)
Expand Down
76 changes: 11 additions & 65 deletions R/module_data_summary.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
#' - `array` (`data.frame`, `DataFrame`, `array`, `Matrix` and `SummarizedExperiment`): Method variant
#' can be applied to any two-dimensional objects on which [ncol()] can be used.
#' - `MultiAssayExperiment`: for which summary contains counts for `colData` and all `experiments`.
#' - For other data types module displays data name with warning icon and no more details.
#'
#' @param id (`character(1)`)
#' `shiny` module instance id.
#' @param teal_data (`reactive` returning `teal_data`)
#' Module includes also "Show/Hide unsupported" button to toggle rows of the summary table
#' containing datasets where number of observations are not calculated.
#'
#' @param id (`character(1)`) `shiny` module instance id.
#' @param teal_data (`reactive` returning `teal_data`)
#'
#' @name module_data_summary
#' @rdname module_data_summary
Expand Down Expand Up @@ -185,52 +187,16 @@ get_filter_overview_wrapper <- function(teal_data) {
}
)

do.call(smart_rbind, out)
do.call(.smart_rbind, out)
}

#' Get filter overview
#'
#' Method to create a single entry in [`module_data_summary`]. Method returns `data.frame` containing:
#' - `dataname`: name of the dataset.
#' - `obs`: number of observations in form of a text `filtered/initial`.
#' - `subjects`: number of subjects in form of a text `filtered/initial`. Applicable when a dataset
#' has multiple entries for a single subject.
#'
#' # Extending for other data types
#' `teal` supports data summary table with observation counts for `data.frame` and
#' `MultiAssayExperiment`. For other datasets teal displays only `dataname` and `class` information.
#' To extend this functionality by other data types one needs to create `get_filter_overview.<custom class>`.
#' New method needs to return a `data.frame` containing at least `dataname` column. In general, `teal` supports
#' any type of the information contained in this `data.frame` and they will be included in combined table.
#' Example method for `data.frame` could look like this:
#'
#' ```
#' get_filter_overview.data.frame <- function(current_data, initial_data, dataname, subject_keys) {
#' data.frame(
#' dataname = dataname,
#' obs = if (!is.null(initial_data)) {
#' sprintf("%s/%s", nrow(current_data), nrow(initial_data))
#' } else {
#' # when dataset is added in transform
#' # then only current data is available
#' nrow(current_data)
#' }
#' )
#' }
#' ```
#'
#'

#' @rdname module_data_summary
#' @param current_data (`object`) current object (after filtering and transforming).
#' @param initial_data (`object`) initial object.
#' @param dataname (`character(1)`)
#' @param subject_keys (`character`) names of the columns which determine a single unique subjects
#' @return `data.frame`
get_filter_overview <- function(current_data, initial_data, dataname, subject_keys) {
UseMethod("get_filter_overview")
}

#' @export
get_filter_overview.default <- function(current_data, initial_data, dataname, subject_keys) {
if (inherits(current_data, c("data.frame", "DataFrame", "array", "Matrix", "SummarizedExperiment"))) {
get_filter_overview_array(current_data, initial_data, dataname, subject_keys)
} else if (inherits(current_data, "MultiAssayExperiment")) {
Expand All @@ -240,8 +206,7 @@ get_filter_overview.default <- function(current_data, initial_data, dataname, su
}
}

# pseudo S3 handled in get_filter_overview.default
# Reason is to avoid registering S3 method so it is easier for external users to override
#' @rdname module_data_summary
get_filter_overview_array <- function(current_data, # nolint: object_length.
initial_data,
dataname,
Expand Down Expand Up @@ -272,6 +237,7 @@ get_filter_overview_array <- function(current_data, # nolint: object_length.
}
}

#' @rdname module_data_summary
get_filter_overview_MultiAssayExperiment <- function(current_data, # nolint: object_length, object_name.
initial_data,
dataname) {
Expand Down Expand Up @@ -323,25 +289,5 @@ get_filter_overview_MultiAssayExperiment <- function(current_data, # nolint: obj
))

experiment_info <- cbind(experiment_obs_info, experiment_subjects_info)
smart_rbind(mae_info, experiment_info)
}

#' Smart `rbind`
#'
#' Combine `data.frame` objects which have different columns
#'
#' @param ... (`data.frame`)
#' @keywords internal
smart_rbind <- function(...) {
checkmate::assert_list(list(...), "data.frame")

Reduce(
x = list(...),
function(x, y) {
all_columns <- union(colnames(x), colnames(y))
x[setdiff(all_columns, colnames(x))] <- NA
y[setdiff(all_columns, colnames(y))] <- NA
rbind(x, y)
}
)
.smart_rbind(mae_info, experiment_info)
}
19 changes: 19 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,22 @@ build_datanames_error_message <- function(label = NULL,
}
)
}

#' Smart `rbind`
#'
#' Combine `data.frame` objects which have different columns
#'
#' @param ... (`data.frame`)
#' @keywords internal
.smart_rbind <- function(...) {
checkmate::assert_list(list(...), "data.frame")
Reduce(
x = list(...),
function(x, y) {
all_columns <- union(colnames(x), colnames(y))
x[setdiff(all_columns, colnames(x))] <- NA
y[setdiff(all_columns, colnames(y))] <- NA
rbind(x, y)
}
)
}
8 changes: 4 additions & 4 deletions man/smart_rbind.Rd → man/dot-smart_rbind.Rd

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

52 changes: 0 additions & 52 deletions man/get_filter_overview.Rd

This file was deleted.

24 changes: 22 additions & 2 deletions man/module_data_summary.Rd

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

4 changes: 2 additions & 2 deletions tests/testthat/test-rcode_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ testthat::test_that("get_datasets_code returns code only for specified datanames
# todo: need to use code dependency? Or test it later via public functions/modules
datasets <- teal.slice::init_filtered_data(
list(
IRIS = list(dataset = iris),
MTCARS = list(dataset = mtcars)
IRIS = iris,
MTCARS = mtcars
)
)
testthat::expect_true(TRUE)
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ testthat::test_that("get_teal_bs_theme", {
})

testthat::test_that("report_card_template function returns TealReportCard object with appropriate content and labels", {
fd <- teal.slice::init_filtered_data(list(iris = list(dataset = iris)))
fd <- teal.slice::init_filtered_data(list(iris = iris))
filter_panel_api <- teal.slice::FilterPanelAPI$new(fd)

card <- shiny::isolate(report_card_template(
Expand Down

0 comments on commit 4efca91

Please sign in to comment.