Skip to content

251 deleayed datasets@main #252

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export(data_extract_spec)
export(data_extract_srv)
export(data_extract_ui)
export(datanames_input)
export(delayed_datasets)
export(filter_spec)
export(first_choice)
export(first_choices)
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# teal.transform 0.6.0.9000

### Enhancements

* Added utility function `delayed_datasets` that facilitates creating multiple `delayed_data_extract_spec`s without knowlege of the available datasets. It is now possible to create `delayed_data_extract_spec` for all available datasets with one call, rather than with one call per dataset.

# teal.transform 0.6.0

### Enhancements
Expand Down
17 changes: 15 additions & 2 deletions R/data_extract_datanames.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,20 @@ get_extract_datanames <- function(data_extracts) {
}
})

unique(unlist(datanames))
.extract_delayed_datasets <- function(x) {
if (inherits(x, "delayed_datasets")) {
attr(x, "datasets", exact = TRUE)
} else {
x
}
}
datanames <- rapply(datanames, .extract_delayed_datasets)

if (any(datanames == "all")) {
"all"
} else {
unique(datanames)
}
}

#' Verify uniform dataset source across data extract specification
Expand All @@ -82,5 +95,5 @@ get_extract_datanames <- function(data_extracts) {
is_single_dataset <- function(...) {
data_extract_spec <- list(...)
dataset_names <- get_extract_datanames(data_extract_spec)
length(dataset_names) == 1
length(dataset_names) == 1L && dataset_names != "all"
}
157 changes: 90 additions & 67 deletions R/data_extract_module.R
Original file line number Diff line number Diff line change
Expand Up @@ -124,74 +124,14 @@ cond_data_extract_single_ui <- function(ns, single_data_extract_spec) {
data_extract_ui <- function(id, label, data_extract_spec, is_single_dataset = FALSE) {
ns <- NS(id)

if (inherits(data_extract_spec, "data_extract_spec")) {
data_extract_spec <- list(data_extract_spec)
}
check_data_extract_spec(data_extract_spec)

if (is.null(data_extract_spec)) {
return(helpText(sprintf("Data extraction with label '%s' is NULL. Please contact the app author.", label)))
}
stopifnot(
`more than one dataset in data_extract_spec but is_single_dataset parameter is set to TRUE` =
!is_single_dataset || length(data_extract_spec) == 1
)

dataset_names <- vapply(
data_extract_spec,
function(x) x$dataname,
character(1),
USE.NAMES = FALSE
)

stopifnot(`list contains data_extract_spec objects with the same dataset` = all(!duplicated(dataset_names)))

dataset_input <- if (is_single_dataset) {
NULL
} else {
if (length(dataset_names) == 1) {
if ((is.null(data_extract_spec[[1]]$filter)) &&
(
!is.null(data_extract_spec[[1]]$select$fixed) &&
data_extract_spec[[1]]$select$fixed == TRUE
)) {
NULL
} else {
helpText("Dataset:", tags$code(dataset_names))
}
} else {
teal.widgets::optionalSelectInput(
inputId = ns("dataset"),
label = "Dataset",
choices = dataset_names,
selected = dataset_names[1],
multiple = FALSE
)
}
}
tagList(
include_css_files(pattern = "data_extract"),
tags$div(
class = "data-extract",
tags$label(label),
dataset_input,
if (length(dataset_names) == 1) {
data_extract_single_ui(
id = ns(id_for_dataset(dataset_names)),
single_data_extract_spec = data_extract_spec[[1]]
)
} else {
do.call(
div,
unname(lapply(
data_extract_spec,
function(x) {
cond_data_extract_single_ui(ns, x)
}
))
)
}
)
# Pass arguments to server function.
div(
checkboxInput(ns("is_single_dataset"), label = NULL, value = is_single_dataset),
textInput(ns("data_extract_label"), label = NULL, value = label),
style = "display: none;"
),
uiOutput(ns("data_extract_ui_container"))
)
}

Expand Down Expand Up @@ -562,6 +502,88 @@ data_extract_srv.list <- function(id,
)
}
})


