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.
CodePudding user response:
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"
)
CodePudding user response:
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!
)
```