R/mod_06_take_up_region.R

Defines functions mod_06_take_up_region_server mod_06_take_up_region_ui

#' 06_take_up_region UI Function
#'
#' @description A shiny Module.
#'
#' @param id,input,output,session Internal parameters for {shiny}.
#'
#' @noRd
#'
#' @importFrom shiny NS tagList
mod_06_take_up_region_ui <- function(id) {
  ns <- NS(id)
  tagList(
    h2("Estimated take-up is low and decreasing over time"),
    p(
      "Estimated take-up is the number of people who receive full or ",
      "partial benefit from applications to the NHS Low Income Scheme,",
      "submitted in the time period, per thousand of the general ",
      "population aged 16 or over."
    ),
    p(
      tags$b(
        "Estimated take-up per thousand of the general population was 8 in ",
        "2015/16, decreasing to 4 in 2020/21."
      )
    ),
    p(
      "By region, we can see that estimated take-up, relative to the ",
      "population, continues to be ",
      tags$b("highest in the North East of England"),
      "and the North in general. Although the North East rate has declined ",
      "from 13 in 2015/16 to 6 in 2020/21."
    ),
    nhs_card(
      heading = "Estimated take-up of NHS Low Income Scheme in England (2015/16 to 2020/21)",
      highcharter::highchartOutput(
        outputId = ns("plot_successful_individuals_by_region"),
        height = "350px"
      ),
      mod_nhs_download_ui(
        id = ns("download_successful_individuals_by_region")
      )
    )
  )
}

#' 06_take_up_region Server Functions
#'
#' @noRd
mod_06_take_up_region_server <- function(id) {
  moduleServer(id, function(input, output, session) {
    ns <- session$ns

    # Calculate take-up rate per 1k adult population of each region
    # Because of inner join from other data-raw output, calculating SDC here
    successful_individuals_by_region_df <-
      lowIncomeSchemeScrollytellR::adult_population_df %>%
      dplyr::group_by(FINANCIAL_YEAR, PCD_REGION_NAME) %>%
      dplyr::summarise(TOTAL_POPULATION = sum(TOTAL_ADULT_POPULATION)) %>%
      dplyr::ungroup() %>%
      dplyr::inner_join(
        y = lowIncomeSchemeScrollytellR::successful_individuals_by_region_df
      ) %>%
      dplyr::mutate(
        TAKE_UP_PER_THOUSAND = janitor::round_half_up(
          TOTAL_SUCCESSFUL_INDIVIDUALS / TOTAL_POPULATION * 1000, 1
        ),
        TOTAL_SUCCESSFUL_INDIVIDUALS = round(TOTAL_SUCCESSFUL_INDIVIDUALS, -1)
      )


    # Add data download
    mod_nhs_download_server(
      id = "download_successful_individuals_by_region",
      filename = "successful_individuals_by_region.csv",
      export_data = successful_individuals_by_region_df
    )

    # Create chart
    output$plot_successful_individuals_by_region <-
      highcharter::renderHighchart({
        successful_individuals_by_region_df %>%
          highcharter::hchart(
            type = "line",
            highcharter::hcaes(
              x = FINANCIAL_YEAR,
              y = TAKE_UP_PER_THOUSAND,
              group = PCD_REGION_NAME
            )
          ) %>%
          theme_nhsbsa(stack = NA) %>%
          highcharter::hc_yAxis(
            title = list(text = "Per thousand of the general population")
          ) %>%
          highcharter::hc_xAxis(
            title = list(text = "Financial year")
          ) %>%
          highcharter::hc_tooltip(
            shared = TRUE,
            valueDecimals = 1
          )
      })
  })
}

## To be copied in the UI
# mod_06_take_up_region_ui("06_take_up_region_ui_1")

## To be copied in the server
# mod_06_take_up_region_server("06_take_up_region_ui_1")
nhsbsa-data-analytics/nhs-low-income-scheme-scrollytell documentation built on Aug. 10, 2024, 6:04 a.m.