R/mod_faithful_histogram.R

Defines functions mod_faithful_histogram_server mod_faithful_histogram_ui

Documented in mod_faithful_histogram_server mod_faithful_histogram_ui

#' Old Faithful histogram Shiny module
#'
#' @description UI and server function for an example Shiny module.
#'
#' @param id Internal parameter for {shiny}.
#' @param variable character variable name.
#'
#' @name mod_faithful_histogram
#'
NULL

#' @rdname mod_faithful_histogram
#'
#' @import shiny
mod_faithful_histogram_ui <- function(id) {
  ns <- NS(id)
  tagList(
    fluidRow(
      column(
        3,
        sliderInput(
          ns("bins"), "Number of bins:",
          min = 1, max = 50, value = 30
        ),
        checkboxInput(
          ns("density"),
          "density"
        )
      ),
      
      # Show a plot of the generated distribution
      column(
        9,
        plotOutput(ns("distPlot"))
      )
    )
  )
}

#' @rdname mod_faithful_histogram
mod_faithful_histogram_server <- function(id, variable = NULL){
  moduleServer( id, function(input, output, session){
    ns <- session$ns
    # generate bins based on input$bins from ui.R
    x    <- datasets::faithful[, variable]
    bins <- reactive(seq(min(x), max(x), length.out = input$bins + 1))
    
    output$distPlot <- renderPlot({
      # draw the histogram with the specified number of bins
      plot_hist(x = x, breaks = bins(), freq = !input$density)
    })
  })
}
guidomaggioorg/ShinyProdWorkshopPublic documentation built on Feb. 18, 2022, 1:49 a.m.