'How to have users download pre-loaded, formatted excel document in R Shiny?

I'm building a Shiny App where users complete a survey, and based on their responses, it suggests different templates for them to use. The templates are all excel files that are heavily formatted (e.g., have pictures on them, misaligned headings, etc.), like the screenshot that I've uploaded here. Unfortunately, stackoverflow won't let me upload the excel file to make this fully reproducible, but if you can run it with any non-tabular excel file, it'll work.[![enter image description here][1]][1]

These templates are all uploaded to the server, and the users input does not affect them. I've tried following the example by others, like this [one][2], but I keep getting errors.

How do I get it so when users click the download button, they get the excel file exactly as it appears?

library(readxl)
library(shiny)
library(writexl)


ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
  ),
    
    mainPanel(
      downloadButton("downloadData", "Download Fancy Excel File")
        )))


server <- function(input, output) {

  
  output$downloadData <- downloadHandler(
    filename = function() {
      paste("file", "xlsx", sep='')
    },
    content = function(file) {
      myfile <- srcpath <- 'Home/Other Layer/Fancy Template.xlsx'
      file.copy(myfile, file)
    }
  )
  }

# Run the application 
shinyApp(ui = ui, server = server)
~~~~


  [1]: https://i.stack.imgur.com/FK034.png
  [2]: https://stackoverflow.com/questions/39449544/shiny-download-an-excel-file


Solution 1:[1]

You are missing a . in the file name. Also, you can keep all the files you want the users to download in www folder. The following works for me.

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
    ),
    
    mainPanel(
      downloadButton("downloadData", "Download Fancy Excel File") 
    )))


server <- function(input, output) {
  
  output$downloadData <- downloadHandler(
    filename = function() {
      paste("testfile", ".xlsx", sep='')
    },
    content = function(file) {
      # myfile <- srcpath <- 'Home/Other Layer/Fancy Template.xlsx'
      myfile <- srcpath <-  "./www/test141.xlsx"
      file.copy(myfile, file)
    }
  )
}

# Run the application 
shinyApp(ui = ui, server = server)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 YBS