如何在rmarkdown文档中自定义下载handler-button

发布于 2025-01-27 19:25:46 字数 2046 浏览 1 评论 0原文

在Shiny a 中,下载Handleractionbutton/下载button在UI-Part中。因此,您可以轻松地通过键入来更改按钮标签:

downloadButton("btn_export", "Export")

这里有一点ShinyApp,只需一个按钮即可导出一些示例数据:

library(shiny)

ui <- fluidPage(
    downloadButton("btn_export", "Export")
)

server <- function(input, output) {
    data <- mtcars
    
    output$btn_export <- downloadHandler(
        filename = function() {
            paste0("shiny_", Sys.Date(), ".csv")
        },
        content = function(file) {
            write.csv(data, file)
        }
    )
}

shinyApp(ui, server)

在rmarkDown中,下载handler直接添加到代码块:

```{r, echo=FALSE}
downloadHandler(
  filename = function() {
    paste0("rmd_", Sys.Date(), ".csv")
  },
  content = function(file) {
    write.csv(data, file)
  }
)
``` 

我如何更改下载按钮的标签?

一个完整的示例:

---
title: "test"
runtime: shiny
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r data_prep, include=FALSE}
data <- mtcars
```

```{r download, echo=FALSE}
downloadHandler(
  filename = function() {
    paste0("rmd_", Sys.Date(), ".csv")
  },
  content = function(file) {
    write.csv(data, file)
  }
)
```

编辑:

上部可再现的最小示例可能太小了。 下载Handler在我的真实rmarkDown文档中正在渲染静态RMD报告供下载:

downloadHandler(filename = function() {
    return("CaRe.html")
  }, content = function(file) {
     rmarkdown::render(input = "CaRe_static.Rmd", 
                       output_file = file,
                       params = list(in_rohdaten = input$in_rohdaten$datapath,
                                     in_kunde = input$in_kunde,
                                     in_kampagne = input$in_kampagne,
                                     in_auftrag = input$in_auftrag,
                                     in_datum = input$in_datum
     ))
  }, contentType = "text/html"
)

以防该解决方案相关。

In shiny a downloadHandler is combined with an actionButton/downloadButton in the UI-Part. Thus you easily can change the buttons label by typing:

downloadButton("btn_export", "Export")

Here is a little shinyapp with just one button to export some example data:

library(shiny)

ui <- fluidPage(
    downloadButton("btn_export", "Export")
)

server <- function(input, output) {
    data <- mtcars
    
    output$btn_export <- downloadHandler(
        filename = function() {
            paste0("shiny_", Sys.Date(), ".csv")
        },
        content = function(file) {
            write.csv(data, file)
        }
    )
}

shinyApp(ui, server)

In Rmarkdown the downloadHandler is added directly to a code chunk:

```{r, echo=FALSE}
downloadHandler(
  filename = function() {
    paste0("rmd_", Sys.Date(), ".csv")
  },
  content = function(file) {
    write.csv(data, file)
  }
)
``` 

How can I change the label of the download button?

A complete example:

---
title: "test"
runtime: shiny
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r data_prep, include=FALSE}
data <- mtcars
```

```{r download, echo=FALSE}
downloadHandler(
  filename = function() {
    paste0("rmd_", Sys.Date(), ".csv")
  },
  content = function(file) {
    write.csv(data, file)
  }
)
```

Edit:

The upper reproducible minimal example might be too minimal. The downloadHandler in my real Rmarkdown document is rendering a static Rmd-report for download:

downloadHandler(filename = function() {
    return("CaRe.html")
  }, content = function(file) {
     rmarkdown::render(input = "CaRe_static.Rmd", 
                       output_file = file,
                       params = list(in_rohdaten = input$in_rohdaten$datapath,
                                     in_kunde = input$in_kunde,
                                     in_kampagne = input$in_kampagne,
                                     in_auftrag = input$in_auftrag,
                                     in_datum = input$in_datum
     ))
  }, contentType = "text/html"
)

Just in case that is relevant for the solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寻找我们的幸福 2025-02-03 19:25:46

grh,如果您仔细阅读手册,那就太简单了。对不起,浪费了时间。

您可以使用outputargs 的参数下载handler来更改下载按钮的标签:

---
title: "test"
runtime: shiny
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r data_prep, include=FALSE}
data <- mtcars
```

```{r download, echo=FALSE}
downloadHandler(
  filename = function() {
    paste0("rmd_", Sys.Date(), ".csv")
  },
  content = function(file) {
    write.csv(data, file)
  },
  outputArgs = list(label = "Export")  # <--- That's it!
)
```

Argh, it's so simple if you carefully read the manual. Sorry for wasting your time.

You can use the outputArgs parameter of downloadHandler to change the label of the download-button:

---
title: "test"
runtime: shiny
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r data_prep, include=FALSE}
data <- mtcars
```

```{r download, echo=FALSE}
downloadHandler(
  filename = function() {
    paste0("rmd_", Sys.Date(), ".csv")
  },
  content = function(file) {
    write.csv(data, file)
  },
  outputArgs = list(label = "Export")  # <--- That's it!
)
```
清欢 2025-02-03 19:25:46

不确定如何使用downloadhandler()进行操作,但是您可以尝试下载软件包。

---
title: "test"
runtime: shiny
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r, include=FALSE}
library(downloadthis)

data <- mtcars
```

```{r, echo=FALSE}
download_this(mtcars,
  output_name = "mtcars data set",
  output_extension = ".csv",
  button_label = "Download mtcars",
  button_type = "warning",
  has_icon = TRUE,
  icon = "fa fa-save"
)

Not sure how to do it using downloadHandler() but you can try downloadthis package.

---
title: "test"
runtime: shiny
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r, include=FALSE}
library(downloadthis)

data <- mtcars
```

```{r, echo=FALSE}
download_this(mtcars,
  output_name = "mtcars data set",
  output_extension = ".csv",
  button_label = "Download mtcars",
  button_type = "warning",
  has_icon = TRUE,
  icon = "fa fa-save"
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文