Download and Email reports in R Shiny app

Most of the dashboards / R shiny app are viewed by the people who want to view the underyling data without doing much analysis. While they view the KPI’s , the charts and the tables it would be also an important for them to take the results out of the app for meetings and presenations. One of way doing it is the traditionally downloading the reports. But to make it one step easier they could also be given an option to email these reports.Thats one way of making most out of such tools.


The libraries which we will be using are shiny for the app itself, shinyFiles and fs for selecting the files to be mailed and shinyAce and mailR for the email component of the app.


The app layout has a sidebar and a main panel. The sidebar has the download and the email functionality and the main panel has the plot . The user can donload the the report in mutiple formats.


Download Reports

Below is the code for downloading report. The user can download the reports in multiple formats. You could also write into powerpoints which can taken into weekly meetings.

output$downloadReport <- downloadHandler(
  filename = function() {
    paste('my-report', sep = '.', switch(
      input$format, PDF = 'pdf', HTML = 'html', Word = 'docx'
  content = function(file) {
    src <- normalizePath('report.Rmd')

    # temporarily switch to the temp dir, in case you do not have write
    # permission to the current working directory
    owd <- setwd(tempdir())
    file.copy(src, 'report.Rmd', overwrite = TRUE)

    out <- render('report.Rmd', switch(
      PDF = pdf_document(), HTML = html_document(), Word = word_document()
    file.rename(out, file)

Email component

The following code handles how to email from the app itself.Once the user downloads the report to their local drive, they can select any files they want to mail to using shinyFiles

volumes <- c(Home = fs::path_home(), "R Installation" = R.home(), getVolumes()())
shinyFileChoose(input, "file", roots = volumes, session = session)

When the user clicks on the send button it takes the inputs such as to, the body of the mail and the file to be mailed which was selected by the user.In this app the email is configured via gmail but this can be done via Outlook as well.

  if(is.null(input$send) || input$send==0) return(NULL)

  subject <- isolate(input$subject)
  msg <- isolate(input$message)
  sender <- ""
  recipients <- isolate(input$to)
  path <- parseFilePaths(volumes, input$file)
  send.mail(from = sender,
            to = recipients,
            subject = subject,
            body = msg,
            #attach.files = path$datapath,#file.path(folder, fileName),
            attach.files = path$datapath,
            smtp = list( = "", port = 465,
              , passwd="*********", ssl=TRUE),
            authenticate = TRUE,
            send = TRUE)



You can find code for the app here.