R/switch.R

Defines functions switch_server switch_ui

Documented in switch_server switch_ui

#' Switch module UI
#'
#' @description A switch module UI that can accept dynamic inputs on the server-side
#' declaration
#' @param id is unique ID associated with the switch for the UI namespace
#'
#' @return HTML UI code for a shiny application
#'
#' @importFrom shiny uiOutput
#' @export

switch_ui <- function(id) {
  ns <- NS(id)
  uiOutput(ns("switch"))
}


#' Switch module Server
#'
#' @description A switch module UI that can accept dynamic inputs on the server-side
#' declaration
#' @param input list of inputs used in the shiny application session
#' @param output list of outputs used the shiny application session
#' @param session The shiny app session object
#' @param label The label of the \code{switchInput} function. Converted to reactive later for enabling encapsulation
#' @param onLabel The label of the \code{switchInput} function's on position.
#' @param offLabel The label of the \code{switchInput} function's off position.
#' @param value The default value for the \code{switchInput} function.
#' @param on The on value for the \code{switchInput} choices.
#' @param off The off value for the \code{switchInput} choices.
#'
#' @return Numeric or Character. Radio buttons selected value
#'
#' @importFrom shiny renderUI
#' @importFrom shinyWidgets switchInput
#' @export

switch_server <-
  function(input,
           output,
           session,
           label = "",
           onLabel = "Transaction Store",
           offLabel = "Primary Store",
           value = F,
           on = T,
           off = F) {
    ns <- session$ns
    output[["switch"]] <- renderUI(
      shinyWidgets::switchInput(
        ns("switch"),
        label,
        onLabel = onLabel,
        offLabel = offLabel,
        onStatus = on,
        offStatus = off,
        value = value
      )
    )
    return(list(selected = reactive(input$switch)))
  }
HarryRosen/hrimodules documentation built on Jan. 11, 2022, 12:36 a.m.