Skip to contents

Visualise age and sex demographics in a population pyramid chart and summary table.

Usage

person_ui(
  id,
  count_vars = NULL,
  title = "Person",
  icon = bsicons::bs_icon("people-fill"),
  opts_btn_lab = "options",
  count_vars_lab = "Indicator",
  full_screen = TRUE
)

person_server(
  id,
  df,
  sex_var,
  male_level,
  female_level,
  age_group_var = NULL,
  age_var = NULL,
  count_vars = NULL,
  age_breaks = c(0, 5, 18, 25, 35, 50, Inf),
  age_labels = c("<5", "5-17", "18-24", "25-34", "35-49", "50+"),
  age_var_lab = "Age (years)",
  age_group_lab = "Age group",
  n_lab = "N patients",
  colours = c("#19a0aa", "#f15f36"),
  filter_info = shiny::reactiveVal(),
  time_filter = shiny::reactiveVal(),
  place_filter = shiny::reactiveVal()
)

Arguments

id

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

count_vars

If data is aggregated, variable name(s) of count variable(s) in data. If more than one variable provided, a select input will appear in the options dropdown. If named, names are used as variable labels.

title

The title for the card.

icon

The icon to display next to the title.

opts_btn_lab

The label for the options button.

count_vars_lab

text label for the aggregate count variables input.

full_screen

Add button to card to with the option to enter full screen mode?

df

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

sex_var

The name of the sex variable in the data.

male_level

The level representing males in the sex variable.

female_level

The level representing females in the sex variable.

age_group_var

The name of a character/factor variable in the data with age groups. If specified, age_var is ignored.

age_var

The name of a numeric age variable in the data. If ages have already been binned into groups, use age_group_var instead.

age_breaks

A numeric vector specifying age breaks for age groups.

age_labels

Labels corresponding to the age breaks.

age_var_lab

The label for the age variable.

age_group_lab

The label for the age group variable.

n_lab

The label for the raw count variable.

colours

Vector of 2 colours to represent male and female, respectively.

filter_info

If contained within an app using filter_server(), supply the filter_info object returned by that function here wrapped in a shiny::reactive() to add filter information to chart exports.

time_filter

supply the output of time_server() wrapped in a shiny::reactive() here to filter the data by click events on the time module bar chart (clicking a bar will filter the data to the period the bar represents)

place_filter

supply the output of place_server() wrapped in a shiny::reactive() here to filter the data by click events on the place module map (clicking a polygon will filter the data to the clicked region)

Value

A bslib::navset_card_tab UI element with chart and table tabs.

Examples

library(shiny)
library(bslib)
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)
}