Skip to contents

Filter linelist data using a sidebar with shiny inputs.

Usage

filter_ui(
  id,
  group_vars,
  date_range,
  title = NULL,
  date_filters_lab = "Date filters",
  period_lab = "Period",
  missing_dates_lab = "Include patients with missing dates?",
  group_filters_lab = "Group filters",
  filter_btn_lab = "Filter",
  reset_btn_lab = "Reset"
)

filter_server(
  id,
  df,
  date_var,
  group_vars,
  time_filter = shiny::reactiveVal(),
  place_filter = shiny::reactiveVal(),
  na_label = getOption("epishiny.na.label", "(Missing)")
)

Arguments

id

Module id. Must be the same in both the UI and server function to link the two.

group_vars

named character vector of categorical variables for the data grouping input. Names are used as variable labels.

date_range

A vector containing the minimum and maximum dates for the date range input.

title

The title of the sidebar.

date_filters_lab

The label for the date filters accordion panel.

period_lab

The label for the date range input.

missing_dates_lab

The label for the include missing dates checkbox.

group_filters_lab

The label for the group filters accordion panel.

filter_btn_lab

The label for the filter data button.

reset_btn_lab

The label for the reset filters button.

df

Data frame or tibble of patient level or aggregated data. Can be either a shiny reactive or static dataset.

date_var

The name of the date variable in the data frame to be filtered on.

time_filter

supply the output of time_server() wrapped in a shiny::reactive() here to add its filter information to the filter sidebar

place_filter

supply the output of place_server() wrapped in a shiny::reactive() here to add its filter information to the filter sidebar

na_label

The label to use for missing values in group variables.

Value

A bslib::sidebar UI element with date filters, group filters, and action buttons.

The server function returns both the filtered data and a formatted text string with filter information named df and filter_info respectively in a reactive list. These should be passed as arguments of the same name in the time, place and person modules wrapped in a shiny::reactive()

Examples

library(shiny)
library(bslib)
#> 
#> Attaching package: ‘bslib’
#> The following object is masked from ‘package:utils’:
#> 
#>     page
library(epishiny)

# example package data
data("df_ll") # linelist
data("sf_yem") # sf geo boundaries for Yemen admin 1 & 2

# setup geo data for adm1 and adm2 using the
# geo_layer function to be passed to the place module
# if population variable is provided, attack rates
# will be shown on the map as a choropleth
geo_data <- list(
  geo_layer(
    layer_name = "Governorate", # name of the boundary level
    sf = sf_yem$adm1, # sf object with boundary polygons
    name_var = "adm1_name", # column with place names
    pop_var = "adm1_pop", # column with population data (optional)
    join_by = c("pcode" = "adm1_pcode") # geo to data join vars: LHS = sf, RHS = data
  ),
  geo_layer(
    layer_name = "District",
    sf = sf_yem$adm2,
    name_var = "adm2_name",
    pop_var = "adm2_pop",
    join_by = c("pcode" = "adm2_pcode")
  )
)

# range of dates used in filter module to filter time period
date_range <- range(df_ll$date_notification, na.rm = TRUE)

# define date variables in data as named list to be used in app
date_vars <- c(
  "Date of notification" = "date_notification",
  "Date of onset" = "date_symptom_start",
  "Date of hospitalisation" = "date_hospitalisation_start",
  "Date of outcome" = "date_hospitalisation_end"
)

# define categorical grouping variables
# in data as named list to be used in app
group_vars <- c(
  "Governorate" = "adm1_origin",
  "Sex" = "sex_id",
  "Hospitalised" = "hospitalised_yn",
  "Vaccinated measles" = "vacci_measles_yn",
  "Outcome" = "outcome"
)

# user interface
ui <- page_sidebar(
  title = "epishiny",
  # sidebar
  sidebar = filter_ui(
    "filter",
    group_vars = group_vars,
    date_range = date_range,
    period_lab = "Notification period"
  ),
  # main content
  layout_columns(
    col_widths = c(12, 7, 5),
    place_ui(
      id = "map",
      geo_data = geo_data,
      group_vars = group_vars
    ),
    time_ui(
      id = "curve",
      title = "Time",
      date_vars = date_vars,
      group_vars = group_vars,
      ratio_line_lab = "Show CFR line?"
    ),
    person_ui(id = "age_sex")
  )
)

# app server
server <- function(input, output, session) {
  app_data <- filter_server(
    id = "filter",
    df = df_ll,
    date_var = "date_notification",
    group_vars = group_vars
  )
  place_server(
    id = "map",
    df = reactive(app_data()$df),
    geo_data = geo_data,
    group_vars = group_vars,
    filter_info = reactive(app_data()$filter_info)
  )
  time_server(
    id = "curve",
    df = reactive(app_data()$df),
    date_vars = date_vars,
    group_vars = group_vars,
    show_ratio = TRUE,
    ratio_var = "outcome",
    ratio_lab = "CFR",
    ratio_numer = "Deceased",
    ratio_denom = c("Deceased", "Healed", "Abandonment"),
    filter_info = reactive(app_data()$filter_info)
  )
  person_server(
    id = "age_sex",
    df = reactive(app_data()$df),
    age_var = "age_years",
    sex_var = "sex_id",
    male_level = "Male",
    female_level = "Female",
    filter_info = reactive(app_data()$filter_info)
  )
}

# launch app
if (interactive()) {
  shinyApp(ui, server)
}