【问题标题】:How to add icon to downloadButton in Shiny?如何在 Shiny 中将图标添加到 downloadButton?
【发布时间】:2019-10-17 02:38:07
【问题描述】:

我可以在我的闪亮应用中将自己的图标添加到downloadButtondownloadBttn (shinyWidgets) 吗?

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

【问题讨论】:

    标签: css r button shiny


    【解决方案1】:
    > 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>
    

    这是“下载”按钮背后的功能。只需使用您自己的函数即可。

    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)
    }
    

    只需在icon中插入你想要的图标,然后像普通的下载按钮一样使用功能

    使用方法:

    #

    # 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)
    

    【讨论】:

    • 谢谢,但是以前的标准图标呢。我可以删除它吗? link
    【解决方案2】:

    我可能会建议为此使用shinyjs 包。您可以使用addCssClass() 函数快速将下载按钮上的字体真棒类更改为其他内容。

    这是一个如何使用它的 css 选择器将默认下载图标更改为 pdf 文件图标的示例。;

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

    不要忘记在您的 UI 中包含 useShinyjs()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-19
      • 1970-01-01
      • 2018-05-10
      • 2019-08-04
      • 2021-12-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多