-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.R
247 lines (226 loc) · 7.26 KB
/
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
# Load necessary libraries
library(shiny)
library(DT)
# Global reactive values to store data accessible across sessions
sharedData <- reactiveValues(
data = NULL,
annotations = data.frame(
id = character(),
text = character(),
username = character(),
label = character(),
stringsAsFactors = FALSE
),
class_labels = c("positive", "negative", "neutral"),
stats = list(
one_label = 0,
two_labels = 0,
three_labels = 0
)
)
# UI definition
ui <- fluidPage(
titlePanel("Text Annotation App"),
sidebarLayout(
sidebarPanel(
fileInput("file1", "Upload CSV File",
accept = c(".csv")),
uiOutput("username_ui"),
actionButton("settings", "Settings"),
h3("Statistics"),
verbatimTextOutput("stats_output"),
downloadButton("downloadData", "Download Annotations")
),
mainPanel(
h3("Annotate the Text"),
tags$blockquote(textOutput("text_to_annotate")),
uiOutput("label_buttons"),
h3("Annotations"),
p(actionButton("delete_annotation", "Delete Selected Annotation")),
DTOutput("annotations_table")
)
)
)
# Server logic
server <- function(input, output, session) {
# Store username per session
user_name <- reactiveVal(NULL)
# Prompt for username when the app is opened
observe({
if (is.null(user_name())) {
showModal(modalDialog(
title = "Enter Username",
textInput("username_input", "Username:", ""),
footer = tagList(
actionButton("submit_username", "Submit")
),
easyClose = FALSE
))
}
})
# Save username and remove modal
observeEvent(input$submit_username, {
req(input$username_input)
user_name(input$username_input)
removeModal()
})
# Display current user's username
output$username_ui <- renderUI({
req(user_name())
h4(paste("Logged in as:", user_name()))
})
# Load data when a file is uploaded
observeEvent(input$file1, {
req(input$file1)
data <- read.csv(input$file1$datapath, stringsAsFactors = FALSE)
if(!all(c("id", "text") %in% colnames(data))) {
showModal(modalDialog(
title = "Error",
"The uploaded file must contain 'id' and 'text' columns.",
easyClose = TRUE
))
return(NULL)
}
data$id <- as.character(data$id)
orig_length <- nrow(data)
data <- data[!duplicated(data$text), ]
if (orig_length > nrow(data)) {
showNotification(paste0("Removed ", (orig_length - nrow(data)), " duplicate texts from the uploaded dataset.", type = "info"))
}
sharedData$data <- data
})
# Determine next text to annotate
next_text <- reactive({
req(sharedData$data, user_name())
data <- sharedData$data
ann <- sharedData$annotations
user_ann <- ann[ann$username == user_name(), ]
data_to_annotate <- data[!(data$id %in% user_ann$id), ]
id_counts <- table(ann$id)
ids_with_less_than_three <- names(id_counts[id_counts < 3])
data_to_annotate <- data_to_annotate[data_to_annotate$id %in% ids_with_less_than_three | !(data_to_annotate$id %in% ann$id), ]
if(nrow(data_to_annotate) > 0) {
sample(data_to_annotate$text, 1)
} else {
NULL # Return NULL when no texts are left
}
})
# Display text to annotate
output$text_to_annotate <- renderText({
text <- next_text()
if (!is.null(text)) {
text
} else {
"No more texts to annotate."
}
})
# Generate label buttons
output$label_buttons <- renderUI({
req(sharedData$data)
labels <- sharedData$class_labels
text_available <- !is.null(next_text())
buttons <- lapply(labels, function(label) {
actionButton(session$ns(paste0("label_", label)), label, disabled = !text_available)
})
do.call(tagList, buttons)
})
# Handle label button clicks
observe({
req(sharedData$data, user_name())
labels <- sharedData$class_labels
lapply(labels, function(label) {
observeEvent(input[[paste0("label_", label)]], {
ann <- sharedData$annotations
current_text <- next_text()
if (is.null(current_text)) {
showNotification("No more texts to annotate.", type = "error")
return()
}
data <- sharedData$data
current_row <- data[data$text == current_text, ]
current_id <- current_row$id[1]
if(any(ann$id == current_id & ann$username == user_name())) {
showNotification("You have already annotated this text.", type = "error")
return()
}
new_ann <- data.frame(
id = current_id,
text = current_text,
username = user_name(),
label = label,
stringsAsFactors = FALSE
)
sharedData$annotations <- rbind(ann, new_ann)
update_stats()
})
})
})
# Update statistics
update_stats <- function() {
ann <- sharedData$annotations
counts <- table(ann$id)
sharedData$stats$one_label <- sum(counts == 1)
sharedData$stats$two_labels <- sum(counts == 2)
sharedData$stats$three_labels <- sum(counts == 3)
}
# Display statistics
output$stats_output <- renderText({
paste(
"Texts with one label:", sharedData$stats$one_label, "\n",
"Texts with two labels:", sharedData$stats$two_labels, "\n",
"Texts with three labels:", sharedData$stats$three_labels
)
})
# Render annotations table with reversed order
output$annotations_table <- renderDT({
ann <- sharedData$annotations
if (nrow(ann) > 0) {
ann <- ann[rev(seq_len(nrow(ann))), ] # Reverse the order
}
datatable(ann, selection = 'single')
})
# Handle deletion of annotations
observeEvent(input$delete_annotation, {
req(input$annotations_table_rows_selected)
ann <- sharedData$annotations
# Adjust the row index because the table is reversed
selected_row <- nrow(ann) - input$annotations_table_rows_selected + 1
ann <- ann[-selected_row, ]
sharedData$annotations <- ann
update_stats()
})
# Provide download functionality
output$downloadData <- downloadHandler(
filename = function() {
paste("annotations-", Sys.Date(), ".csv", sep="")
},
content = function(file) {
write.csv(sharedData$annotations, file, row.names = FALSE)
}
)
# Settings modal for class labels
observeEvent(input$settings, {
showModal(modalDialog(
title = "Configure Class Labels",
textInput("class_labels_input", "Class Labels (comma-separated):", value = paste(sharedData$class_labels, collapse = ", ")),
footer = tagList(
modalButton("Cancel"),
actionButton("save_class_labels", "Save")
),
easyClose = TRUE
))
})
# Save new class labels
observeEvent(input$save_class_labels, {
labels_input <- input$class_labels_input
labels <- unlist(strsplit(labels_input, "\\s*,\\s*"))
if(length(labels) > 0) {
sharedData$class_labels <- labels
removeModal()
} else {
showNotification("Please enter at least one class label.", type = "error")
}
})
}
# Run the application
shinyApp(ui, server)