bslib::card_header 中的闪亮::downloadButton 带有图标而不是文本

问题描述 投票:0回答:1

我希望在我的

downloadButton
的右侧添加一个
bslib::card
。理想情况下,这将是一个图标。到目前为止我所做的是

require(shiny)
require(bslib)
require(ggplot2)

ui <- bslib::page_fillable(
    theme=bslib::bs_theme(version=5),
    title = "Card with Download Icon",
    bslib::card(
        bslib::card_header(
            class = "d-flex justify-content-between",
            "Card Title",
            shiny::downloadButton(
                outputId="download",
                label="",
                icon=shiny::icon("image") # way too large
            )
        ),
        shiny::plotOutput("the_plot")
    )
)

server <- function(input, output, session) {
    plot_obj <- shiny::reactive(
        ggplot2::qplot(Sepal.Length, Sepal.Width, data = iris)
    )
    output$the_plot <- shiny::renderPlot(plot_obj())
    output$download <- shiny::downloadHandler(
        filename = function() "card_snapshot.png",
        content = function(file) {
            ggplot2::ggsave(
                filename=file, plot=plot_obj(), device=png,
                width=16, height=9, units="cm", bg="white"
            )
        }
    )
}

shiny::shinyApp(ui, server)

但这会导致卡头太大,即

我想知道如何创建一个较小的图标以供下载?

我也尝试过

shiny::downloadLink
,但好像没有
icon
的说法...

任何提示表示赞赏!

r shiny bslib
1个回答
1
投票

您正在使用

outputId = "download"
创建一个按钮,因此您可以向
ui
添加一些 css,以通过 ID (
#download
) 选择此元素并更改 padding,例如

tags$head(
        tags$style(
            HTML("
                #download {
                    padding: 0px 5px;
                }")
        )
    )

输出

完整
ui
代码

ui <- bslib::page_fillable(
    tags$head(
        tags$style(
            HTML("
                #download {
                    padding: 0px 5px;
                }")
        )
    ),
    theme = bslib::bs_theme(version = 5),
    title = "Card with Download Icon",
    bslib::card(
        bslib::card_header(
            class = "d-flex justify-content-between",
            "Card Title",
            shiny::downloadButton(
                outputId = "download",
                label = "",
                icon = shiny::icon("image") # way too large
            )
        ),
        shiny::plotOutput("the_plot")
    )
)
© www.soinside.com 2019 - 2024. All rights reserved.