R/csat_tab.R

Defines functions csatGaugeUI csatGauge csatDiffUI csatDiff csatWidgetUI csatWidget csatTemplateUI csatTemplate

Documented in csatDiff csatDiffUI csatGauge csatGaugeUI csatTemplate csatTemplateUI csatWidget csatWidgetUI

# For CSAT tab

#' Makes UI for intent KPI gauge & slider.
#'
#' Makes UI for intent KPI gauge & slider.
#' @export
#' @seealso \code{\link{intentGauge}}
#' @param id Module namespace.
#' @param hovertext string, RLumShiny::tooltip's text parameter; displayed when cursor hovers over plot
#' @return A shiny tagList() containing a flexdashboard gauge and sliderInput.
csatGaugeUI <- function(id, hovertext = NULL) {
  ns <- shiny::NS(id)
  shiny::tagList(
    flexdashboard::gaugeOutput(
      ns("csat_gauge"), width = "100%", height = "100%"),
    RLumShiny::tooltip(ns("csat_gauge"), hovertext, placement = "auto")
  )
}
#' Server-side code for intent KPI gauge & slider.
#'
#' Server-side code for intent KPI gauge & slider.  Uses dplyr:: functions to figure out what percentage of users intend to buy the product.
#' @export
#' @seealso \code{\link{intentGaugeUI}}
#' @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 ddat dataframe, data filtered by desired variables (probably demographic or demographic and product).
#' @param pcol numeric, number of the column in fdat containing data for gauge.
#' @param title string, gauge's title.
#' @param color_cutoff numeric, cutoff between different colours for the gauge.
#' @param minRange numeric, gauge's minimum value.
#' @param maxRange numeric, gauge's maximum value.
#' @param reversed logical, denotes whether gauge colours should be reversed (ie. warning colour for high values, happy colour for low values).
#' @param symbol string, e.g. "\%" or "miles" or whatever unit your gauge is showing.
csatGauge <- function(input, output, session, ddat, pcol, title,
                      color_cutoff, minRange, maxRange, reversed, symbol = '') {
  csat_gauge_reactive <- shiny::reactive({
    val_dat(ddat())
    x <- ddat() %>%
      dplyr::rename(primary_col = pcol) %>%
      cc() %>%
      dplyr::filter(primary_col %in% "Yes") %>%
      dplyr::select(`per`) %>%
      round(2)
    gauge <- flexdashboard::gauge(
      x[[1]],
      min = minRange, max = maxRange, symbol = symbol, label = title,
      flexdashboard::gaugeSectors(
        success = c(color_cutoff() + 0.5, maxRange),
        warning = c(color_cutoff() - 0.5, color_cutoff() + 0.5),
        danger = c(minRange, color_cutoff() - 0.5),
        colors = c("success", "warning", "danger"))
    )
  })
  output$csat_gauge <- flexdashboard::renderGauge({csat_gauge_reactive()})
}

#' UI for boxes showing difference between actual CSAT value and good value.
#'
#' UI for boxes showing difference between actual CSAT value and good value.
#' @seealso \code{\link{csatDiff}}
#' @param id Module namespace.
#' @return A flexdashboard valueBox.
csatDiffUI <- function(id) {
  ns <- shiny::NS(id)
  flexdashboard::valueBoxOutput(ns("diff")) # Yes, this uses flex- rather than shiny-
}

#' Server-side function for boxes showing difference between actual CSA value and good value.
#'
#' Server-side function for boxes showing difference between actual CSA value and good value.  Makes use of rating_diff_value(), rating_diff_box().
#' @seealso \code{\link{csatDiffUI}}, \code{\link{rating_diff_value}}, \code{\link{rating_diff_box}}
#' @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 ddat dataframe, data filtered by desired variables (probably demographic or demographic and product).
#' @param pcol numeric, number of the column in fdat containing data for gauge.
#' @param color_cutoff numeric, cutoff between different colours for the gauge.
#' @param reversed logical, denotes whether gauge colours should be reversed (ie. warning colour for high values, happy colour for low values).
csatDiff <- function(input, output, session,
                     ddat, pcol, color_cutoff, reversed) {
  output$diff <- shinydashboard::renderValueBox({
    val_dat(ddat())
    output <- rating_diff_value(ddat(), pcol, color_cutoff()) %>%
      rating_diff_box(reversed = reversed)
  })
}

