如何在rmarkdown文档中自定义下载handler-button
在Shiny a 中,下载Handler
与actionbutton
/下载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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
grh,如果您仔细阅读手册,那就太简单了。对不起,浪费了时间。
您可以使用
outputargs
的参数下载handler
来更改下载按钮的标签:Argh, it's so simple if you carefully read the manual. Sorry for wasting your time.
You can use the
outputArgs
parameter ofdownloadHandler
to change the label of the download-button:不确定如何使用
downloadhandler()
进行操作,但是您可以尝试下载软件包。Not sure how to do it using
downloadHandler()
but you can try downloadthis package.