Augusto Chile
Augusto Chile

Reputation: 59

How to add icon to downloadButton in Shiny?

May I add my own icon to downloadButton or downloadBttn (shinyWidgets) in my shiny app?

downloadBttn("downloadDataxlsx",
      label = "Download .xlsx", 
      style = "stretch",
      color = "primary",
      size = "md"))

Upvotes: 0

Views: 1986

Answers (2)

fullera
fullera

Reputation: 204

I might suggest using the shinyjs package for this. You could quickly change the font awesome class on the download button to something else by using the addCssClass() function.

Here is an example of how to change the default download icon to a pdf file icon using it's css selector.;

shinyjs::addCssClass(selector = "i.fa.fa-download", class = 'fa fa-file-pdf')

Don't forget to include useShinyjs() in your UI.

Upvotes: 0

DSGym
DSGym

Reputation: 2867

> downloadButton
function (outputId, label = "Download", class = NULL, ...) 
{
    aTag <- tags$a(id = outputId, class = paste("btn btn-default shiny-download-link", 
        class), href = "", target = "_blank", download = NA, 
        icon("download"), label, ...)
}
<bytecode: 0x000000001a919c58>
<environment: namespace:shiny>

This is the function behind the Download button. Just use your own function.

customDownloadbutton <- function(outputId, label = "Download"){
    tags$a(id = outputId, class = "btn btn-default shiny-download-link", href = "", 
           target = "_blank", download = NA, icon("accessible-icon"), label)
}

Just insert in icon the icon you want and use the function like the normal downloadbutton

How to use it:

#

# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
#    http://shiny.rstudio.com/
#

customDownloadbutton <- function(outputId, label = "Download"){
    tags$a(id = outputId, class = "btn btn-default shiny-download-link", href = "", 
           target = "_blank", download = NA, icon("accessible-icon"), label)
}

library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(

   # Application title
   titlePanel("Old Faithful Geyser Data"),

   # Sidebar with a slider input for number of bins 
   sidebarLayout(
      sidebarPanel(
         sliderInput("bins",
                     "Number of bins:",
                     min = 1,
                     max = 50,
                     value = 30),
         customDownloadbutton("myDownloadButton")
      ),

      # Show a plot of the generated distribution
      mainPanel(
         plotOutput("distPlot")
      )
   )
)

# Define server logic required to draw a histogram
server <- function(input, output) {

   output$distPlot <- renderPlot({
      # generate bins based on input$bins from ui.R
      x    <- faithful[, 2] 
      bins <- seq(min(x), max(x), length.out = input$bins + 1)

      # draw the histogram with the specified number of bins
      hist(x, breaks = bins, col = 'darkgray', border = 'white')
   })
}

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

Upvotes: 3

Related Questions