-
Notifications
You must be signed in to change notification settings - Fork 1
/
classify-example.Rmd
307 lines (259 loc) · 10.3 KB
/
classify-example.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
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
---
title: "Classify"
output:
flexdashboard::flex_dashboard:
orientation: columns
#logo: # add the relative path/file.png
#favicon: # add the relative path/file.png
theme: bootstrap
#css: custom.css # add the relative path/file.css
vertical_layout: fill
runtime: shiny
---
```{r global, include=FALSE, message=FALSE}
# setup
# -----------------------------------------------------------------------------
library(flexdashboard)
library(shiny)
library(rdrop2)
library(tidyverse)
library(shinyWidgets)
library(DT)
# authenticate with dropbox for remote storage
# -----------------------------------------------------------------------------
# is file storage remote (1) or local (0)
remote <- 0
# https://github.com/karthik/rdrop2
# https://github.com/karthik/rdrop2/issues/61#issuecomment-423341288
# run once:
# drop_auth()
# token <- drop_auth()
# saveRDS(token, file = "droptoken.rds") # updload to server with app
if (remote==1) {
outputDir <- "dash" # output directory on Dropox (toplevel)
drop_auth(rdstoken = "droptoken.rds") # give token
}
# construct helpers
# -----------------------------------------------------------------------------
# set reactive to delay eventReactives
set.seed(1)
rv <- reactiveValues()
rv$run2 <- 0
# input variables
vars <- c("rater", "classification", "other", "confidence", "language")
```
```{r load, include=FALSE, message=FALSE}
# master files
# -----------------------------------------------------------------------------
# load current masters
if (remote==1) { # load from dropbox
master <- reactiveValues(df = drop_read_csv("/dash/master.csv",
stringsAsFactors = FALSE))
raters <- drop_read_csv("/dash/raters.csv", stringsAsFactors = FALSE)
} else { # load from local repo
master <- reactiveValues(df = read.csv("dash/master.csv",
stringsAsFactors = FALSE))
raters <- read.csv("dash/raters.csv", stringsAsFactors = FALSE)
}
# get new master on submit
eventReactive(rv$run2, {
if (remote==1) {
master$df <- drop_read_csv("/dash/master.csv", stringsAsFactors = FALSE)
} else {
master$df <- read.csv("dash/master.csv", stringsAsFactors = FALSE)
}
}, ignoreNULL = TRUE)
# processing
# -----------------------------------------------------------------------------
# check for items that have agreement between two raters
done <- eventReactive(rv$run2, {
master$df %>%
group_by(id, classification) %>%
count() %>%
filter(n>1 & !(is.na(classification)))
}, ignoreNULL=FALSE)
# find items that the rater has not rated
rated <- eventReactive(rv$run2, {
master$df %>%
group_by(id, rater) %>%
filter(rater==input$rater)
}, ignoreNULL=FALSE)
# select an item for the rater
class <- eventReactive(rv$run2, {
temp <-
master$df %>%
filter(!(id %in% done()$id)) %>%
filter(!(id %in% rated()$id))
# prevent error message if there are no more messages for this rater
validate(need(nrow(temp)!=0,
"There are no more messages to classify. Nice work!"))
temp %>%
sample_n(1)
}, ignoreNULL=FALSE)
```
Task
=====================================
Column {data-width=300}
-----------------------------------------------------------------------
```{r ui-rater}
# define UI
conditionalPanel(
condition = "input.rater == ''",
mainPanel(br(),
br(),
pickerInput(inputId = "rater",
label = "Select your name",
choices = raters$name,
options = list(title = "Make a selection"),
multiple = FALSE)
)
)
```
Column {data-width=600}
-----------------------------------------------------------------------
Welcome! You will read SMS messages and classify the user's intent. In other words, you'll try to identify what the user wants based on the text message. The intents that populate the selection list are described on the "Definitions" panel. If you think the user is asking about something not included in the list, select 'Another option not in this list' and describe the user's intent in a few words in the space that will pop-up. If you read the message and have no clue what the user wants, just select 'Cannot make sense of text'. For every selection, you must also rate your confidence in your choice. When you hit the `submit` button, your selections will be captured and a new message will appear. Keep going as long as you want. If you hit the `submit` button and nothing happens, it's likely because you did not make a selection in all fields.
```{r ui-ratings}
# create object with SMS question
output$textq <- renderText(class()$question)
# define inputs
# -----------------------------------------------------------------------------
fillCol(height = 600, flex = c(NA, 1),
mainPanel(
br(),
br(),
# show SMS question
tags$style("#textq{font-size: 30px;
}"
),
conditionalPanel(
condition = "input.rater != ''",
textOutput("textq")
),
br(),
br()
),
conditionalPanel(
condition = "input.rater != ''",
wellPanel(
# classification input
pickerInput(inputId = "classification",
label = "Select the best classification",
choices = c("intent 1",
"intent 2",
"intent 3",
"intent 4",
"Another option not in this list",
"Cannot make sense of text"),
options = list(title = "Make a selection"),
multiple = FALSE),
conditionalPanel(
condition = "input.classification == 'Another option not in this list'",
# other classification input
textInput("other",
"In a few words, describe what the user wants to know:",
value = NA,
width = '600px')
),
conditionalPanel(
condition = "input.classification != 'Cannot make sense of text' &
input.classification != ''",
# rating confidence
pickerInput(inputId = "confidence",
label = "How confident are you about this selection?",
choices = c("Not at all confident",
"Not very confident",
"Somewhat confident",
"Very confident"),
options = list(title = "Make a selection"),
multiple = FALSE)
),
# indicate language
pickerInput(inputId = "language",
label = "Language of message",
choices = c("Engish", "Spanish", "German", "Not sure"),
options = list(title = "Make a selection"),
multiple = FALSE),
actionButton("submit", "Submit", width = '200px')
)
)
)
```
```{r ratings}
# compile data from inputs
# -----------------------------------------------------------------------------
dat <- reactive({
# require certain inputs
req(input$classification)
if (input$classification == "Another option not in this list") {
req(input$other)
}
if (input$classification != "Cannot make sense of text" &
input$classification != "") {
req(input$confidence)
}
req(input$rater)
req(input$language)
#
dat_ <- data.frame(t(unlist(sapply(vars, function(x) input[[x]]))))
dat_ %>%
mutate(id = class()$id,
question = class()$question) %>%
select(id, question, rater, classification, other, confidence, language)
})
# submit data
# -----------------------------------------------------------------------------
observeEvent(input$submit, {
# update master with data from inputs
master$df <-
master$df %>%
bind_rows(dat())
# save
if (remote==1) {
write.csv(master$df, file.path(tempdir(), "master.csv"), row.names = FALSE,
quote = TRUE)
drop_upload(file.path(tempdir(), "master.csv"), path = outputDir)
} else {
write.csv(master$df, file="dash/master.csv", row.names = FALSE, quote = TRUE)
}
# update counter for eventReactives
rv$run2 <- rv$run2 + 1
# update SMS question object for display
output$textq <- renderText(class()$question)
# update inputs
updatePickerInput(session, "classification",
label = "Select the best classification",
choices = c("intent 1",
"intent 2",
"intent 3",
"intent 4",
"Another option not in this list",
"Cannot make sense of text"))
updatePickerInput(session, "confidence",
label = "How confident are you about this selection?",
choices = c("Not at all confident",
"Not very confident",
"Somewhat confident",
"Very confident"))
updateTextInput(session, "other",
"In a few words, describe what the user wants to know:",
value = NA)
updatePickerInput(session, "language",
label = "Language of message",
choices = c("Engish", "Spanish", "German", "Not sure"))
})
```
Definitions
=====================================
```{r}
defs <- data.frame(label = c("intent 1",
"intent 2",
"intent 3",
"intent 4"),
definition = c("Insert here",
"Insert here",
"Insert here",
"Insert here"))
DT::renderDT({
datatable(defs)
})
```