R/mod_import_data_format.R

Defines functions mod_import_data_format_server mod_import_data_format_ui

Documented in mod_import_data_format_server mod_import_data_format_ui

# Module UI
  
#' @title   mod_import_data_format_ui and mod_import_data_format_server
#' @description  A shiny Module.
#'
#' @param id shiny id
#' @param input internal
#' @param output internal
#' @param session internal
#'
#' @rdname mod_import_data_format
#'
#' @keywords internal
#' @export 
#' @importFrom shiny NS tagList 
mod_import_data_format_ui <- function(id, name = NULL){
  ns <- NS(id)
  
  tagList(
    sidebarLayout(
      
      # Sidebar panel for inputs ----
      sidebarPanel(
        
        # Input: Name of the data used in the application
        textInput(ns("name"), "Nom", value = name),
        
        # Input: Type de données charges D/D ou règlements
        selectInput(ns("type"), "Type de données", list("Triangle de règlement"="triangle-reglement",
                                                        "Triangle de charges D/D"="triangle-charge",
                                                        "Réserves D/D (vecteur colonne)"="reserve-charge",
                                                        "Réserves (autres)"="reserve-autres")),
        
        # Input: Checkbox if file has header ----
        checkboxInput(ns("header"), "Header", TRUE),
        
        # Input: Checkbox if file has rownames ----
        checkboxInput(ns("rownames"), "Rownames", TRUE),
        
        # Input: Select separator ----
        radioButtons(ns("sep"), "Separator",
                     choices = c(Comma = ",",
                                 Semicolon = ";",
                                 Tab = "\t"),
                     selected = ","),
        
        # Input: Select quotes ----
        radioButtons(ns("decimal"), "Decimal",
                     choices = c("." = ".",
                                 "," = ","),
                     selected = '.'),
        
        # Input: Select quotes ----
        radioButtons(ns("quote"), "Quote",
                     choices = c(None = "",
                                 "Double Quote" = '"',
                                 "Single Quote" = "'"),
                     selected = '"'),
        
        # Horizontal line ----
        tags$hr(),
        
        # Input: Select number of rows to display ----
        radioButtons(ns("disp"), "Display",
                     choices = c(Head = "head",
                                 All = "all"),
                     selected = "all")
        
      ),
      
      # Main panel for displaying outputs ----
      mainPanel(
        uiOutput(ns("warning-message")),
        # Output: Data file ----
        tags$div(tableOutput(ns("contents")),
                 style = "overflow: auto"), style = "padding-right:15px"
        
      )
      
    )
  )
}
    
# Module Server
    
#' @rdname mod_import_data_format
#' @export
#' @keywords internal
    
mod_import_data_format_server <- function(input, output, session, datapath, id_file){
  ns <- session$ns
  
  # Data imported by the user
  data <- reactive({
    
    # If file openning didn't went well, data() returns NULL
    tryCatch({
      
      # If has rownames, put first colum as rownames
      if (input$rownames){
        df <- as.matrix(read.csv(datapath,
                                 header = input$header,
                                 sep = input$sep,
                                 dec = input$decimal,
                                 quote = input$quote, 
                                 row.names = 1))
      } else {
        df <- as.matrix(read.csv(datapath,
                                 header = input$header,
                                 sep = input$sep,
                                 dec = input$decimal,
                                 quote = input$quote))
      }
      
      # Check que les données sont bien de type numérique
      if(is.numeric(df))
        return(df)
      else
        return(NULL)
      
    }, warning = function(w) {
      return(NULL)
    }, error = function(e) {
      return(NULL)
    })
    
  })
  
  # Warning message
  output[["warning-message"]] <- renderUI({
    if (is.null(data()))
      tags$div(class = "alert alert-danger", "Erreur dans l'import des données.")
    else if (!is.numeric(data()))
      tags$div(class = "alert alert-danger", "Erreur dans l'import des données : le type de données reconnu n'est pas 'numeric'.")
    else
      tags$div()
  })
  
  # Render data
  #   - The modal contains a table which help the user to 
  #     know if the data is correctly imported.
  output[["contents"]] <- renderTable({
    if(input$disp == "head") {
      return(head(data()))
    }
    else {
      return(data())
    }
  })
  
  
  return(list("id" = id_file, 
              "name" = reactive({ input[["name"]] }), 
              "type" = reactive({ input[["type"]] }), 
              "data" = data, "datapath" = datapath))
}



    
## To be copied in the UI
# mod_import_data_format_ui("import_data_format_ui_1")
    
## To be copied in the server
# callModule(mod_import_data_format_server, "import_data_format_ui_1")
 
MehdiChelh/triangle.tlbx documentation built on May 18, 2020, 3:14 a.m.