John Smith John Smith - 2 months ago 45
R Question

Shiny Download an excel file

I have a

shiny
server set up on a Linux instance
I have a folder called templates which has a excel file template
The user goes to the web page and presses a download button and in theory they should be able to download the template wherever they like on their local machine.

I have seen the code from the post Shiny download file not working

When i try and run it i get a file saved to
my downloads
on my windows laptop and it is called NA not the name Template.xlsx

My two questions are


  • Is it possible to prompt the user where to save the file locally

  • Is it possible to make it default to an
    XLSX



The pseudo code is

ui <- shinyUI(fluidPage(

# Side Panel with Options
fluidRow(
column(4, wellPanel(
id = "leftPanel",
div(
id = "Header",
h3("Options"),
tags$hr()
),
div(
h4("1. Download the empty excel template"),
downloadButton("downloadBtn", "Download Excel Template")
)
)))))


Server



# Define server logic required
server <- shinyServer(
function(input, output) {

output$downloadBtn <- downloadHandler(
filename = function() {
paste(input$filenames, sep='')
},
content = function(file) {
myfile <- srcpath <- '/home/foo/Save to Database/templates/Template.xlsm'
file.copy(myfile, file)
}
)})

GyD GyD
Answer

You are using paste(input$filenames, sep='') but you don't have input$filenames defined in the UI part. It should work if you replace that line with "Template.xlsm"

Comments