R/app.R

Defines functions app

Documented in app

#' Lancement de l'application Shiny
#'
#' @import shiny
#' @import shinydashboard
#' @importFrom dplyr %>%
#' @export
app <- function() {

  ui <- dashboardPage(
    dashboardHeader(title = paste("Offre de formation", apogee::annee_u(apogee::annee_en_cours())), titleWidth = 270),
    dashboardSidebar(width = 270, sidebarMenu(
      menuItem("Filtres", tabName = "formations", startExpanded = TRUE, icon = icon("filter")),
      column(12, h5(textOutput("n_formations")), offset = 0.5),
      shinyWidgets::selectizeGroupUI(
        "filterset",
        inline = FALSE,
        btn_label = "Supprimer les filtres",
        params = list(
          lib_composante = list(inputId = "lib_composante", title = "Composante"),
          acronyme_type_diplome = list(inputId = "acronyme_type_diplome", title = "Type de diplôme"),
          lib_mention = list(inputId = "lib_mention", title = "Mention"),
          lib_domaine = list(inputId = "lib_domaine", title = "Domaine"),
          annee_etape = list(inputId = "annee_etape", title = "Année de fromation"),
          lib_etape = list(inputId = "lib_etape", title = "Formation")
        )
      ),
      fluidRow(div(style = "height: 30px;", "")),
      menuItem("Téléchargement", tabName = "export", startExpanded = TRUE, icon = icon("download")),
      column(4, downloadButton("csv", "CSV")),
      column(4, downloadButton("excel", "Excel"))
    )),
    dashboardBody(
      tags$head(
        tags$style(HTML(".main-sidebar { font-size: 15px; }")),
        tags$style(HTML("#filter_label { font-size: 14px; }"))
      ),
      tabItems(
        tabItem(tabName = "formations",
                fixedRow(
                  column(width = 12,
                         fluidRow(
                           DT::dataTableOutput("table")
                         )
                  )

                )
        )
      )
    )
  )

  server <- function(input, output, session) {

    cols_name <- names(formation.offre::formations) %>%
      dplyr::recode("lib_composante" = "Composante", "acronyme_type_diplome" = "Type diplôme", "lib_mention" = "Mention", "annee_etape" = "Année formation", "lib_etape" = "Libellé formation", "lib_domaine" = "Domaine")

    filtered_data <- callModule(
      module = shinyWidgets::selectizeGroupServer,
      id = "filterset",
      data = formation.offre::formations,
      vars = c("lib_composante", "acronyme_type_diplome", "lib_mention", "lib_domaine", "annee_etape", "lib_etape")
    )

    filtered_data2 <- reactive({
      formation.offre::formations_unique %>%
        dplyr::semi_join(filtered_data(), by = "code_etape")
    })

    output$n_formations <- reactive({
      n_formations <- length(unique(filtered_data2()$code_etape))
      if (n_formations == nrow(formation.offre::formations_unique)) {
        paste(nrow(formation.offre::formations_unique), "formations au total")
      } else if (n_formations == 1) {
        glue::glue("{n_formations} formation filtrée")
      } else {
        glue::glue("{n_formations} formations filtrées")
      }
    })


    output$table <- DT::renderDataTable({

      table <- filtered_data2() %>%
        DT::datatable(selection = list(mode = 'single', selected = 1),
                      colnames = c('Composante' = 'lib_composante', 'Type diplôme' = 'acronyme_type_diplome', 'Année formation' = 'annee_etape', 'Code étape' = 'code_etape', 'Libellé formation' = 'lib_etape', 'Acronyme' = 'acronyme_etape', 'Mention' = 'lib_mention', 'Domaine' = 'lib_domaine'), #'Option' = 'option', 'Particularité' = 'particularite', 'Ville' = 'ville'
                      rownames = FALSE,
                      extensions = 'Buttons',
                      options = list(
                        pageLength = -1,
                        dom = 'rt',
                        scrollY = '85vh',
                        autoWidth = TRUE
                      )
        )

      table

    })

    output$csv <- downloadHandler(
      filename = function() {
        "formations.csv"
      },
      content = function(con) {
        write.csv2(filtered_data(), con, row.names = FALSE, na = "")
      }
    )

    output$excel <- downloadHandler(
      filename = function() {
        "formations.xlsx"
      },
      content = function(con) {
        writexl::write_xlsx(filtered_data(), con)
      }
    )

  }


  shinyApp(ui = ui, server = server)

}
ove-ut3/formation.offre documentation built on March 8, 2020, 6:46 p.m.