Skip to content

Commit 6616eab

Browse files
committed
rewrite data_extract_module to create entire UI server side
1 parent b2b7e31 commit 6616eab

File tree

1 file changed

+90
-67
lines changed

1 file changed

+90
-67
lines changed

R/data_extract_module.R

Lines changed: 90 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -124,74 +124,14 @@ cond_data_extract_single_ui <- function(ns, single_data_extract_spec) {
124124
data_extract_ui <- function(id, label, data_extract_spec, is_single_dataset = FALSE) {
125125
ns <- NS(id)
126126

127-
if (inherits(data_extract_spec, "data_extract_spec")) {
128-
data_extract_spec <- list(data_extract_spec)
129-
}
130-
check_data_extract_spec(data_extract_spec)
131-
132-
if (is.null(data_extract_spec)) {
133-
return(helpText(sprintf("Data extraction with label '%s' is NULL. Please contact the app author.", label)))
134-
}
135-
stopifnot(
136-
`more than one dataset in data_extract_spec but is_single_dataset parameter is set to TRUE` =
137-
!is_single_dataset || length(data_extract_spec) == 1
138-
)
139-
140-
dataset_names <- vapply(
141-
data_extract_spec,
142-
function(x) x$dataname,
143-
character(1),
144-
USE.NAMES = FALSE
145-
)
146-
147-
stopifnot(`list contains data_extract_spec objects with the same dataset` = all(!duplicated(dataset_names)))
148-
149-
dataset_input <- if (is_single_dataset) {
150-
NULL
151-
} else {
152-
if (length(dataset_names) == 1) {
153-
if ((is.null(data_extract_spec[[1]]$filter)) &&
154-
(
155-
!is.null(data_extract_spec[[1]]$select$fixed) &&
156-
data_extract_spec[[1]]$select$fixed == TRUE
157-
)) {
158-
NULL
159-
} else {
160-
helpText("Dataset:", tags$code(dataset_names))
161-
}
162-
} else {
163-
teal.widgets::optionalSelectInput(
164-
inputId = ns("dataset"),
165-
label = "Dataset",
166-
choices = dataset_names,
167-
selected = dataset_names[1],
168-
multiple = FALSE
169-
)
170-
}
171-
}
172127
tagList(
173-
include_css_files(pattern = "data_extract"),
174-
tags$div(
175-
class = "data-extract",
176-
tags$label(label),
177-
dataset_input,
178-
if (length(dataset_names) == 1) {
179-
data_extract_single_ui(
180-
id = ns(id_for_dataset(dataset_names)),
181-
single_data_extract_spec = data_extract_spec[[1]]
182-
)
183-
} else {
184-
do.call(
185-
div,
186-
unname(lapply(
187-
data_extract_spec,
188-
function(x) {
189-
cond_data_extract_single_ui(ns, x)
190-
}
191-
))
192-
)
193-
}
194-
)
128+
# Pass arguments to server function.
129+
div(
130+
checkboxInput(ns("is_single_dataset"), label = NULL, value = is_single_dataset),
131+
textInput(ns("data_extract_label"), label = NULL, value = label),
132+
style = "display: none;"
133+
),
134+
uiOutput(ns("data_extract_ui_container"))
195135
)
196136
}
197137

@@ -562,6 +502,89 @@ data_extract_srv.list <- function(id,
562502
)
563503
}
564504
})
505+
506+
507+
output$data_extract_ui_container <- renderUI({
508+
ns <- session$ns
509+
510+
logger::log_debug(
511+
"initializing data_extract_ui with datasets: { paste(names(datasets), collapse = ', ') }."
512+
)
513+
514+
if (inherits(data_extract_spec, "data_extract_spec")) {
515+
data_extract_spec <- list(data_extract_spec)
516+
}
517+
check_data_extract_spec(data_extract_spec)
518+
519+
if (is.null(data_extract_spec)) {
520+
return(helpText(sprintf("Data extraction with label '%s' is NULL. Please contact the app author.", label)))
521+
}
522+
stopifnot(
523+
`more than one dataset in data_extract_spec but is_single_dataset parameter is set to TRUE` =
524+
isFALSE(input$is_single_dataset) || length(data_extract_spec) == 1
525+
)
526+
527+
dataset_names <- vapply(
528+
data_extract_spec,
529+
function(x) x$dataname,
530+
character(1),
531+
USE.NAMES = FALSE
532+
)
533+
534+
if (anyDuplicated(dataset_names) != 0L) {
535+
stop("list contains data_extract_spec objects with the same dataset")
536+
}
537+
538+
dataset_input <-
539+
if (isTRUE(input$is_single_dataset)) {
540+
# if (FALSE) {
541+
NULL
542+
} else {
543+
if (length(dataset_names) == 1) {
544+
if ((is.null(data_extract_spec[[1]]$filter)) &&
545+
(
546+
!is.null(data_extract_spec[[1]]$select$fixed) &&
547+
data_extract_spec[[1]]$select$fixed == TRUE
548+
)) {
549+
NULL
550+
} else {
551+
helpText("Dataset:", tags$code(dataset_names))
552+
}
553+
} else {
554+
teal.widgets::optionalSelectInput(
555+
inputId = ns("dataset"),
556+
label = "Dataset",
557+
choices = dataset_names,
558+
selected = dataset_names[1],
559+
multiple = FALSE
560+
)
561+
}
562+
}
563+
tagList(
564+
include_css_files(pattern = "data_extract"),
565+
tags$div(
566+
class = "data-extract",
567+
tags$label(input$data_extract_label),
568+
dataset_input,
569+
if (length(dataset_names) == 1) {
570+
data_extract_single_ui(
571+
id = ns(id_for_dataset(dataset_names)),
572+
single_data_extract_spec = data_extract_spec[[1]]
573+
)
574+
} else {
575+
do.call(
576+
div,
577+
unname(lapply(
578+
data_extract_spec,
579+
function(x) {
580+
cond_data_extract_single_ui(ns, x)
581+
}
582+
))
583+
)
584+
}
585+
)
586+
)
587+
})
565588
filter_and_select_reactive
566589
}
567590
)

0 commit comments

Comments
 (0)