R/save_data_tables.R

Defines functions save_datatable_server save_datatable_ui

Documented in save_datatable_server save_datatable_ui

#' save_datatable_ui
#'
#' @export
save_datatable_ui <- function(id){
  ns <- shiny::NS(id)
  fluidPage(
    fluidRow(column(width = 3, actionButton(inputId = ns("export_datatable"),label = "Export", icon = icon("download"))))
  )
}

#' save_datatable_server
#' @export
save_datatable_server <-  function(id, data_table = NULL,export_name = ""){
  moduleServer( id, function(input, output,session) {

    ns <- session$ns

    observeEvent(input$export_datatable,{
      showModal(modalDialog(title = paste("Export Data table"), easyClose  = FALSE, size  = "m", footer =  tagList(modalButton("Dismiss"),
                                                                                                    downloadButton(ns("downlaod_datatable"), "Download Data")
                                    ),
                            fluidRow(
                              column(width = 3, textInput(inputId = ns("datatable_doc_name"), label = "Title",value = export_name)),
                              column(width = 4, radioButtons(inputId = ns("export_type"), label = "Type", choices = c("csv","xlsx","html"), inline = TRUE, selected = "csv")),
                              )
                            )
      )
    })
    output$downlaod_datatable <- renderUI({
      req(input$datatable_doc_name)
    })

    output$downlaod_datatable <- downloadHandler(
      filename = function() {
        datatable_doc_name <- ifelse(input$datatable_doc_name == "","thaink2_datatable",input$datatable_doc_name)
        paste(datatable_doc_name,"_", Sys.Date(), ".", input$export_type, sep="")
      },
      content = function(file) {
        if(input$export_type == "csv")readr::write_csv(data_table(), file)
        if(input$export_type =="xlsx")writexl::write_xlsx(data_table(), file)
        rmd_file <- system.file("helpers/datatable_exporter_pdf.Rmd", package  = "SaldaeModulesUI")
        dt_params <- list(dt_title = input$datatable_doc_name, dt = data_table())
        if(input$export_type =="html")rmarkdown::render(rmd_file, params = dt_params, output_file = file)
      }
    )

  })
}
Aqvayli06/SaldaeModulesUI documentation built on Feb. 4, 2024, 6:25 a.m.