If you want to use shinyCohortBuilder
with a custom
source type, a set of methods needs to be defined.
Currently there exists one official extension
cohortBuilder.db
package that allows you to use
shinyCohortBuilder
(and cohortBuilder
) with
database connections.
The goal of this document is to explain how to create custom
extensions to shinyCohortBuilder
.
In general to create the custom layer you need to create an R package where:
cohortBuilder
methods
is implemented (see.
vignettes("custom-extensions", package = "cohortBuilder")
).shinyCohortBuilder
are implemented.If you have cohortBuilder
integration ready for the
selected source type
(a new package named
cohortBuilder.<type>
), the next step is to add
shinyCohortBuilder
integration.
Below we describe all the required and optional methods you need to define within the created package.
.render_filters
- method used to define
structure for filters rendering in a selected stepRequired parameters:
source
- Source object.cohort
- Cohort object.step_id
- Id of the filtering step.ns
- Namespace function....
- Unused, added for S3 integration only.Details:
cohort$get_step(step_id)$filters
..update_data_stats
method described below..render_filter
method.shiny::div(class = "cb_filters", `data-step_id` = step_id)
.Examples:
shinyCohortBuilder
- default method<- function(source, cohort, step_id, ns, ...) {
.render_filters.default <- cohort$get_step(step_id)
step ::tagList(
shiny::htmlOutput(ns(paste0(step_id, "-stats")), class = "scb_data_stats"),
shiny$filters %>%
step::map(~ .render_filter(.x, step_id, cohort, ns = ns)) %>%
purrr::div(class = "cb_filters", `data-step_id` = step_id)
shiny
) }
shinyCohortBuilder
- tblist data class<- function(source, cohort, step_id, ns, ...) {
.render_filters.tblist <- cohort$get_step(step_id)
step
group_filters(cohort$get_source(), step$filters) %>%
::imap(~ dataset_filters(.x, .y, step_id, cohort, ns = ns)) %>%
purrr::div(class = "cb_filters", `data-step_id` = step_id)
shiny }
In this example we group all the defined filters by related datasets
from source (group_filters
), and attach a separate
statistics placeholder for each dataset (dataset_filters).
cohortBuilder.db
- db data class<- function(source, cohort, step_id, ns) {
render_filters.db <- cohort$get_step(step_id)
step
group_filters_db(cohort$get_source(), step$filters) %>%
::imap(~ dataset_filters_db(.x, .y, step_id, cohort, ns = ns)) %>%
purrrdiv(class = "cb_filters", `data-step_id` = step_id)
}
.update_data_stats
- logic for updating data
statisticsRequired parameters:
source
- Source object.step_id
- Id of the filtering step.cohort
- Cohort object.session
- Shiny session object....
- Unused, added for S3 integration only.Details:
.render_filters
.cohort$get_cache(step_id, state = "pre")
.cohort$attributes$stats
to get displayed statistics
state chosen by the user (“pre”, “post”, both or NULL)..pre_post_stats
(or
.pre_post_stats_text
)` which returns formatted statistics
output..sendOutput
method (useful when sending output in loop see
“tblist” source example below).Examples:
shinyCohortBuilder
- default method<- function(source, step_id, cohort, session, ...) {
.update_data_stats.default <- session$ns
ns <- cohort$attributes$stats
stats
$output[[paste0(step_id, "-stats")]] <- shiny::renderUI({
session<- cohort$get_cache(step_id, state = "pre")$n_rows
previous if (!previous > 0) {
return("No data selected in previous step.")
}<- cohort$get_cache(step_id, state = "post")$n_rows
current .pre_post_stats(current, previous, percent = TRUE, stats = stats)
}) }
shinyCohortBuilder
- tblist data class<- function(source, step_id, cohort, session, ...) {
.update_data_stats.tblist <- cohort$attributes$stats
stats <- cohort$get_step(step_id)
step
<- names(cohort$get_source()$attributes$datasets)
dataset_names <- purrr::map_chr(step$filters, get_filter_dataset)
data_filters <- intersect(dataset_names, data_filters)
dataset_names
%>% purrr::walk(
dataset_names ~ .sendOutput(
paste0(step_id, "-stats_", .x),
::renderUI({
shiny<- cohort$get_cache(step_id, state = "pre")[[.x]]$n_rows
previous if (!previous > 0) {
return("No data selected in previous step.")
}<- cohort$get_cache(step_id, state = "post")[[.x]]$n_rows
current .pre_post_stats(current, previous, percent = TRUE, stats = stats)
}),
session
)
) }
cohortBuilder.db
<- function(source, step_id, cohort, session) {
update_data_stats.db <- cohort$attributes$stats
stats
<- source$attributes$tables
dataset_names %>% purrr::walk(
dataset_names ~ shinyCohortBuilder::sendOutput(
paste0(step_id, "-stats_", .x),
::renderUI({
shiny<- cohort$get_cache(step_id, state = "pre")[[.x]]$n_rows
previous if (!previous > 0) {
return("No data selected in previous step.")
}<- cohort$get_cache(step_id, state = "post")[[.x]]$n_rows
current ::pre_post_stats(current, previous, percent = TRUE, stats = stats)
shinyCohortBuilder
})
)
) }
autofilter
(optional) - automatically generate
filters configuration based on Source dataRequired parameters:
source
- Source object,attach_as
- Should filters be added as the first step
("step"
) or as available filters for configuration panel
("meta"
),...
- Unused, added for S3 integration only.Details:
cohortBuilder::filter
.attach_as = "step"
wrap them with
cohortBuilder::step
and attach to the Source using
add_step
method.attach_as = "meta"
attach filters to the
available_filters
Source attribute
(source$attributes$available_filters
).Examples:
shinyCohortBuilder
- tblist data class<- function(source, attach_as = c("step", "meta"), ...) {
autofilter.tblist <- rlang::arg_match(attach_as)
attach_as <- source$dtconn %>%
step_rule ::imap(~filter_rules(.x, .y)) %>%
purrrunlist(recursive = FALSE) %>%
::map(~do.call(cohortBuilder::filter, .)) %>%
purrrunname()
if (identical(attach_as, "meta")) {
$attributes$available_filters <- step_rule
sourceelse {
} %>%
source ::add_step(do.call(cohortBuilder::step, step_rule))
cohortBuilder
}
return(source)
}
.available_filters_choices
- define choices for
new step configuration panelRequired parameters:
source
- Source object,cohort
- Cohort object,...
- Unused, added for S3 integration only.Details:
shinyWidgets::prepare_choices
output.value
of prepare_choices
should point to filter ids.Examples:
shinyCohortBuilder
- tblist data class<- function(source, cohort, ...) {
.available_filters_choices.tblist
<- cohort$attributes$available_filters
available_filters
<- purrr::map(available_filters, function(x) {
choices ::tibble(
tibblename = as.character(
::div(
shiny`data-tooltip-z-index` = 9999,
`data-tooltip` = x$get_params("description"),
`data-tooltip-position` = "top right",
`data-tooltip-allow-html` = "true",
$name
x
)
),id = x$id,
dataset = x$get_params("dataset")
)%>% dplyr::bind_rows()
})
::prepare_choices(choices, name, id, dataset)
shinyWidgets }
.step_attrition
- define how step attrition
plot should be renderedRequired parameters:
source
- Source object.id
- Id of the attrition plot output.cohort
- Cohort object.session
- Shiny session object....
- Unused, added for S3 integration only.Details:
cohort$show_attrition
method to generate the plot (and pass required parameters to it when
needed, see “tblist” class example where dataset
is
needed).id
parameter to as an id of plot output
placeholder.Examples:
shinyCohortBuilder
- default method<- function(source, id, cohort, session, ...) {
.step_attrition.default <- session$ns
ns
list(
render = shiny::renderPlot({
$show_attrition()
cohort
}),output = shiny::plotOutput(id)
) }
shinyCohortBuilder
- tblist data class<- function(source, id, cohort, session, ...) {
.step_attrition.tblist <- session$ns
ns <- names(source$attributes$datasets)
choices
list(
render = shiny::renderPlot({
$show_attrition(dataset = session$input$attrition_input)
cohort
}),output = shiny::tagList(
::selectInput(ns("attrition_input"), "Choose dataset", choices),
shiny::plotOutput(id)
shiny
)
) }
.custom_attrition
- (optional) a custom method
used for your own version of attrition plotThe parameters and output structure is the same as for
.step_attrition
. The main difference is that you should put
your custom logic for generating attrition (i.e. using a specific
package meant for this).
When the method is defined, the attrition will be printed inside an extra tab of attrition modal.