R/input-select-period.R

Defines functions period_presets_vec input_select_period

Documented in input_select_period

#' Create a picker input to select aggregation period
#'
#' @param inputId the input slot that will be used to access the value.
#' @param selected_period a string indicating selected period
#' @param periods a named vector of periods.
#' @param label a string to display as label.
#' @param select_func a select input function. Either
#'   \code{\link[shiny]{selectInput}} or \code{\link[shinyWidgets]{pickerInput}}
#' @param ... additional parameters passed on to \code{select_func}
#' @export
#' @examples
#' \dontrun{
#' input_select_period('period') %>%
#'   shinybones::preview_component()
#' input_select_period('period', selected_period = 'week') %>%
#'   shinybones::preview_component()
#' }
input_select_period <- function(inputId,
                                selected_period = NULL,
                                periods = 'All',
                                label = 'aggregated_by',
                                select_func = shiny::selectInput,
                                ...){
  choices <- period_presets_vec(get_value(periods))
  select_func(
    inputId,
    label = label,
    choices = choices,
    selected = choices[selected_period],
    ...
  )
}

period_presets_vec <- function(periods = "All"){
  periods_all <-   c(
    "Day" = "day", "Week" = "week",
    "Month" = "month", "Quarter" = "quarter",
    "Year" = "year",
    "Rolling 7 Day" = 'rolling_7d', "Rolling 28 Day" = "rolling_28d",
    "Rolling 56 Day" = "rolling_56d"
  )
  if (length(periods) == 1 && periods == 'All'){
    periods_all
  } else {
    periods_all[periods_all %in% periods]
  }
}
ramnathv/shinymetrics documentation built on June 29, 2020, 10:39 p.m.