R/plot_pie_testdir.R

Defines functions plot_pie

# Function for plotting pie charts in test directory shiny apps with a uniform
# style
#' @export
plot_pie <- function(data,
                     id,
                     meta_data = NULL,
                     app = NULL,
                     subtitle = "Source: https://www.finddx.org/test-directory",
                     ...) {
  
  validate(need(nrow(data) > 0, "No data to show. Please choose different filters"))
  
  vars <-
    data |>
    pull(!! id)
  
  factors <- na.omit(unique(vars))
  
  x <- list(id, factors)
  
  #data$color <- unlist(purrr::pmap(x, map_color))
  
  data_pie <-
    data |>
    rename(value = !! id ) |>
    count(value) |>
    filter(!is.na(value)) |>
    rename(`Number of tests` = n)
  
  data_pie$color <- unlist(purrr::pmap(x, map_color))
  
  data_pie |>
    e_charts(value) |>
    e_pie(`Number of tests`, radius = c("50%", "70%"), left = "80") |>
    e_toolbox_feature(feature = "saveAsImage", title = "Save", name = "FIND Plot") |>
    e_labels(show = FALSE) |>
    e_tooltip() |>
    e_legend(left = "0",
             top = "30px",
             type = "scroll", 
             orient = "vertical",
             textStyle = list(width = "140", fontSize = "11", overflow = "break")
    ) |>
    e_add_nested("itemStyle", color) |>
    e_title(
      text = map_id(id, meta_data, app),
      subtitle = "Number of tests"
    ) |>
    (bottom = 0, subtitle = subtitle, height = 20)
  
}
finddx/shinyfind documentation built on May 28, 2024, 8:37 p.m.