Skip to content

Commit

Permalink
382 rename internal functions (#868)
Browse files Browse the repository at this point in the history
Related to [this
issue](insightsengineering/teal.slice#382)

`disassemble_slices` function is removed, will use `as.list.teal_slices`
from `teal.slice`
`reassemble_slices` function is remove, will use `as.teal_slices` from
this package
`as.teal_slices` function lifted from `teal.slice` is renamed to
`list_to_teal_slices`
  • Loading branch information
chlebowa authored Jul 24, 2023
1 parent 24b361c commit 6ce8ad7
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 79 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ S3method(ui_nested_tabs,teal_module)
S3method(ui_nested_tabs,teal_modules)
export("%>%")
export(TealReportCard)
export(as.teal_slices)
export(example_module)
export(get_code_tdata)
export(get_join_keys)
Expand Down
6 changes: 2 additions & 4 deletions R/init.R
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,11 @@ init <- function(data,

if (!inherits(filter, "teal_slices")) {
checkmate::assert_subset(names(filter), choices = datanames)
# as.teal_slices is lifted from teal.slice package, see zzz.R
# list_to_teal_slices is lifted from teal.slice package, see zzz.R
# This is a temporary measure and will be removed two release cycles from now (now meaning 0.13.0).
filter <- as.teal_slices(filter)
filter <- list_to_teal_slices(filter)
}



# check teal_slices
for (i in seq_along(filter)) {
dataname_i <- shiny::isolate(filter[[i]]$dataname)
Expand Down
38 changes: 5 additions & 33 deletions R/module_snapshot_manager.R
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ snapshot_manager_srv <- function(id, slices_global, mapping_matrix, filtered_dat
filter <- isolate(slices_global())
snapshot_history <- reactiveVal({
list(
"Initial application state" = disassemble_slices(filter)
"Initial application state" = as.list(filter, recursive = TRUE)
)
})

Expand Down Expand Up @@ -133,7 +133,7 @@ snapshot_manager_srv <- function(id, slices_global, mapping_matrix, filtered_dat
)
updateTextInput(inputId = "snapshot_name", value = , placeholder = "Meaningful, unique name")
} else {
snapshot <- disassemble_slices(slices_global())
snapshot <- as.list(slices_global(), recursive = TRUE)
attr(snapshot, "mapping") <- matrix_to_mapping(mapping_matrix())
snapshot_update <- c(snapshot_history(), list(snapshot))
names(snapshot_update)[length(snapshot_update)] <- snapshot_name
Expand All @@ -149,7 +149,7 @@ snapshot_manager_srv <- function(id, slices_global, mapping_matrix, filtered_dat
s <- "Initial application state"
### Begin restore procedure. ###
snapshot <- snapshot_history()[[s]]
snapshot_state <- reassemble_slices(snapshot)
snapshot_state <- as.teal_slices(snapshot)
mapping_unfolded <- unfold_mapping(attr(snapshot_state, "mapping"), names(filtered_data_list))
mapply(
function(filtered_data, filters) {
Expand Down Expand Up @@ -183,7 +183,7 @@ snapshot_manager_srv <- function(id, slices_global, mapping_matrix, filtered_dat
observers[[id_pickme]] <- observeEvent(input[[id_pickme]], {
### Begin restore procedure. ###
snapshot <- snapshot_history()[[s]]
snapshot_state <- reassemble_slices(snapshot)
snapshot_state <- as.teal_slices(snapshot)
mapping_unfolded <- unfold_mapping(attr(snapshot_state, "mapping"), names(filtered_data_list))
mapply(
function(filtered_data, filters) {
Expand All @@ -207,7 +207,7 @@ snapshot_manager_srv <- function(id, slices_global, mapping_matrix, filtered_dat
},
content = function(file) {
snapshot <- snapshot_history()[[s]]
snapshot_state <- reassemble_slices(snapshot)
snapshot_state <- as.teal_slices(snapshot)
teal.slice::slices_store(tss = snapshot_state, file = file)
}
)
Expand Down Expand Up @@ -237,34 +237,6 @@ snapshot_manager_srv <- function(id, slices_global, mapping_matrix, filtered_dat

### utility functions ----

#' Convert teal_slices and to list of lists (drop classes), while maintaining attributes.
#' Adds special class so that the reverse action can have assertion on argument type.
#' @param tss (`teal_slices`)
#' @return Object of class `teal_slices_snapshot`, which is a list of the same length as `tss`,
#' where each `teal_slice` has been converted to a list.
#' @keywords internal
#'
disassemble_slices <- function(tss) {
checkmate::assert_class(tss, "teal_slices")
ans <- unclass(tss)
ans[] <- lapply(ans, as.list)
class(ans) <- "teal_slices_snapshot"
ans
}

#' Rebuild `teal_slices` from `teal_slices_snapshot`.
#' @param x (`teal_slices_snapshot`)
#' @return A `teal_slices` object.
#' @keywords internal
#'
reassemble_slices <- function(x) {
checkmate::assert_class(x, "teal_slices_snapshot")
attrs <- attributes(unclass(x))
ans <- lapply(x, as.teal_slice)
do.call(teal_slices, c(ans, attrs))
}


#' Explicitly enumerate global filters.
#'
#' Transform module mapping such that global filters are explicitly specified for every module.
Expand Down
17 changes: 17 additions & 0 deletions R/teal_slices.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#' If empty list, all filters will be available to all modules but will start inactive.
#' If `module_specific` is `FALSE`, only `global_filters` will be active on start.
#'
#' @param x (`list`) of lists to convert to `teal_slices`
#'
#' @examples
#' filter <- teal_slices(
#' teal.slice::teal_slice(dataname = "iris", varname = "Species", id = "species"),
Expand Down Expand Up @@ -92,6 +94,21 @@ teal_slices <- function(...,
})
}


#' @rdname teal_slices
#' @export
#' @keywords internal
#'
as.teal_slices <- function(x) { # nolint
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))
}


#' Deep copy `teal_slices`
#'
#' it's important to create a new copy of `teal_slices` when
Expand Down
2 changes: 1 addition & 1 deletion R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

# Use non-exported function(s) from teal.slice.
# This is a temporary measure and will be removed two release cycles from now (now meaning 0.13.0).
as.teal_slices <- getFromNamespace("as.teal_slices", "teal.slice") # nolint
list_to_teal_slices <- getFromNamespace("list_to_teal_slices", "teal.slice") # nolint
# This one is here because setdiff_teal_slice should not be exported from teal.slice.
setdiff_teal_slices <- getFromNamespace("setdiff_teal_slices", "teal.slice")
# all *Block objects are private in teal.reporter
Expand Down
21 changes: 0 additions & 21 deletions man/disassemble_slices.Rd

This file was deleted.

18 changes: 0 additions & 18 deletions man/reassemble_slices.Rd

This file was deleted.

6 changes: 6 additions & 0 deletions man/teal_slices.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-snapshot_manager.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ testthat::test_that("snapshot manager holds initial state in history", {
testthat::expect_true("Initial application state" %in% names(snapshot_history()))

snapshot <- snapshot_history()[["Initial application state"]]
snapshot_state <- reassemble_slices(snapshot)
snapshot_state <- as.teal_slices(snapshot)

testthat::expect_equal(disassemble_slices(snapshot_state), disassemble_slices(filter))
testthat::expect_equal(as.list(snapshot_state, recursive = TRUE), as.list(filter, recursive = TRUE))
}
)
})

0 comments on commit 6ce8ad7

Please sign in to comment.