如何在bslib::card_header中添加小图标版shiny::downloadButton?
解决bslib卡片头部下载图标过大的问题
可以通过以下几种方法调整图标尺寸,避免卡片头部高度异常:
方案1:直接调整downloadButton的图标与按钮样式
给downloadButton添加内联样式控制图标大小,同时通过align-items-center让标题与按钮垂直居中:
bslib::card_header( class = "d-flex justify-content-between align-items-center", "Card Title", shiny::downloadButton( outputId="download", label="", icon=shiny::icon("image"), style = "font-size: 1.2rem; padding: 0.25rem 0.5rem;" ) )
方案2:用downloadLink配合自定义按钮样式
downloadLink虽无直接的icon参数,但可以嵌套图标标签,并通过bootstrap按钮类控制尺寸:
bslib::card_header( class = "d-flex justify-content-between align-items-center", "Card Title", shiny::tags$a( id = "download", class = "btn btn-outline-primary btn-sm", shiny::icon("image", style = "font-size: 1rem;"), href = "", target = "_blank", download = NA ) )
方案3:使用bslib自带的download_button(最适配)
bslib的download_button支持size参数直接控制按钮与图标尺寸,完美适配bslib主题:
bslib::card_header( class = "d-flex justify-content-between align-items-center", "Card Title", bslib::download_button( outputId="download", label="", icon=shiny::icon("image"), size = "sm" ) )
完整示例代码(方案3)
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 align-items-center", "Card Title", bslib::download_button( outputId="download", label="", icon=shiny::icon("image"), size = "sm" ) ), 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)
内容的提问来源于stack exchange,提问作者Karsten W.
相关产品推荐
相关产品推荐

