---
title: "Introduction to  downloadthis"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Introduction to  downloadthis}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

This package provides a wrapper around `bsplus::bs_button()`, which has to goal to provide a download button in HTML outputs from R Markdown without the need to have `runtime: shiny`.

```{block, type = 'rmdinfo'}
Currently, the download buttons functionality (embedded files) is not supported in Internet Explorer.

Please, refer to the [Customizing buttons with css vignette](customization.html) for tips on how to customize the buttons.
```

<br>

You can easily produce a download button as follows:

## Data frames

### Passing a data frame to the function

```{r}
library(downloadthis)

mtcars %>% 
  download_this(
    output_name = "mtcars dataset",
    output_extension = ".csv",
    button_label = "Download data as csv",
    button_type = "default",
    has_icon = TRUE,
    icon = "fa fa-save"
  )
```

## Lists (only `.xlsx` and `.rds` are supported)

### Passing a list with data frames to the function 

```{r}
list(mtcars, iris) %>%
  download_this(
    output_name = "mtcars and iris datasets",
    output_extension = ".xlsx",
    button_label = "Download datasets as xlsx",
    button_type = "warning",
    has_icon = TRUE,
    icon = "fa fa-save"
  )
```

### Passing a named list with data frames to the function

```{r}
list('mtcars dataset' = mtcars, 'iris dataset' = iris) %>%
  download_this(
    output_name = "mtcars and iris datasets",
    output_extension = ".xlsx",
    button_label = "Download datasets as xlsx",
    button_type = "warning",
    has_icon = TRUE,
    icon = "fa fa-save"
  )
```

## R Objects

In case you want to make any R object available for download, you may use the `.rds` extension.

```{r}
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",
    has_icon = TRUE,
    icon = "fa fa-save"
  )
```

## Web address

```{r}
## Link in Github repo
download_link(
  link = "https://github.com/fmmattioni/downloadthis/raw/master/inst/example/file_1.pdf",
  button_label = "Download pdf file",
  button_type = "danger",
  has_icon = TRUE,
  icon = "fa fa-save",
  self_contained = FALSE
)
```

## Local files

### Single file

```{r}
download_file(
  path = system.file("assets/css/all.min.css", package = "downloadthis"),
  output_name = "CSS file from downloadthis",
  button_label = "Download css file",
  button_type = "danger",
  has_icon = TRUE,
  icon = "fa fa-save",
  self_contained = FALSE
)
```

### Multiple files

```{r}
path_files <- list.files(path = system.file("assets/css", package = "downloadthis"), full.names = TRUE)

download_file(
  path = path_files,
  output_name = "Files from downloadthis",
  button_label = "Download files",
  button_type = "danger",
  has_icon = TRUE,
  icon = "fa fa-save",
  self_contained = FALSE
)
```

## Local directory

```{r}
download_dir(
  path = system.file("assets", package = "downloadthis"),
  output_name = "example dir",
  button_label = "Download directory",
  button_type = "success",
  has_icon = TRUE,
  icon = "fa fa-save",
  self_contained = FALSE
)
```

## ggplot2

```{r}
library(ggplot2)

plot <- ggplot(cars, aes(speed, dist)) +
  geom_point()

plot
```

```{r}
download_this(plot)
```
