R/module-utils.R

Defines functions cut_var_server cut_var_ui create_col_server create_col_ui update_vars_server update_vars_ui show_data_server show_data_ui

# Show data -------------------------------------------------------------------------

#' @importFrom shiny NS
#' @importFrom htmltools tags css
#' @importFrom phosphoricons ph
show_data_ui <- function(id) {
  ns <- NS(id)
  icon <- tags$div(
    style = css(position = "relative", width = "35px"),
    ph("table", height = "2em", title = i18n("Show data")),
    ph(
      "eye",
      style = css(position = "absolute", top = 0, right = 0),
      height = "1.2em",
      weight = "bold",
      title = i18n("Show data")
    )
  )
  btn_header(i18n("Show data"), icon, class = " px-0")(ns("btn"))
}

#' @importFrom shiny moduleServer observeEvent showNotification reactive
#' @importFrom datamods show_data
show_data_server <- function(id, data_r = reactive(NULL)) {
  moduleServer(
    id,
    function(input, output, session) {

      observeEvent(input$btn, {
        data <- data_r()
        if (!is.data.frame(data)) {
          showNotification(
            ui = "No data to display",
            duration = 700,
            id = paste("esquisse", sample.int(1e6, 1), sep = "-"),
            type = "warning"
          )
        } else {
          show_data(data, title = i18n("Dataset"), type = "modal")
        }
      })

    }
  )
}




# Update vars -----------------------------------------------------------------------

#' @importFrom shiny NS
#' @importFrom htmltools tags css
#' @importFrom phosphoricons ph
update_vars_ui <- function(id) {
  ns <- NS(id)
  icon <- tags$div(
    style = css(position = "relative", width = "35px"),
    ph("table", height = "2em", title = i18n("Update variables")),
    ph(
      "gear",
      style = css(position = "absolute", top = 0, right = 0),
      height = "1.2em",
      weight = "bold",
      title = i18n("Update variables")
    )
  )
  btn_header(i18n("Update variables"), icon, class = " px-0")(ns("btn"))
}

#' @importFrom shiny moduleServer observeEvent modalDialog showModal reactive
#' @importFrom datamods update_variables_ui update_variables_server
update_vars_server <- function(id, data_r = reactive(NULL)) {
  moduleServer(
    id,
    function(input, output, session) {
      ns <- session$ns
      observeEvent(input$btn, {
        showModal(modalDialog(
          title = tagList(
            i18n("Update & select variables"),
            button_close_modal()
          ),
          datamods::update_variables_ui(ns("mod"), title = NULL),
          easyClose = TRUE,
          size = "l",
          footer = NULL
        ))
      })
      res <- datamods::update_variables_server(
        id = "mod",
        data = data_r
      )
      return(res)
    }
  )
}





# Create column ---------------------------------------------------------------------

#' @importFrom shiny NS
create_col_ui <- function(id) {
  ns <- NS(id)
  btn_header(i18n("Create column"), "columns-plus-right")(ns("btn"))
}

#' @importFrom shiny moduleServer observeEvent modalDialog showModal reactive
#' @importFrom datamods update_variables_ui update_variables_server
create_col_server <- function(id, data_r = reactive(NULL)) {
  moduleServer(
    id,
    function(input, output, session) {
      ns <- session$ns
      observeEvent(input$btn, datamods::modal_create_column(ns("mod")))
      res <- datamods::create_column_server(
        id = "mod",
        data = data_r
      )
      return(res)
    }
  )
}





# Cut variable ------------------------------------------------------------

#' @importFrom shiny NS
#' @importFrom htmltools tags css
#' @importFrom phosphoricons ph
cut_var_ui <- function(id) {
  ns <- NS(id)
  icon <- tags$div(
    style = css(position = "relative", width = "35px"),
    ph("list-numbers", height = "2em", title = i18n("Cut numeric variable into factors")),
    ph(
      "scissors",
      style = css(position = "absolute", top = 0, right = 0, transform = "scale(-1, 1)"),
      height = "1.2em",
      weight = "bold",
      title = i18n("Cut numeric variable into factors")
    )
  )
  btn_header(i18n("Cut numeric variable into factors"), class = " px-0", icon)(ns("btn"))
}

#' @importFrom shiny moduleServer observeEvent modalDialog showModal reactive
#' @importFrom datamods cut_variable_ui cut_variable_server
cut_var_server <- function(id, data_r = reactive(NULL)) {
  moduleServer(
    id,
    function(input, output, session) {
      ns <- session$ns
      observeEvent(input$btn, datamods::modal_cut_variable(ns("mod")))
      observeEvent(res(), shiny::removeModal())
      res <- datamods::cut_variable_server(
        id = "mod",
        data = data_r
      )
      return(res)
    }
  )
}
dreamRs/esquisse documentation built on April 28, 2024, 4:07 p.m.