R/mod_import_data.R

Defines functions mod_import_data_server mod_import_data_ui

Documented in mod_import_data_server mod_import_data_ui

# Module UI
  
#' @title   mod_import_data_ui and mod_import_data_server
#' @description  A shiny Module.
#'
#' @param id shiny id
#' @param input internal
#' @param output internal
#' @param session internal
#'
#' @rdname mod_import_data
#'
#' @keywords internal
#' @export 
#' @importFrom shiny NS tagList 
#' @include mod_import_data_format.R
mod_import_data_ui <- function(id){
  ns <- NS(id)
  modalDialog(title = "Import Data",
              size = "l", easyClose = TRUE,
              
              sidebarLayout(
                
                # Sidebar panel for inputs ----
                sidebarPanel(
                  
                  # Input: Select a file ----
                  fileInput(ns("files"), "Choose CSV Files",
                            multiple = TRUE,
                            accept = c("text/csv",
                                       "text/comma-separated-values,text/plain",
                                       ".csv"))
                  
                ),
                
                # Main panel for displaying outputs ----
                mainPanel(
                  tableOutput(ns("files-summary"))
                )
              ), 
              
              uiOutput(ns("wrapper-tabset")), 
              
              footer = tagList(
                modalButton("Annuler"),
                actionButton(ns("next"), "Suivant", class = "btn btn-primary", icon = icon("arrow-circle-right"), style="color: white"), #bug sur la couleur de la font
                shinyjs::hidden(actionButton(ns("ok"), "Valider", class = "btn btn-success", icon = icon("check"), style="color: white")) #bug sur la couleur de la font
              )
  )
}




# Module Server

#' @rdname mod_import_data
#' @export
#' @keywords internal

mod_import_data_server <- function(input, output, session){
  ns <- session$ns
  
  # Data imported by the user
  data <- list()

  # Display files summary (sidebar of the modal)
  output[["files-summary"]] <- renderTable({
    req(input$files)
    input$files[, c("name", "size")]
  })
  
  # Affiche un tabSetPanel avec un nombre d'onglet (tabPanel) dynamique, qui dépend du nombre de fichiers sélectionnés
  output[["wrapper-tabset"]] <- renderUI({
    req(input$files)
    
    nb_files <- nrow(input$files)
    # Data imported by the user
    data <<- list()
    
    # liste des onglets
    panels <- lapply(1:nb_files, function(id_file){
      name <- input$files$name[id_file]
      
      datapath <- input$files$datapath[id_file]
      
      # Module de formattage des fichiers importés
      data[[id_file]] <<- callModule(module = mod_import_data_format_server, id = id_file, datapath, id_file)
      
      tabPanel(title = name,
               value = id_file,
               tags$div(style = "margin-top:15px", mod_import_data_format_ui(ns(id_file), name = name)))
    })
    panels[["id"]] <- ns("tabset")
    
    do.call(tabsetPanel, panels)
    
  })
  
  
  # Triggered when we validate current panel
  # ----------------------------------------
  observeEvent(input[["next"]], {
    current_panel <- as.numeric(input[["tabset"]])
    
    updateTabsetPanel(session, "tabset",
                      selected = as.character(current_panel + 1))
  })
  
  
  # Triggered when we change panel
  # ------------------------------
  observeEvent(input[["tabset"]], {
    if (input[["tabset"]] < nrow(input$files)){
      shinyjs::show(id = "next")
      shinyjs::hide(id = "ok")
    } else {
      shinyjs::hide(id = "next")
      shinyjs::show(id = "ok")
    }
  })
  
  return(list("data" = reactive({ input[["ok"]] # trigger
    data }), "trigger"=reactive({ input[["ok"]] })))
}
MehdiChelh/triangle.tlbx documentation built on May 18, 2020, 3:14 a.m.