-
Notifications
You must be signed in to change notification settings - Fork 41
/
09-uploads-downloads.Rmd
226 lines (160 loc) · 6.2 KB
/
09-uploads-downloads.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# Uploads and Downloads
**Learning objectives:**
* Learn how to include file upload/download functionality into a Shiny application
* Implement the key UI components required to provide uploading/downloading functionality in an app
* Implement the key server components required to provide uploading/downloading functionality in an app
* Describe how the the UI and server elements fit together to provide this functionality
* Develop an app that validates the user's input from an upload
* Observe examples where uploading/downloading functionality is applied within a simple Shiny application
## File uploads
### UI
To upload a file use `fileInput(id, label)`.
Some other arguments are:
- `multiple` - can multiple files be uploaded at once?
- `accept` - what file types are accepted? (character vector)
- file extension: `.csv`, `.tsv`, `.rds`
- MIME type: `application/JSON`, `image/png`
- one of: `audio/*`, `video/*`, `image/*`
- Use `?fileInput` in console to learn more
### Server
In the server, we work with a **data frame** with a special structure.
There are 4 columns:
- `name` - name of the file on user's computer
- `size` - file size in bytes. Default 5 MB; adjust with
`shiny.maxRequestSize` option.
- `type` - MIME type of the file
- `datapath` - file path on the server. Temporary.
## Uploading data
There are 2 things to note about uploading a data set.
- Use `req(input$file)` to make sure file is uploaded before code runs.
- Use `accept` argument to `fileInput()` to limit input types.
- browser doesn't always enforce, so make sure to `validate()`
Example of uploading a data set and validating file type (from book):
```{r, eval = FALSE}
ui <- fluidPage(
fileInput("file", NULL, accept = c(".csv", ".tsv")),
numericInput("n", "Rows", value = 5, min = 1, step = 1),
tableOutput("head")
)
server <- function(input, output, session) {
data <- reactive({
req(input$file)
ext <- tools::file_ext(input$file$name)
switch(ext,
csv = vroom::vroom(input$file$datapath, delim = ","),
tsv = vroom::vroom(input$file$datapath, delim = "\t"),
validate("Invalid file; Please upload a .csv or .tsv file")
)
})
output$head <- renderTable({
head(data(), input$n)
})
}
```
### Observe this in action
- Check out the `app-upload.R` example
### Use a break-point to observe what's going on here
- Add a breakpoint before the `switch()` function.
- Examine `input$upload`
- Examine `input$file$datapath` object
- Examine `ext`
- Import data in the interactive debugger
```{r eval=FALSE}
data <- reactive({
req(input$upload)
ext <- tools::file_ext(input$upload$name)
#> Add a breakpoint here
switch(ext,
csv = vroom::vroom(input$upload$datapath, delim = ","),
tsv = vroom::vroom(input$upload$datapath, delim = "\t"),
validate("Invalid file; Please upload a .csv or .tsv file")
)
})
```
## Downloads
### UI
- For file downloads, use `downloadButton(id, label)` or `downloadLink(id, label)`.
- Customize the appearance with the `class` or `icon` arguments.
### Server
In the server, use `downloadHandler(filename, content)`.
There are only 2 arguments, both are **functions**.
- `filename` - no arguments. Returns file name as a string.
- `content(file)` - one argument (`file`). Path to save the file.
```{r, eval = FALSE}
output$download <- downloadHandler(
filename = function() {
paste0(input$dataset, ".csv")
},
content = function(file) {
write.csv(data(), file)
}
)
```
## Downloading data
#### Diagram of data download functionality {-}
![](images/09-uploads-downloads/data-download.png)
We can allow the user to download a file containing data based on their exploration of an application.
- provide functionality to download a tab separated file (e.g., `.csv`; `.tsv`)
- provide functionality to download any type of content (e.g., `png`)
### Observe this in action
- Check out the `app-download-data.R` example
## Downloading reports
#### Diagram of report download functionality {-}
![](images/09-uploads-downloads/report-download.png)
We can allow the user to download a report based on their exploration of an
application.
- parameterized R Markdown is a good way to do this
- potential parameters - filters, simulation parameters
- specify parameters in YAML header of R Markdown document
- **key idea** - call `rmarkdown::render()` from `content` argument of
`downloadHandler()`
### Observe this in action
- Check out the `app-download-report.R` example
### Some tips & tricks when including report download functionality:
- `.Rmd` renders in working directory so copy file temporary directory before
rendering.
- `.Rmd` renders in current R process so consider running in separate session
with e.g. `callr` package.
## Meeting Videos
### Cohort 1
`r knitr::include_url("https://www.youtube.com/embed/o157ZBd0CUY")`
### Cohort 2
`r knitr::include_url("https://www.youtube.com/embed/SZIVWc91g-o")`
<details>
<summary> Meeting chat log </summary>
```
00:51:00 Ryan Metcalf: https://github.com/royfrancis/shinyapp_calendar_plot/blob/master/app.R
00:54:23 Ryan Metcalf: https://pandoc.org/
```
</details>
### Cohort 3
`r knitr::include_url("https://www.youtube.com/embed/A08gd1X74GQ")`
### Cohort 4
`r knitr::include_url("https://www.youtube.com/embed/jwexyE8C20E")`
<details>
<summary>Meeting chat log</summary>
```
00:08:10 Matthew Efoli: hi trevin
00:09:29 Lydia Gibson: Hello all
00:09:31 Lucio Cornejo: Hi everyone
00:09:34 Trevin Flickinger: Okay, I can start?
00:30:00 Lydia Gibson: To confirm, Ctrl + C stops Shiny apps? Or Ctrl + Esc ?
00:30:12 Lydia Gibson: Thank toy
00:30:15 Lydia Gibson: you*
00:30:22 Trevin Flickinger: Ctrl + C
00:49:07 Lydia Gibson: Thank you Trevin!
00:49:16 Trevin Flickinger: Joe Cheng talk link - https://www.youtube.com/watch?v=5KByRC6eqC8
00:49:34 Lucio Cornejo: the exercises were interesting as well, due to the libraries explored
00:50:07 Lucio Cornejo: thank you
00:55:11 Lydia Gibson: Shiny Conf: https://shinyconf.appsilon.com/
00:55:45 Lucio Cornejo: thanks, bye eveyone
```
</details>
### Cohort 5
`r knitr::include_url("https://www.youtube.com/embed/URL")`
<details>
<summary>Meeting chat log</summary>
```
LOG
```
</details>