Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: Shiny downloadButton for static content

Tags:

r

shiny

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.

like image 660
tchakravarty Avatar asked May 12 '14 21:05

tchakravarty


3 Answers

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.

pic showing btn with icon

like image 121
jdharrison Avatar answered Oct 27 '22 16:10

jdharrison


May be this will help:

content = function(file) {
  file.copy('www/foo.pdf', file)
 }
like image 41
Yang Avatar answered Oct 27 '22 17:10

Yang


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)
    }
  )
like image 2
Megatron Avatar answered Oct 27 '22 17:10

Megatron