R/overview_tab.R

Defines functions overviewUI overview

Documented in overview overviewUI

# Overviews tab

#' Makes UI for overview plots.
#'
#' Makes UI for overview plots.
#' @export
#' @seealso \code{\link{overview}}
#' @param id Module namespace.
#' @param hovertext string, RLumShiny::tooltip's text parameter; displayed when cursor hovers over plot
#' @return A shiny tagList() containing a formatted ggplot2 plot.
overviewUI <- function(id, hovertext = NULL) {
  ns <- shiny::NS(id)
  shiny::tagList(
    shiny::fluidRow(
      shiny::column(
        width = 12,
        shinydashboard::box(
          title = "Overview", status = "info", width = 12,
          solidHeader = TRUE, collapsible = TRUE,
          shiny::plotOutput(ns("overview"), height = "600px"),
          RLumShiny::tooltip(ns("overview"),
                             text = hovertext, placement = "auto")
        )
      )
    )
  )
}
# server-side
#' Server-side code for overview plots.
#'
#' Server-side code for overview plots.
#' @seealso \code{\link{overviewUI}}, \code{\link{factor_overview_plotter}}
#' @export
#' @param input Required for shiny modules' server functions.
#' @param output Required for shiny modules' server functions.
#' @param session Required for shiny modules' server functions.
#' @param fdat dataframe, filtered by demographic variables and product.
#' @param overview_options  vector, 4 elements: 1) numeric, column # containing primary column for plot, 2) function, the function to generate the plot, 3) numeric, a cutoff dividing 'good' from 'bad' values (green vs red); c(pcol, title, plot_function, color_cutoff), 4) reversed_values, logical, whether to flip valence; see \code{\link{factor_overview_plotter}}.
#' @param demo_pick_one string, the current 1st demographic variable selected
#' @param demo_pick_two string, the current 2nd demographic variable selected
#' @param title_suffix vector of two strings, the title suffix if normal and if isTRUE(reversed_values).
overview <- function(input, output, session, fdat,
                     overview_options, demo_pick_one, demo_pick_two,
                     title_suffix = overview_suffix) {
  suffix <- reactive({
    if (isTRUE(overview_options()[[4]])) {
      suffix <- title_suffix[[2]]
    } else {suffix <- title_suffix[[1]]}
  })
  plt <- shiny::reactive({
    val_dat(fdat())
    fdat() %>%
      dplyr::rename(target_column = overview_options()[[1]]) %>%
      overview_options()[[2]](
        paste0(names(fdat())[overview_options()[[1]]], suffix()),
        overview_options()[[3]],
        demo_pick_one(),
        demo_pick_two(),
        reversed_values = overview_options()[[4]])
  })
  output$overview <- shiny::renderPlot(plt())
}
IskanderBlue/morseldash documentation built on Oct. 30, 2019, 7:24 p.m.