R/rnd_tab.R

Defines functions simpleBarPlotUI simpleBarPlot

Documented in simpleBarPlot simpleBarPlotUI

#' Shiny UI wrapper for bp(); makes horizontal bar plots, stacked by Gender.
#'
#' Shiny UI wrapper for bp(); makes horizontal bar plots, stacked by Gender.
#' @export
#' @seealso \code{\link{simpleBarPlot}}, \code{\link{bp}}
#' @param id Module namespace.
#' @param box_title string, title for box containing plot
#' @return Shiny tagList containing plotly bar plot inside a shindashboard box inside a 3/4 wide column.
simpleBarPlotUI <- function(id, box_title = "On a scale from 1 to 5 [...] please rate the following features") {
  ns <- shiny::NS(id)
  shiny::tagList(
    shiny::column(width = 9,
                  box_d(title = box_title,
                        plotly::plotlyOutput(ns("plot"))
                  )
    )
  )
}
#' Shiny UI wrapper for bp(); makes horizontal bar plots, stacked by Gender.
#'
#' Shiny UI wrapper for bp(); makes horizontal bar plots, stacked by Gender.
#' @export
#' @seealso \code{\link{simpleBarPlotUI}}, \code{\link{bp}}
#' @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, typically filtered by demographic variables and product.
#' @param bar_selected string, name of column of interest; not numeric, used as plot title too.
#' @param demo_selected string, name of demographic column to be renamed sec_col, secondary column of interest.
#' @param plot_colors vector, (strings), colors for Gender breakdown.
simpleBarPlot <- function(input, output, session, fdat, bar_selected, demo_selected,
                          plot_colors = fmu_colors) {
  plot <- shiny::reactive({
    val_dat(fdat())
    fdat() %>%
      dplyr::rename(primary_col = bar_selected()) %>%
      dplyr::rename(sec_col = demo_selected()) %>%
      group_by_primary_sec(mode_by_user = FALSE) %>%
      dplyr::filter(!is.na(primary_col)) %>%
      dplyr::mutate(primary_col = factor(primary_col)) %>%
      bp(bar_selected(), plot_colors)
  })
  output$plot <- plotly::renderPlotly({plot()})
}
IskanderBlue/morseldash documentation built on Oct. 30, 2019, 7:24 p.m.