R/action.R

Defines functions action_server action_ui

Documented in action_server action_ui

#' Action button module UI
#'
#' @description An action button module UI that has the flexibility to
#' be enabled and disabled based on actions that javascript will listen for
#'
#' @param id is unique ID associated with the button for the UI namespace
#' @param label a character describing the action button's use or purposes
#'
#' @return HTML UI code for a shiny application
#'
#' @importFrom shiny actionButton
#' @importFrom shiny NS
#' @export

action_ui <- function(id, label = 'Insert') {
  ns <- NS(id)
  actionButton(ns('btn'), label)
}

#' Action button module Server
#'
#' @description An action button module server code  that has the flexibility to
#' be enabled and disabled based on actions that javascript will listen for
#'
#' @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 trig A reactive function.  The reactive function should result in a logical value to trigger the enable or disable action
#' @param disable Logical. A logical value that enables the trigger actions
#'
#' @return Numeric. Action button value
#'
#' @importFrom shinyjs enable disable
#' @importFrom shiny observeEvent
#' @importFrom shiny reactive
#' @export

action_server <-
  function(input, output, session, trig, disable = F) {

    #Check if there is a trigger
    if (!missing("trig"))
      observeEvent(trig(), ignoreInit = TRUE, {
        #If a trigger exists, determine if te trigger has been toggled and whether or not the trigger
        #can disable the action button
        if (disable && trig())
          disable(id = "btn")
        if(disable && !trig())
          enable(id = "btn")
      })

    #Return the action button
    return(list(selected = reactive(input$btn)))
  }
HarryRosen/hrimodules documentation built on Jan. 11, 2022, 12:36 a.m.