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

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)

但是这导致了太大的卡头,即,

screenshot

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

我也试过shiny::downloadLink次,但似乎没有icon次的争论...

任何提示赞赏!

推荐答案

您正在创建一个带有outputId = "download"的按钮,因此您可以在ui中添加一些css,以通过ID(#download) Select 该元素并更改padding.

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

输出

enter image description here

在这种情况下,实际上没有必要将css包装成htmltools::HTML().然而,如果您的样式变得更加复杂,并且开始使用您需要指定的字符不应该被转义,这将是必要的,例如导入字体的示例here.我认为在R代码中编写html或css时,最好总是使用HTML(),这样你就不必考虑是否使用了可能被转义的字符.

Complete ui code

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::plot输出("the_plot")
    )
)

R相关问答推荐

导入到固定列宽的R中时出现问题

有没有方法将琴弦完全捕捉到R中的多边形?

将一个载体的值相加,直到达到另一个载体的值

将Multilinetring合并到一个线串中,使用sf生成规则间隔的点

创建重复删除的唯一数据集组合列表

如何创建构成多个独立列条目列表的收件箱框列?

查找具有平局的多个列的最大值并返回列名或平局 destruct 者NA值

使用R中的Shapetime裁剪格栅文件

terra nearest()仅为所有`to_id`列返回NA

R Highcharts与两个位置关联的注释

如何在emmeans中计算连续变量的对比度

Rplotly中的Sankey Diagram:意外连接&

如何优化向量的以下条件赋值?

R中的时间序列(Ts)函数计数不正确

如何在R中描绘#符号?

在R函数中使用加号

如何将一些单元格的内容随机 Select 到一个数据框中?

如何为混合模型输出绘制不同的线型?

基于R中的辅助向量中的值有条件地连接向量中的字符串

如何将EC50值绘制在R中的剂量-react 曲线上?