output$data_extract_ui_container <- renderUI({
ns <- session$ns

logger::log_debug(
"initializing data_extract_ui with datasets: { paste(names(datasets), collapse = ', ') }."
)

if (inherits(data_extract_spec, "data_extract_spec")) {
data_extract_spec <- list(data_extract_spec)
}
check_data_extract_spec(data_extract_spec)

if (is.null(data_extract_spec)) {
return(helpText(sprintf("Data extraction with label '%s' is NULL. Please contact the app author.", label)))
}
stopifnot(
`more than one dataset in data_extract_spec but is_single_dataset parameter is set to TRUE` =
isFALSE(input$is_single_dataset) || length(data_extract_spec) == 1
)

dataset_names <- vapply(
data_extract_spec,
function(x) x$dataname,
character(1),
USE.NAMES = FALSE
)

if (anyDuplicated(dataset_names) != 0L) {
stop("list contains data_extract_spec objects with the same dataset")
}

dataset_input <-
if (isTRUE(input$is_single_dataset)) {
NULL
} else {
if (length(dataset_names) == 1) {
if ((is.null(data_extract_spec[[1]]$filter)) &&
(
!is.null(data_extract_spec[[1]]$select$fixed) &&
data_extract_spec[[1]]$select$fixed == TRUE
)) {
NULL
} else {
helpText("Dataset:", tags$code(dataset_names))
}
} else {
teal.widgets::optionalSelectInput(
inputId = ns("dataset"),
label = "Dataset",
choices = dataset_names,
selected = dataset_names[1],
multiple = FALSE
)
}
}
tagList(
include_css_files(pattern = "data_extract"),
tags$div(
class = "data-extract",
tags$label(input$data_extract_label),
dataset_input,
if (length(dataset_names) == 1) {
data_extract_single_ui(
id = ns(id_for_dataset(dataset_names)),
single_data_extract_spec = data_extract_spec[[1]]
)
} else {
do.call(
div,
unname(lapply(
data_extract_spec,
function(x) {
cond_data_extract_single_ui(ns, x)
}
))
)
}
)
)
})
filter_and_select_reactive
}
)
Expand Down Expand Up @@ -764,6 +786,7 @@ data_extract_multiple_srv.list <- function(data_extract,
)

data_extract <- Filter(Negate(is.null), data_extract)
data_extract <- resolve_delayed_datasets(data_extract, names(datasets))

if (is.function(select_validation_rule)) {
select_validation_rule <- sapply(
Expand Down
9 changes: 7 additions & 2 deletions R/data_extract_spec.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@
#' @export
#'
data_extract_spec <- function(dataname, select = NULL, filter = NULL, reshape = FALSE) {
checkmate::assert_string(dataname)
checkmate::assert(
checkmate::check_string(dataname),
checkmate::check_class(dataname, "delayed_datasets")
)
stopifnot(
is.null(select) ||
(inherits(select, "select_spec") && length(select) >= 1)
Expand All @@ -111,7 +114,7 @@ data_extract_spec <- function(dataname, select = NULL, filter = NULL, reshape =

for (idx in seq_along(filter)) filter[[idx]]$dataname <- dataname

if (
ans <- if (
inherits(select, "delayed_select_spec") ||
any(vapply(filter, inherits, logical(1), "delayed_filter_spec"))
) {
Expand All @@ -125,4 +128,6 @@ data_extract_spec <- function(dataname, select = NULL, filter = NULL, reshape =
class = "data_extract_spec"
)
}
assert_delayed_datesets(ans)
ans
}
54 changes: 54 additions & 0 deletions R/delayed_datasets.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#' Delayed datasets
#'
#' Generate `delayed_data_extract_spec` without prior knowledge of the data.
#'
#' `delayed_datasets` is a character string with class `delayed_datasets`
#' and attribute `datasets`, which is set to `x`. The attribute specifies
#' a wishlist of datasets for which `delayed_data_extract_spec`s are to be created,
#' maintaining the same specification for `select`, `filter`, and `reshape`.
#'
#' `delayed_data_extract_spec` that have `delayed_datasets` for `dataname` are resolved internally.
#'
#' It is forbidden to use different `delayed_datasets` within one `delayed_data_extract_spec`
#' as well as to mix `delayed_datasets` with specific dataset specification within one `delayed_data_extract_spec`.
#' This is enforced when creating `data_extract_spec`s.
#'
#' @inheritSection resolve_delayed_datasets Resolution
#'
#' @param x (`character`) set of dataset names for wchich `delayed_data_extract_spec`s will be created;
#' set to `"all"` to use all available datasets
#'
#' @return Character string with `class` `delayed_datasets` and attribute `datasets`.
#'
#' @examples
#' # resolve into delayed_data_extract_specs for all available datasets
#' data_extract_spec(
#' dataname = delayed_datasets()
#' )
#'
#' # resolve into delayed_data_extract_specs for available datasets from among ADSL and ADAE
#' data_extract_spec(
#' dataname = delayed_datasets(c("ADSL", "ADAE"))
#' )
#'
#' # use the same delayed_datasets() in child elements of a des
#' data_extract_spec(
#' dataname = delayed_datasets(),
#' select = select_spec(
#' choices = variable_choices(
#' data = delayed_datasets(),
#' subset = function(data) names(Filter(is.numeric, data))
#' ),
#' selected = last_choice()
#' )
#' )
#'
#' @export
#'
delayed_datasets <- function(x = "all") {
structure(
"delayed_datasets",
class = c("delayed_datasets", "delayed_data", "character"),
datasets = x
)
}
Loading