Bogaso
Bogaso

Reputation: 3308

Adding/removing icon in downloadButton() and fileInput()

In Shiny, we have downloadButton() and fileInput() buttons for downloading and uploading data respectively.

However it happens that, with downloadButton() there is download icon, and however with fileInput() no icon is attached.

In my Shiny app, I have both buttons. However since one of them has icon attached, it brings some kind of visual inconsistency in my App.

So, I either want to remove such icon from downloadButton(), or add some upload button with fileInput() to bring consistency.

However, it appears that there is not any direct approach to perform either of them.

So can anybody suggest here if there is any way to :

Either remove icon from downloadButton() Or, attach some upload icon with fileInput()

Any pointer is highly appreciated.

Thanks,

Upvotes: 4

Views: 2532

Answers (2)

Jason Dealey
Jason Dealey

Reputation: 310

To add an icon to fileInput(), add a list to the buttonLabel. e.g.

shinyApp(
  fluidPage(
    fileInput("myFileInput",label="Test",buttonLabel=list(icon("folder"),"TestyMcTestFace"))
  ),
  function(input, output, session){
  }
)

Upvotes: 8

Gregor de Cillia
Gregor de Cillia

Reputation: 7645

If you look at the source code of downloadButton, you will see that changing/removing the button is pretty straightforward

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, ...)
## }
## <environment: namespace:shiny>

You just need to replace icon("download") with NULL. Here is a complete example

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

shinyApp(
  fluidPage(myDownloadButton("download")),
  function(input, output, session){
    output$download = downloadHandler(
      "file.rds", function(file){ saveRDS(mtcars, file) }
    )
  }
)

Upvotes: 4

Related Questions