-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path09-uploads-downloads.Rmd
More file actions
250 lines (209 loc) · 5.8 KB
/
Copy path09-uploads-downloads.Rmd
File metadata and controls
250 lines (209 loc) · 5.8 KB
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# Uploads and downloads
```{r setup, include=FALSE}
knitr::opts_chunk$set(eval = FALSE)
```
## 9.4 Exercises {-}
2.
```{r}
library(shiny)
# Increase max limit of size of uploaded file
options(shiny.maxRequestSize = 10 * 1024^2)
ui <- fluidPage(
# upload a csv file
fileInput("upload", NULL,
buttonLabel = "Upload CSV", accept = ".csv"),
# select a variable
selectInput("var", "Select a variable", choices = NULL),
# show output of t.test()
verbatimTextOutput("t_test")
)
server <- function(input, output, session) {
# uploaded dataset
data <- reactive({
req(input$upload)
readr::read_csv(input$upload$datapath)
})
# once user uploads data, fill in the available variables
observeEvent(data(), {
choices <- unique(colnames(data()))
updateSelectInput(inputId = "var", choices = choices)
})
# show output of t-test
output$t_test <- renderPrint({
req(input$var)
t.test(data()[[input$var]], mu = 0)
})
}
shinyApp(ui, server)
```
3.
```{r}
library(shiny)
library(tidyverse)
ui <- fluidPage(
# upload a csv file
fileInput("upload", NULL,
buttonLabel = "Upload CSV", accept = ".csv"),
# select a variable
selectInput("var", "Select a variable", choices = NULL),
# show histogram
plotOutput("plot"),
radioButtons("ext", "Save As:",
choices = c("png", "pdf", "svg"), inline = TRUE),
# download histogram
downloadButton("download")
)
server <- function(input, output, session) {
# uploaded dataset
data <- reactive({
req(input$upload)
read_csv(input$upload$datapath)
})
# once user uploads data, fill in the available variables
observeEvent(data(), {
choices <- unique(colnames(data()))
updateSelectInput(inputId = "var", choices = choices)
})
# create reactive plot
plot_output <- reactive({
req(input$var)
ggplot(data()) +
geom_histogram(aes(.data[[input$var]]))
})
# show histogram
output$plot <- renderPlot({
req(input$var)
plot_output()
})
# download
output$download <- downloadHandler(
filename = function() {
paste("histogram", input$ext, sep = ".")
},
content = function(file) {
ggsave(file, plot_output(), device = input$ext)
}
)
}
shinyApp(ui, server)
```
4. From [Mastering Shiny Solutions 2021](https://mastering-shiny-solutions.org/uploads-and-downloads.html#exercise-9.4.4):
```{r}
library(shiny)
library(brickr)
library(png)
# Function to provide user feedback (checkout Chapter 8 for more info).
notify <- function(msg, id = NULL) {
showNotification(msg, id = id, duration = NULL, closeButton = FALSE)
}
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
fluidRow(
fileInput("myFile", "Upload a PNG file", accept = c('image/png')),
sliderInput("size", "Select size:", min = 1, max = 100, value = 35),
radioButtons("color", "Select color palette:", choices = c("universal", "generic"))
)
),
mainPanel(
plotOutput("result"))
)
)
server <- function(input, output) {
imageFile <- reactive({
if(!is.null(input$myFile))
png::readPNG(input$myFile$datapath)
})
output$result <- renderPlot({
req(imageFile())
id <- notify("Transforming image...")
on.exit(removeNotification(id), add = TRUE)
imageFile() %>%
image_to_mosaic(img_size = input$size, color_palette = input$color) %>%
build_mosaic()
})
}
shinyApp(ui, server)
```
5. From the 9.3 Case study, the main change happens in the cleaning step inside the server function, where one large reactive is broken down into three smaller ones.
```{r}
library(shiny)
# Uploading and parsing the file
ui_upload <- sidebarLayout(
sidebarPanel(
fileInput("file", "Data", buttonLabel = "Upload..."),
textInput("delim", "Delimiter (leave blank to guess)", ""),
numericInput("skip", "Rows to skip", 0, min = 0),
numericInput("rows", "Rows to preview", 10, min = 1)
),
mainPanel(
h3("Raw data"),
tableOutput("preview1")
)
)
# Cleaning the file
ui_clean <- sidebarLayout(
sidebarPanel(
checkboxInput("snake", "Rename columns to snake case?"),
checkboxInput("constant", "Remove constant columns?"),
checkboxInput("empty", "Remove empty cols?")
),
mainPanel(
h3("Cleaner data"),
tableOutput("preview2")
)
)
# Downloading the file.
ui_download <- fluidRow(
column(width = 12, downloadButton("download", class = "btn-block"))
)
# which get assembled into a single fluidPage():
ui <- fluidPage(
ui_upload,
ui_clean,
ui_download
)
server <- function(input, output, session) {
# Upload ---------------------------------------------------------
raw <- reactive({
req(input$file)
delim <- if (input$delim == "") NULL else input$delim
vroom::vroom(input$file$datapath, delim = delim, skip = input$skip)
})
output$preview1 <- renderTable(head(raw(), input$rows))
# Clean step ---------------------------------------------------------
# Breaking one large reactive up into multiple pieces
cleaned_names <- reactive({
out <- raw()
if (input$snake) {
names(out) <- janitor::make_clean_names(names(out))
}
out
})
removed_empty <- reactive({
out <- cleaned_names()
if (input$empty) {
out <- janitor::remove_empty(out, "cols")
}
out
})
removed_constant <- reactive({
out <- removed_empty()
if (input$constant) {
out <- janitor::remove_constant(out)
}
out
})
output$preview2 <- renderTable(head(removed_constant(), input$rows))
# Download -------------------------------------------------------
output$download <- downloadHandler(
filename = function() {
paste0(tools::file_path_sans_ext(input$file$name), ".tsv")
},
content = function(file) {
vroom::vroom_write(removed_constant(), file)
}
)
}
shinyApp(ui, server)
```