diff --git a/DESCRIPTION b/DESCRIPTION index 46ba078dd2..40bd0576c3 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -96,6 +96,8 @@ Language: en-US LazyData: true Roxygen: list(markdown = TRUE, packages = c("roxy.shinylive")) RoxygenNote: 7.3.3 +Remotes: + insightsengineering/teal.slice@main Collate: 'TealAppDriver.R' 'after.R' diff --git a/R/checkmate.R b/R/checkmate.R index a6463c175e..7e754d2e51 100644 --- a/R/checkmate.R +++ b/R/checkmate.R @@ -12,7 +12,7 @@ check_reactive <- function(x, null.ok = FALSE) { # nolint: object_name_linter. paste0(cl, collapse = "','") )) } - return(TRUE) + TRUE } #' @rdname check_reactive test_reactive <- function(x, null.ok = FALSE) { # nolint: object_name_linter. diff --git a/R/module_transform_data.R b/R/module_transform_data.R index 3c9daa7d2f..a9558ebd7f 100644 --- a/R/module_transform_data.R +++ b/R/module_transform_data.R @@ -25,7 +25,7 @@ ui_transform_teal_data <- function(id, transformators, class = "well") { transformators <- list(transformators) } checkmate::assert_list(transformators, "teal_transform_module") - names(transformators) <- sprintf("transform_%d", seq_len(length(transformators))) + names(transformators) <- sprintf("transform_%d", seq_along(transformators)) lapply( names(transformators), @@ -75,7 +75,7 @@ srv_transform_teal_data <- function(id, data, transformators, modules = NULL, is transformators <- list(transformators) } checkmate::assert_list(transformators, "teal_transform_module", null.ok = TRUE) - names(transformators) <- sprintf("transform_%d", seq_len(length(transformators))) + names(transformators) <- sprintf("transform_%d", seq_along(transformators)) moduleServer(id, function(input, output, session) { module_output <- Reduce( diff --git a/R/teal_slices.R b/R/teal_slices.R index d4547b4eff..af234cb115 100644 --- a/R/teal_slices.R +++ b/R/teal_slices.R @@ -123,43 +123,6 @@ teal_slices <- function(..., }) } - -#' @rdname teal_slices -#' @export -#' @keywords internal -#' -as.teal_slices <- function(x) { # nolint: object_name. - checkmate::assert_list(x) - lapply(x, checkmate::assert_list, names = "named", .var.name = "list element") - - attrs <- attributes(unclass(x)) - ans <- lapply(x, function(x) if (is.teal_slice(x)) x else as.teal_slice(x)) - do.call(teal_slices, c(ans, attrs)) -} - - -#' @rdname teal_slices -#' @export -#' @keywords internal -#' -c.teal_slices <- function(...) { - x <- list(...) - checkmate::assert_true(all(vapply(x, is.teal_slices, logical(1L))), .var.name = "all arguments are teal_slices") - - all_attributes <- lapply(x, attributes) - all_attributes <- coalesce_r(all_attributes) - all_attributes <- all_attributes[names(all_attributes) != "class"] - - do.call( - teal_slices, - c( - unique(unlist(x, recursive = FALSE)), - all_attributes - ) - ) -} - - #' Deep copy `teal_slices` #' #' it's important to create a new copy of `teal_slices` when @@ -179,3 +142,26 @@ deep_copy_filter <- function(filter) { filter_copy }) } + + +#' Copy functions to `teal` namespace +#' +#' Useful when we require function from other namespace where this function +#' calls other functions from `teal` namespace (see `as.teal_slices`, `c.teal_slices`). +#' @keywords internal +.copy_to_teal <- function(fun) { + environment(fun) <- getNamespace("teal") + fun +} + +#' @rdname teal_slices +#' @export +#' @keywords internal +#' +as.teal_slices <- .copy_to_teal(teal.slice::as.teal_slices) # nolint: object_name_linter. + +#' @rdname teal_slices +#' @export +#' @keywords internal +#' +c.teal_slices <- .copy_to_teal(utils::getS3method("c", "teal_slices", envir = getNamespace("teal.slice"))) diff --git a/man/dot-copy_to_teal.Rd b/man/dot-copy_to_teal.Rd new file mode 100644 index 0000000000..57b83cffb8 --- /dev/null +++ b/man/dot-copy_to_teal.Rd @@ -0,0 +1,13 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/teal_slices.R +\name{.copy_to_teal} +\alias{.copy_to_teal} +\title{Copy functions to \code{teal} namespace} +\usage{ +.copy_to_teal(fun) +} +\description{ +Useful when we require function from other namespace where this function +calls other functions from \code{teal} namespace (see \code{as.teal_slices}, \code{c.teal_slices}). +} +\keyword{internal} diff --git a/tests/testthat/test-teal_slices.R b/tests/testthat/test-teal_slices.R index a64f518639..7bcc337f23 100644 --- a/tests/testthat/test-teal_slices.R +++ b/tests/testthat/test-teal_slices.R @@ -111,10 +111,6 @@ testthat::test_that( } ) - -# from different file - - testthat::test_that("teal_slices mapping should be an empty list or a named list or missing", { testthat::expect_no_error( teal_slices( @@ -156,3 +152,52 @@ testthat::test_that("teal_slices mapping should be an empty list or a named list "Assertion.+failed" ) }) + +testthat::test_that("c.teal_slices combines mapping of teal_slices objects", { + tss1 <- teal_slices( + teal.slice::teal_slice(dataname = "data1", varname = "var1", id = "test1"), + module_specific = TRUE, + mapping = list(module1 = "test1") + ) + tss2 <- teal_slices( + teal.slice::teal_slice(dataname = "data2", varname = "var2", id = "test2"), + module_specific = TRUE, + mapping = list(module2 = "test2") + ) + testthat::expect_identical( + c(tss1, tss2), + teal_slices( + tss1[[1]], tss2[[1]], + module_specific = TRUE, + mapping = list( + module1 = "test1", + module2 = "test2" + ) + ) + ) +}) + +testthat::test_that("c.teal_slices combines mapping of two equal slices objects but ignores adding duplicated one", { + tss1 <- teal_slices( + teal.slice::teal_slice(dataname = "data1", varname = "var1", id = "test1"), + module_specific = TRUE, + mapping = list(module1 = "test1") + ) + tss2 <- teal_slices( + teal.slice::teal_slice(dataname = "data1", varname = "var1", id = "test1"), + module_specific = TRUE, + mapping = list(module2 = "test1") + ) + + testthat::expect_identical( + c(tss1, tss2), + teal_slices( + tss1[[1]], + module_specific = TRUE, + mapping = list( + module1 = "test1", + module2 = "test1" + ) + ) + ) +})