Reputation: 10954
I have a static PDF file in my www
folder, that I would like to link to a UI downloadButton()
. It seems that the server-side downloadHandler
needs the content
option to be populated, and is meant for reactively produced output.
I know that I can link to static content using HTML tags, tags$a('Download file.', href = 'foo.pdf')
on the UI side.
Any suggestions as to how to put the two together will be helpful.
Upvotes: 10
Views: 4339
Reputation: 191
May be this will help:
content = function(file) {
file.copy('www/foo.pdf', file)
}
Upvotes: 9
Reputation: 17089
In the context of ui.R
and server.R
:
ui.R
downloadButton(
"statFile",
"Download static file"
)
server.R
output$statFile <- downloadHandler(
filename="example.txt", # desired file name on client
content=function(con) {
file.copy("file_name_on_server", con)
}
)
Upvotes: 3
Reputation: 30425
I am guessing you just want a button to show up? You can add class = 'btn'
to your anchor.
Here is an example showing with class = 'btn'
and without.
library(shiny)
runApp(list(
ui = bootstrapPage(
numericInput('n', 'Number of obs', 100),
plotOutput('plot'),
tags$a(href = 'foo.pdf', class = "btn", icon("download"), 'Download file.'),
tags$a('Download file2.', href = 'foo2.pdf'),
downloadButton('downloadData', 'Download')
),
server = function(input, output) {
output$plot <- renderPlot({ hist(runif(input$n)) })
output$downloadData <- downloadHandler(
filename = function() {
paste('data-', Sys.Date(), '.csv', sep='')
},
content = function(con) {
write.csv(data, con)
}
)
}
))
To add an icon like the shiny
function downloadHandler
has refer to the fontawesome library. For example
here is the icon the shiny function uses http://fontawesome.io/icon/download/. Shiny has a wrapper function icon
to include these icons.
Upvotes: 8