R/table_reactable.R

Defines functions table_reactable

Documented in table_reactable

#' table_reactable
#'
#' @export
#'
table_reactable <- function(covid_dat, all_dat, totals) {

  tmp_covid <- covid_dat %>%
    tibble::rownames_to_column("area") %>%
    reshape2::melt(id.var = "area", variable.name = "date")
  dat_covid <- tmp_covid %>%
    dplyr::group_by(area) %>%
    dplyr::summarise(covid_deaths = list(value),
                     covid_total = sum(value, na.rm = TRUE), .groups = "drop") %>%
    dplyr::mutate(covid_sparkline = NA)

  tmp_all <- all_dat %>%
    tibble::rownames_to_column("area") %>%
    reshape2::melt(id.var = "area", variable.name = "date")
  dat_all <- tmp_all %>%
    dplyr::group_by(area) %>%
    dplyr::summarise(all_deaths = list(value),
                     all_total = sum(value, na.rm = TRUE), .groups = "drop") %>%
    dplyr::mutate(all_sparkline = NA)

  tab.this <- merge(dat_covid, dat_all, by = "area") %>%
    merge(totals, by = "area", all.x = TRUE) %>%
    dplyr::mutate(covid_per100 = round((covid_total / population) * 100000),
                  all_per100 = round((all_total / population) * 100000)) %>%
    dplyr::select(area, population, covid_deaths, covid_total, covid_per100,
                  covid_sparkline, all_deaths, all_total, all_per100,
                  all_sparkline)

  reactable::reactable(tab.this, columns = list(

    area = reactable::colDef(name = "Area"),

    population = reactable::colDef(
      name = "Population size (2018)",
      align = "left", cell = function(x) {
        width = paste0(((x / max(tab.this$population)) * 100), "%")
        buffer = max(nchar(tab.this$population)) - nchar(x)
        marginLeft = paste0(((buffer * 6) + 8), "px")
        bar_chart(x, width, marginLeft)
      }),

    covid_deaths = reactable::colDef(
      show = FALSE,
      cell = function(x)
      sparkline::sparkline(x, type = "bar", chartRangeMin = 0,
                           chartRangeMax = max(tmp_covid$value))
      ),

    covid_total = reactable::colDef(
      name = "Covid deaths",
      align = "left", cell = function(x) {
        width = paste0(((x / max(tab.this$covid_total)) * 100), "%")
        buffer = max(nchar(tab.this$covid_total)) - nchar(x)
        marginLeft = paste0(((buffer * 6) + 8), "px")
        bar_chart(x, width, marginLeft)
      }),

    covid_per100 = reactable::colDef(
      name = "Per 100k",
      align = "left", cell = function(x) {
        width = paste0(((x / max(tab.this$covid_per100)) * 100), "%")
        buffer = max(nchar(tab.this$covid_per100)) - nchar(x)
        marginLeft = paste0(((buffer * 6) + 8), "px")
        bar_chart(x, width, marginLeft)
      }),

    covid_sparkline = reactable::colDef(
      name = "Weekly count",
      cell = function(covid_deaths, index)
      sparkline::sparkline(tab.this$covid_deaths[[index]])
      ),

    all_deaths = reactable::colDef(
      name = "Daily count",
      show = FALSE,
      cell = function(x)
      sparkline::sparkline(x, type = "bar", chartRangeMin = 0,
                           chartRangeMax = max(tmp_all$value))
      ),

    all_total = reactable::colDef(
      name = "All deaths",
      align = "left", cell = function(x) {
        width = paste0(((x / max(tab.this$all_total)) * 100), "%")
        buffer = max(nchar(tab.this$all_total)) - nchar(x)
        marginLeft = paste0(((buffer * 6) + 8), "px")
        bar_chart(x, width, marginLeft)
      }),

    all_per100 = reactable::colDef(
      name = "Per 100k",
      align = "left", cell = function(x) {
        width = paste0(((x / max(tab.this$all_per100)) * 100), "%")
        buffer = max(nchar(tab.this$all_per100)) - nchar(x)
        marginLeft = paste0(((buffer * 6) + 8), "px")
        bar_chart(x, width, marginLeft)
      }),

    all_sparkline = reactable::colDef(
      name = "Weekly count",
      cell = function(all_deaths, index)
        sparkline::sparkline(tab.this$all_deaths[[index]])
    )
  ))

}
soniamitchell/SCRCshinyApp documentation built on July 7, 2020, 5:36 a.m.