Wrapper around bsplus::bs_button() to provide a download button for HTML
outputs in R Markdown. Internally, the function writes the file to
tempdir(), encodes it, and produces the download button. Currently,
Internet Explorer does not support downloading embedded files. For
downloading links, files, or directories, see download_link(),
download_file(), and download_dir().
Arguments
- .data
A data frame or (named) list to write to disk. See 'Examples' for more details.
- ...
attributes (named arguments) and children (unnamed arguments) of the button, passed to
htmltools::tag().- output_name
Name of of the output file, if
NULLuses the deparsed.dataobject.- output_extension
Extension of the output file. Currently,
.csv,.xlsx, and.rdsare supported. If a (named) list is passed to the function, only.xlsxand.rdsare supported.Character (HTML), button label
Character, one of the standard Bootstrap types
- icon
Fontawesome tag e.g.: "fa fa-save", set to
NULLto not show any icon.- self_contained
A boolean to specify whether your HTML output is self-contained. Default to
FALSE.- csv2
A boolean to specify whether to use
readr::write_csv2()in case theoutput_extensionis chosen as '.csv'. IfFALSE,readr::write_csv()will be used instead. Default toTRUE.- ggsave_args
List of arguments to pass to
ggplot2::ggsave, e.g.:list(height = 5).
Value
htmltools::tag, <button>
Warning
This example will write the mtcars dataset to tempdir() and produce the
download button for the file mtcars dataset.csv with the fa fa-save icon
on the Download data label.
Examples
if (FALSE) { # \dontrun{
# Passing a data frame to the function
mtcars %>%
download_this(
output_name = "mtcars dataset",
output_extension = ".csv",
button_label = "Download data",
button_type = "warning",
icon = "fa fa-save"
)
# Passing a list with data frames to the function
list(mtcars, iris) %>%
download_this(
output_name = "mtcars and iris datasets",
output_extension = ".xlsx",
button_label = "Download data",
button_type = "warning",
icon = "fa fa-save"
)
# Passing a named list with data frames to the function
list("mtcars dataset" = mtcars, "iris dataset" = iris) %>%
download_this(
output_name = "mtcars and iris datasets",
output_extension = ".xlsx",
button_label = "Download data",
button_type = "warning",
icon = "fa fa-save"
)
# Passing any R object to the function
vector_example <- 1:10
linear_model <- lm(mpg ~ gear, data = mtcars)
list(mtcars, iris, vector_example, linear_model) %>%
download_this(
output_name = "datasets, vector, and linear model",
output_extension = ".rds",
button_label = "Download as rds",
button_type = "warning",
icon = "fa fa-save"
)
} # }
