0

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
bad_coder
  • 11,289
  • 20
  • 44
  • 72
J.Sabree
  • 2,280
  • 19
  • 48

1 Answers1

0

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)
YBS
  • 19,324
  • 2
  • 9
  • 27
  • sorry, I'm new to Shiny. What do you mean by the www folder? I don't have a folder called www. – J.Sabree Jul 22 '21 at 01:57
  • Then you should create folder name `www` in the same location where you keep your app.R program. You keep the images, custom.CSS file, script.js, etc., in that folder so that your app can access those files. – YBS Jul 22 '21 at 02:25