#' UI for a CSAT element with title, gauge, and difference.
#'
#' UI for a CSAT element with title, gauge, and difference.
#' @export
#' @seealso \code{\link{csatWidget}}
#' @param id Module namespace.
#' @param title string, labels gauge-diff combo element (probably identical to gauge title).
#' @return A shiny tagList() containing a string (title), gauge, and box (difference).
csatWidgetUI <- function(id, title) {
  ns <- shiny::NS(id)
  shiny::tagList(
    title,
    userFractionGaugeUI(ns("gauge")),
    csatDiffUI(ns("diff"))
  )
}
#' Server-side function for CSAT element with title, gauge, and difference.
#'
#' Server-side function for CSAT element with title, gauge, and difference.
#' @export
#' @seealso \code{\link{csatWidgetUI}}
#' @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 ddat dataframe, data filtered by desired variables (probably demographic or demographic and product).
#' @param pcol numeric, number of the column in fdat containing data for gauge.
#' @param title string, gauge's title.
#' @param color_cutoff numeric, cutoff between different colours for the gauge.
#' @param minRange numeric, gauge's minimum value.
#' @param maxRange numeric, gauge's maximum value.
#' @param reversed logical, denotes whether gauge colours should be reversed (ie. warning colour for high values, happy colour for low values).
#' @param symbol string, a symbol to pass through to userFractionGauge(); \'\' / empty by default.
csatWidget <- function(input, output, session, ddat, pcol, title,
                       color_cutoff, minRange, maxRange, reversed,
                       symbol = "%") {
  shiny::callModule(
    userFractionGauge, "gauge", ddat, pcol, color_cutoff, "Yes", symbol, title,
    minRange, maxRange, reversed)
  shiny::callModule(
    csatDiff, "diff", ddat, pcol, color_cutoff, reversed)
}

#' Initial CSAT UI for pepsi data
#'
#' Initial CSAT UI for pepsi data
#' @export
#' @seealso \code{\link{csatTemplate}}
#' @param id Module namespace.
#' @return A shiny tagList() containing 2 boxes each with 4 CSAT widgets.
csatTemplateUI <- function(id) {
  ns <- shiny::NS(id)
  shiny::tagList(
    shinydashboard::box(
      title = "ALL PRODUCTS", status = "primary", width = 6,
      solidHeader = TRUE,
      csatWidgetUI(ns("all_flavor"), "Flavor"),
      csatWidgetUI(ns("all_quality"), "Quality"),
      csatWidgetUI(ns("all_likeme"), "For Someone Like Me"),
      csatWidgetUI(ns("all_preferanother"), "Prefer Another Brand")),
    shinydashboard::box(
      title = "SELECTED PRODUCTS", status = "primary", width = 6,
      solidHeader = TRUE,
      csatWidgetUI(ns("sel_flavor"), "Flavor"),
      csatWidgetUI(ns("sel_quality"), "Quality"),
      csatWidgetUI(ns("sel_likeme"), "For Someone Like Me"),
      csatWidgetUI(ns("sel_preferanother"), "Prefer Another Brand"))
  )
}
#' Initial server-side CSAT for pepsi data.
#'
#' Initial server-side CSAT for pepsi data.
#' @export
#' @seealso \code{\link{csatTemplateUI}}
#' @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 ddat dataframe, data filtered by demographic variables.
#' @param fdat dataframe, data filtered by demographic and product variables.
#' @param color_cutoff vector, numerics defining cutoffs between different colours for the gauges.
#' @param columns vector numerics, column numbers denoting pcols for each plot.
#' @param gaugeTitles vector, titles for each plot (strings)
#' @param minRange numeric, gauges' minimum value.
#' @param maxRange numeric, gauges' maximum value.
#' @param reversed logical, denotes whether gauge colours should be reversed (ie. warning colour for high values, happy colour for low values).
csatTemplate <- function(input, output, session, ddat, fdat, color_cutoff,
                         columns = c(flavor_col, quality_col, likeme_col, preferother_col),
                         gaugeTitles = c("Flavor", "Quality", "Like Me", "Prefer another"),
                         minRange = 1, maxRange = 5, reversed = FALSE) {
  shiny::callModule(
    csatWidget, "all_flavor", ddat, columns[[1]], gaugeTitles[[1]],
    color_cutoff, minRange, maxRange, reversed)
  shiny::callModule(
    csatWidget, "all_quality", ddat, columns[[2]], gaugeTitles[[2]],
    color_cutoff, minRange, maxRange, reversed)
  shiny::callModule(
    csatWidget, "all_likeme", ddat, columns[[3]], gaugeTitles[[3]],
    color_cutoff, minRange, maxRange, reversed)
  shiny::callModule(
    csatWidget, "all_preferanother", ddat, columns[[4]], gaugeTitles[[4]],
    color_cutoff, minRange, maxRange, reversed = TRUE)
  shiny::callModule(
    csatWidget, "sel_flavor", fdat, columns[[1]], gaugeTitles[[1]],
    color_cutoff, minRange, maxRange, reversed)
  shiny::callModule(
    csatWidget, "sel_quality", fdat, columns[[2]], gaugeTitles[[2]],
    color_cutoff, minRange, maxRange, reversed)
  shiny::callModule(
    csatWidget, "sel_likeme", fdat, columns[[3]], gaugeTitles[[3]],
    color_cutoff, minRange, maxRange, reversed)
  shiny::callModule(
    csatWidget, "sel_preferanother", fdat, columns[[4]], gaugeTitles[[4]],
    color_cutoff, minRange, maxRange, reversed = TRUE)
}
IskanderBlue/morseldash documentation built on Oct. 30, 2019, 7:24 p.m.