R/mod_histogram.R

Defines functions mod_histogram_server mod_histogram_ui

#' histogram UI Function
#'
#' @description A shiny Module.
#'
#' @param id,input,output,session Internal parameters for {shiny}.
#'
#' @noRd 
#'
#' @importFrom shiny NS tagList 
mod_histogram_ui <- function(id){
  ns <- NS(id)
  tagList(
 shiny::plotOutput(ns('histogram'))
  )
}
    
#' histogram Server Functions
#'
#' @noRd 
mod_histogram_server <- function(id,
                                 metric,
                                 title= NULL,
                                 xtitle = NULL,
                                 ytitle = NULL,
                                 facet = NULL,
                                 min = NULL,
                                 max = NULL){
  
  moduleServer( id, function(input, output, session){
    ns <- session$ns
 
    if(rlang::is_null(facet) == TRUE){
      
      output$histogram <- shiny::renderPlot({
      
        dataset %>% 
          dplyr::select(c(metric)) %>% 
          na.omit() %>% 
          ggplot2::ggplot(ggplot2::aes(x= .data[[metric]])) + 
          ggplot2::geom_histogram(fill = 'tomato' ,
                                  alpha = 0.7 ) + 
          ggplot2::labs(y = 'Count') + 
          bbplot::bbc_style() +
          ggplot2::labs(title=title) +
          ggplot2::theme(axis.title.x = ggplot2::element_text(size = 16),
                         axis.title.y = ggplot2::element_text(size = 16),
                         plot.title = ggplot2::element_text(hjust = .5))  
        
        
        })
      
    } else if(rlang::is_null(facet) == FALSE){
    
      output$histogram <- shiny::renderPlot({
        

        dataset %>% 
          dplyr::select(c(metric,facet)) %>% 
          na.omit() %>% 
          ggplot2::ggplot(
            ggplot2::aes(x = .data[[metric]], y = .data[[facet]])) +
          ggridges::geom_density_ridges(alpha = .7, rel_min_height = .01,
                                        fill = "tomato") +
          # scale_fill_cyclical(breaks = c("1997 Summer", "1997 Winter"),
          #                     labels = c(`1997 Summer` = "Summer",
          #                                `1997 Winter` = "Winter"),
          #                     values = c("tomato", "dodgerblue"),
          #                     name = "Season:", guide = "legend") +
          #theme_ridges(grid = FALSE) +
          
          bbplot::bbc_style() +
          
          ggplot2::labs(title=title) +
          
          ggplot2::theme(axis.title.x = ggplot2::element_text(size = 18)) +
          
          ggplot2::xlim(c(min,max)) +
          ggplot2::theme(axis.title.x = ggplot2::element_text(size = 16),
                         plot.title = ggplot2::element_text(hjust = .5))  

        
    })
      
    }    
    
    
    
    
  })
}
    
## To be copied in the UI
# mod_histogram_ui("histogram_ui_1")
    
## To be copied in the server
# mod_histogram_server("histogram_ui_1")
gavandrewj/pdphApp documentation built on Dec. 20, 2021, 9:48 a.m.