Skip to contents

The function returns filtering panel placeholder, you may use in you custom Shiny application. Use in the UI part of your application.

Usage

cb_ui(
  id,
  ...,
  state = FALSE,
  steps = TRUE,
  code = TRUE,
  attrition = TRUE,
  new_step = c("clone", "configure"),
  manage_step = FALSE,
  assistant = FALSE
)

cb_server(
  id,
  cohort,
  run_button = "none",
  stats = c("pre", "post"),
  feedback = FALSE,
  render_source = "auto",
  enable_bookmarking = shiny::getShinyOption("bookmarkStore", default = "disable"),
  show_help = TRUE,
  chat = NULL,
  ...
)

Arguments

id

Id of the module used to render the panel.

...

Extra attributes passed to the panel div container.

state

Set to TRUE (default) to enable get/set state panel.

steps

Set to TRUE (default) if multiple steps should be available.

code

Set to TRUE (default) to enable reproducible code panel.

attrition

Set to TRUE (default) to enable attrition plot panel.

new_step

Choose which add step method should be used for creating new step. Possible options are: "clone" - copy filters from last step, "configure" - opening modal and allow to chose filters from available filters.

manage_step

When `TRUE`, enables feature, that allows to modify the latest step filters (add/remove them). Available list of filters used by the feature should be stored as `source$available_filters` object (can be defined with `available_filters` argument for set_source).

assistant

Set to TRUE to enable the LLM cohort assistant panel.

cohort

Cohort object storing filtering steps configuration.

run_button

Should Run button be displayed? If so, the current step computations are run only when clicked. Three options are available "none" - no button, "local" - button displayed at each step panel, "global" - button visible in top filtering panel.

stats

Choose which statistics should be displayed for data (and some filters). Possible options are: "pre" - previous step stat, "post" - current step stats, `c("pre", "post")` - for both and NULL for no stats.

feedback

Set to TRUE (default) if feedback plots should be displayed at each filter.

render_source

Controls how filter inputs (choices/ranges) are sourced when a filter declares a `domain`. Possible options are: "auto" (default) - use cached statistics in stats mode, and the filter's domain when stats are disabled (`stats = NULL` and `feedback = FALSE`); "domain" - in stats mode, build choices and range bounds from the full domain vocabulary and overlay counts from statistics where available. A filter without a domain always falls back to statistics. The value can be overridden per filter via `filter@extra$render_source`.

enable_bookmarking

Set to TRUE (default) if panel should be compatible with native shiny bookmarking.

show_help

Set to TRUE (default) to enable help buttons.

chat

A chat client object (e.g. from 'ellmer') passed to the assistant; `NULL` (default) disables the assistant server logic.

Value

Nested list of `shiny.tag` objects - html structure of filtering panel module.

`shiny::moduleServer` output providing server logic for filtering panel module.

Examples

if (interactive()) {
  library(cohortBuilder)
  library(shiny)
  library(shinyCohortBuilder)

  librarian_source <- set_source(as.tblist(librarian))
  librarian_cohort <- cohort(
    librarian_source,
    filter(
      "discrete", id = "author", dataset = "books",
      variable = "author", value = "Dan Brown",
      active = FALSE
    ),
    filter(
      "range", id = "copies", dataset = "books",
      variable = "copies", range = c(5, 10),
      active = FALSE
    ),
    filter(
      "date_range", id = "registered", dataset = "borrowers",
      variable = "registered", range = c(as.Date("2010-01-01"), Inf),
      active = FALSE
    )
  )

  ui <- fluidPage(
    sidebarLayout(
      sidebarPanel(
        cb_ui("librarian")
      ),
      mainPanel()
    )
  )

  server <- function(input, output, session) {
    cb_server("librarian", librarian_cohort)
  }

  shinyApp(ui, server)
}
if (interactive()) {
  # enabling latest step filters management
  library(cohortBuilder)
  library(shiny)
  library(shinyCohortBuilder)

  librarian_source <- set_source(
    as.tblist(librarian),
    available_filters = list(
      filter(
        "discrete", id = "author", dataset = "books",
        variable = "author", value = "Dan Brown",
        active = FALSE
      ),
      filter(
        "range", id = "copies", dataset = "books",
        variable = "copies", range = c(5, 10),
        active = FALSE
      ),
      filter(
        "date_range", id = "registered", dataset = "borrowers",
        variable = "registered", range = c(as.Date("2010-01-01"), Inf),
        active = FALSE
      ),
      filter(
        "discrete", id = "program", dataset = "borrowers",
        variable = "program", value = NA,
        active = FALSE
      )
    )
  )
  librarian_cohort <- cohort(
    librarian_source,
    filter(
      "range", id = "copies", dataset = "books",
      variable = "copies", range = c(5, 10),
      active = FALSE
    ),
    filter(
      "date_range", id = "registered", dataset = "borrowers",
      variable = "registered", range = c(as.Date("2010-01-01"), Inf),
      active = FALSE
    )
  )

  ui <- fluidPage(
    sidebarLayout(
      sidebarPanel(
        cb_ui("librarian", manage_step = TRUE)
      ),
      mainPanel()
    )
  )

  server <- function(input, output, session) {
    cb_server("librarian", librarian_cohort)
  }

  shinyApp(ui, server)
}