-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathr34-app.R
454 lines (386 loc) · 18.8 KB
/
r34-app.R
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#App to display Partner Services data collected in Network Canvas
#install dependencies if needed
if (!require(shiny)) install.packages("shiny")
if (!require(dplyr)) install.packages("dplyr")
if(!require(DT)) install.packages("DT")
if(!require(rclipboard)) install.packages("rclipboard")
#load libraries
library(shiny); library(dplyr); library(DT); library(rclipboard)
# source some data cleaning functions
source("r34_cleaning.R")
#UI
ui <- navbarPage("Partner Services Network Canvas Data Upload",
id = "navpage",
tabPanel("Data",
# Sidebar
sidebarLayout(
# Sidebar panel
sidebarPanel(
# Input: Select a file
fileInput("all_data", "Upload .zip File",
multiple = TRUE,
accept = c(".zip")),
),
mainPanel(
# this textOuput checks that the data have been loaded correctly
# and the number of referral contacts included
# probably could do something fancier, but not sure it's necessary
textOutput("check_load"),
value = "Data",
fluidRow(
column(6,
# these actionButtons allow us to navigate between
# the different navbarpage/menu/panels/etc
# this one uses the observeEvent 'jumpToSexint"
# has the text "Next" on it, and has width 200px
actionButton('jumpToVenues','Next',width='200px')
),
),
)
)
),
tabPanel(title = "Venues",
value = "venues",
h3("Venues"),
fluidRow(
rclipboardSetup(),
column(12,
# this selectInput allows people to choose which contact
# the data will be displayed for - choices here gets updated
# using the observe in the server function
selectInput('Venues','Venues:',choices = 1)),
column(12,
# output our datatable w/venues
DT::dataTableOutput("venues")),
column(3,
# another navigation button
actionButton('jumpToData','Previous',width='200px')),
column(3,
# another navigation button
actionButton('jumpToSexint','Next',width='200px'))
),
br()
),
navbarMenu("Sexual behavior",
tabPanel(title = "Within Interview Period",
value = "interview",
h3("Sexual Behavior Within Interview Period"),
fluidRow(
rclipboardSetup(),
column(12,
# this selectInput allows people to choose which interview period
# the data will be displayed for
selectInput('IP','Interview Period:',
choices = list("3 months", "7 months", "12 months"))),
column(12,
#select interview period start and end dates
dateInput("date_start", "Interview period start date: ")),
column(12,
# output our datatable w/sexual behaviour in selected interview period
DT::dataTableOutput("sexbehavIP")),
column(3,
# another navigation button
actionButton('jumpToVenues','Previous',width='200px')),
column(3,
# another navigation button
actionButton('jumpToSex12m','Next',width='200px'))
),
),
tabPanel("Within 12 months",value = "12m",
h3("Sexual Behavior Within 12 months"),
fluidRow(
# I don't know if you need the rclipboardSetup()
# on each tabpanel/etc or just once, but doesn't seem
# to hurt having it on each one - this is so that we
# can have copy buttons in the datatable outputted, "sexbehav12m"
rclipboardSetup(),
column(12,
# output our datatable w/sexual behaviour in the past 12 months
DT::dataTableOutput("sexbehav12m")),
column(3,
# another navigation button
actionButton('jumpBackToSexint','Previous',width='200px')),
column(3,
# another navigation button
actionButton('jumpToSub12m','Next',width='200px'))
),
# added a line break to make there be a little white space below
# the buttons
br()
)
),
navbarMenu("Substance Use",
tabPanel(title = "Within 12 months",
value = "12msub",
h3("Substance Use Within 12 months"),
fluidRow(
# allows for the copy buttons in "druguse12m" table
rclipboardSetup(),
column(12,
# datatable with drug use in the previous 12 months
DTOutput("druguse12m")),
column(3,
# another navigation button
actionButton('jumpBackToSex12m','Previous',width='200px')),
column(3,
# another navigation button
actionButton('jumpToReferrals','Next',width='200px'))
),
br()
),
),
tabPanel(title = "Referral contacts",
value = "Referral",
h3("Referral Contacts"),
fluidRow(
column(12,
# this selectInput allows people to choose which contact
# the data will be displayed for - choices here gets updated
# using the observe in the server function
selectInput('Contact','Contact:',choices = 1)),
column(12,
# data table with referral contact info
DTOutput("referral_table")),
column(6,
# another navigation button
actionButton('jumpBackToSub12m','Previous',width='200px'))
),
br()
)
)
#server
server <- function(input, output) {
# use the reactive() function to allow us to have an object we can use throughout
# that stores the cleaned data - after running this
# can use graph_dat() to call the data throughout
graph_dat <- reactive({
req(input$all_data)
req(input$date_start)
# run our data_cleaning function from the r34_cleaning script
return(data_cleaning(input$all_data$datapath, input$date_start))
})
# this just checks that our data has loaded in and creates a message to tell the user
# that it was loaded ok
output$check_load <- renderText({
req(input$all_data)
# is there something called "egodat" in our graph_dat() object?
check1 <- "egodat" %in% names(graph_dat())
# how many referrals did we have (first column in "contact_referral" dataframe
# is the responses, so the number of columns - 1 is the number of referrals)
check2 <- ncol(graph_dat()$contact_referral)-1
if(check1) {
# concatenate a string with our check
outstring <- paste0("Respondent data successfully loaded, with ",check2,
" contact referrals provided.")
} else {
# if not tell the user that the zip file they provided didn't have what
# we expected it to have
outstring <- "No respondent data loaded."
}
return(outstring)
})
# data table with all sexual behavior for the past 12 months in it
output$sexbehav12m <- renderDT({
req(input$all_data)
data <- graph_dat()$sexbehav12m
# add a column with a "Copy" button - this is super finnicky and I don't
# understand how the rclipButton function works - I guess it's outputting
# HTML for the clip button, and then because we use "escape=FALSE" below
# that HTML gets rendered into a clip button...
data$Copy <- unlist(lapply(data$Responses,
function(x) {
rclipButton(
# not sure what this does
inputId = "clipbtn",
# the button will say "Copy" on it
label = "Copy",
# text that's getting copied is data$Responses
clipText = x,
# there'll be a little picture of a clipboard
icon = icon("clipboard")
) %>% as.character()
}))
data <- DT::datatable(data,
# this tells us how many things in the table we want to have
# on a single page, could fiddle with this instead of 25
options = list(pageLength = 25),
# this is just an appearance thing
class = "cell-border stripe",
# turn off rownames
rownames = FALSE,
# Need "escape = FALSE" to render the HTML for the clip button
# correctly
escape = FALSE)
return(data)
})
# data table with sexual behavior for interview period
# determines which interview period was selected and renders the corresponding table
output$sexbehavIP <- renderDT({
req(input$all_data)
#if statement, if choice is 90 days, render 90 days, etc
if (input$IP == "3 months") {
data <- graph_dat()$sexbehav90days
}
if (input$IP == "7 months"){
data <- graph_dat()$sexbehav7mo
}
if (input$IP == "12 months"){
data <- graph_dat()$sexbehav12m
}
# add a column with a "Copy" button
data$Copy <- unlist(lapply(data$Responses,
function(x) {
rclipButton(
# not sure what this does
inputId = "clipbtn",
# the button will say "Copy" on it
label = "Copy",
# text that's getting copied is data$Responses
clipText = x,
# there'll be a little picture of a clipboard
icon = icon("clipboard")
) %>% as.character()
}))
data <- DT::datatable(data,
# this tells us how many things in the table we want to have
# on a single page, could fiddle with this instead of 25
options = list(pageLength = 25),
# this is just an appearance thing
class = "cell-border stripe",
# turn off rownames
rownames = FALSE,
# Need "escape = FALSE" to render the HTML for the clip button
# correctly
escape = FALSE)
return(data)
})
# This one is basically identical to the sexbehav12m one but for druguse12m
output$druguse12m <- renderDT({
req(input$all_data)
data <- graph_dat()$druguse12m
data$Copy <- unlist(lapply(data$Responses,
function(x) {
rclipButton(
inputId = "clipbtn",
label = "Copy",
clipText = x,
icon = icon("clipboard")
) %>% as.character()
}))
data <- DT::datatable(data,
options = list(pageLength = 25),
class = "cell-border stripe",
rownames = FALSE,
# caption = "Substance use within 12 Months",
escape = FALSE)
return(data)
})
# These are all of the observeEvents for the navigation buttons
# tell us that when someone has clicked on the different buttons, which
# page do they want to move to (the "selected" argument) - these names
# are in the "value" argument of the tabPanel etc
observeEvent(input$jumpToSexint, {
updateNavbarPage(inputId = "navpage",
selected = "interview")
})
observeEvent(input$jumpToData, {
updateNavbarPage(inputId = "navpage",
selected = "Data")
})
observeEvent(input$jumpToVenues, {
updateNavbarPage(inputId= "navpage",
selected = "venues")
})
observeEvent(input$jumpToSex12m, {
updateNavbarPage(inputId = "navpage",
selected = "12m")
})
observeEvent(input$jumpBackToSexint, {
updateNavbarPage(inputId = "navpage",
selected = "interview")
})
observeEvent(input$jumpToSubint, {
updateNavbarPage(inputId = "navpage",
selected = "interviewsub")
})
observeEvent(input$jumpBackToSex12m, {
updateNavbarPage(inputId = "navpage",
selected = "12m")
})
observeEvent(input$jumpBackToSubint, {
updateNavbarPage(inputId = "navpage",
selected = "interviewsub")
})
observeEvent(input$jumpToSub12m, {
updateNavbarPage(inputId = "navpage",
selected = "12msub")
})
observeEvent(input$jumpBackToSub12m, {
updateNavbarPage(inputId = "navpage",
selected = "12msub")
})
observeEvent(input$jumpToReferrals, {
updateNavbarPage(inputId = "navpage",
selected = "Referral")
})
# This updates the Contact dropdown to respond to the number of contact referrals in the
# graph_dat()$contact_referral object (ncols-1 because first column is "Response" column)
observe({
updateSelectInput(inputId="Contact", choices = 1:(ncol(graph_dat()$contact_referral)-1))
})
# This updates the Venues dropdown to respond to the number of venues in the
# graph_dat()$venues object (ncols-1 because first column is "Response" column)
observe({
updateSelectInput(inputId="Venues", choices = 1:(ncol(graph_dat()$venues)-1))
})
# This table is similar to the sexbehav12m and druguse12m but adds in needing to figure out
# which referral we want to be displaying data for
output$referral_table <- renderDT({
req(input$all_data)
# choosing to only display the contact_referral columns 1 ("Responses" column)
# and as.numeric(input$Contact)+1 which is using the dropdown menu input
data <- graph_dat()$contact_referral[,c(1,as.numeric(input$Contact)+1)]
names(data)[2] <- "Responses"
data$Copy <- unlist(lapply(data[,2],
function(x) {
rclipButton(
inputId = "clipbtn",
label = "Copy",
clipText = x,
icon = icon("clipboard")
) %>% as.character()
}))
data <- DT::datatable(data,
options = list(pageLength = 25),
class = "cell-border stripe",
rownames = FALSE,
# caption = "Substance use within 12 Months",
escape = FALSE)
return(data)
})
output$venues <- renderDT({
req(input$all_data)
#choosing to display venues columns 1 ("Responses" column)
# and as.numeric(input$Venues)+1 which is using the dropdown menu input
data <- graph_dat()$venues[,c(1,as.numeric(input$Venues)+1)]
names(data)[2] <- "Responses"
data$Copy <- unlist(lapply(data[,2],
function(x) {
rclipButton(
inputId = "clipbtn",
label = "Copy",
clipText = x,
icon = icon("clipboard")
) %>% as.character()
}))
data <- DT::datatable(data,
options = list(pageLength = 25),
class = "cell-border stripe",
rownames = FALSE,
# caption = "Substance use within 12 Months",
escape = FALSE)
return(data)
})
}
# Run the app
shinyApp(ui, server)