-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathserver.R
More file actions
428 lines (364 loc) · 16.7 KB
/
server.R
File metadata and controls
428 lines (364 loc) · 16.7 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
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
##########################################################################################################
# THREADNET:SHINY SERVER
# This software may be used according to the terms provided in the
# GNU General Public License (GPL-3.0) https://opensource.org/licenses/GPL-3.0?
# Absolutely no warranty!
##########################################################################################################
server <- shinyServer(function(input, output, session) {
observe({
hide(selector = "#navbar li a[data-value=choosePOV]")
hide(selector = "#navbar li a[data-value=visualize]")
hide(selector = "#navbar li a[data-value=subsets]")
hide(selector = "#navbar li a[data-value=comparisons]")
hide(selector = "#navbar li a[data-value=movingWindow]")
hide(selector = "#navbar li a[data-value=parameterSettings]")
})
options(warn=-1)
options(shiny.maxRequestSize=30*1024^2)
# create reactive value to force execution of function that gets map names for menus
rv <-reactiveValues(newmap=0)
# get_CF returns the choice of contextual factors from the Data tab.
get_CF <<- reactive({ return( input$CFcolumnsID ) })
# get_POV returns the choice of POV from the POV tab
get_THREAD_CF <<- reactive({ return(input$THREAD_CF_ID) })
get_EVENT_CF <<- reactive({ return(input$EVENT_CF_ID) })
get_COMPARISON_CF <<- reactive({ return(setdiff(get_CF(), union(get_THREAD_CF(),get_EVENT_CF() ))) })
# time scale for use throughout the app
get_timeScale <<- reactive({ return(input$timeScaleID) })
# These sliders controls the zoom level for zooming in-out
# they are grouoped here because hopefully they can be replaced by a single function... except that reactive functions don't take parameters
get_Zoom_VIZ <<- reactive({ return( ifelse (zoom_upper_limit(get_POV(input$VisualizeEventMapInputID))==1 ,
"ZM_1", paste0("ZM_",input$VisualizeTabZoomID))) })
get_Zoom_COMP_A <<- reactive({
req(input$CompareMapInputID_A, input$CompareZoomID_A)
return( ifelse (zoom_upper_limit(get_POV(input$CompareMapInputID_A))==1 ,
"ZM_1", paste0("ZM_",input$CompareZoomID_A)))
})
get_Zoom_COMP_B <<- reactive({
req(input$CompareMapInputID_B, input$CompareZoomID_B)
return( ifelse (zoom_upper_limit(get_POV(input$CompareMapInputID_B))==1 ,
"ZM_1", paste0("ZM_",input$CompareZoomID_B)))
})
get_Zoom_DIA_COMP <<- reactive({
req(input$DiaCompareMapInputID, input$DiaCompareZoomID)
return( ifelse (zoom_upper_limit(get_POV(input$DiaCompareMapInputID))==1 ,
"ZM_1", paste0("ZM_",input$DiaCompareZoomID)))
})
get_Zoom_MOVE <<- reactive({
req(input$MovingWindowMapInputID, input$MovingWindowZoomID)
return( ifelse (zoom_upper_limit(get_POV(input$MovingWindowMapInputID))==1 ,
"ZM_1", paste0("ZM_",input$MovingWindowZoomID)))
})
get_Zoom_REGEX <<- reactive({
req(input$RegExInputMapID, input$regexZoomID)
return( ifelse (zoom_upper_limit(get_POV(input$RegExInputMapID))==1 ,
"ZM_1", paste0("ZM_",input$regexZoomID)))
})
get_Zoom_freqNgram <<- reactive({
req(input$freqNgramInputMapID, input$freqNgramZoomID)
return( ifelse (zoom_upper_limit(get_POV(input$freqNgramInputMapID))==1 ,
"ZM_1", paste0("ZM_",input$freqNgramZoomID)))
})
get_Zoom_CHUNK <<- reactive({
req(input$ChunkInputMapID, input$chunkZoomID)
return( ifelse (zoom_upper_limit(get_POV(input$ChunkInputMapID))==1 ,
"ZM_1", paste0("ZM_",input$chunkZoomID))) })
# add reactive value to force update
get_POV_names <- reactive({
rv$newmap
get_POV_name_list()
})
###########################
### read data functions ###
###########################
#dataframe for occurrences that are read in from file1
occ <<- eventReactive(input$inputFile,parseInputData(input$inputFile))
# selected columns from the raw data
selectOcc <<- reactive(occ()[c("tStamp", input$CFcolumnsID)] )
# select all rows using the nice DT input
# global for general use -- check what else calls this if anything
selectOccFilter <<- reactive(selectOcc()[input$dataFilter_rows_all,])
# The POV tabs reconstruct the data into threads by sorting by tStamp and
# adding columns for threadNum and seqNum for the selected POV in ThreadOccByPOV
threadedOcc <- reactive({
validate(need(input$THREAD_CF_ID != "", "You must select at least one context factor to define Threads"))
validate(need(input$EVENT_CF_ID != "", "You must select at least one context factor to define Events within Threads"))
ThreadOccByPOV( selectOccFilter(), input$THREAD_CF_ID, input$EVENT_CF_ID )
})
# Here we need a function to do the subset on the POV tab
threadedOccSubSet <- reactive(threadedOcc()[input$povDataThreads_rows_all,])
observeEvent(input$addPOVButton,
if (check_POV_name(input$POVMapName)){
POVName = input$POVMapName
showNotification(paste('POV Name', POVName , 'already exists, please select a different name'), type='message', duration=10)
} else {
rv$newmap <- rv$newmap+1 # trigger reactive value
isolate(
store_POV(input$POVMapName, threadedOccSubSet(),input$THREAD_CF_ID,input$EVENT_CF_ID ) # this is the name; need to get object to add
)
shinyjs::show(selector = "#navbar li a[data-value=visualize]")
shinyjs::show(selector = "#navbar li a[data-value=subsets]")
shinyjs::show(selector = "#navbar li a[data-value=comparisons]")
shinyjs::show(selector = "#navbar li a[data-value=movingWindow]")
shinyjs::show(selector = "#navbar li a[data-value=parameterSettings]")
showNotification(paste('New POV named', input$POVMapName ,'has been created'), type='message', duration=10 )
}, ignoreInit = TRUE)
# get the data that will be the input for this tab
chunkInputEvents <- reactive({
rv$newmap
req(input$ChunkInputMapID)
get_POV(input$ChunkInputMapID)
})
# this function runs when you push the button to create a new mapping based on chunks
observeEvent( input$EventButton2,
if (check_POV_name(input$EventMapName2)){
mapName2 = input$EventMapName2
showNotification(paste('POV Name', mapName2 , 'already exists, please select a different name'), type='message', duration=10 )
} else {
rv$newmap <- rv$newmap+1 # trigger reactive value
thread_CF = get_POV_THREAD_CF(input$ChunkInputMapID)
event_CF = get_POV_EVENT_CF(input$ChunkInputMapID)
compare_CF = get_POV_COMPARISON_CF(input$ChunkInputMapID, get_CF())
isolate(
OccToEvents_By_Chunk(
chunkInputEvents(),
input$Chunks_method_Button, # which method?
input$EventMapName2,
input$fixed_chunk_size,
input$chunk_time_gap_threshold,
get_timeScale(),
input$chunk_CFs,
thread_CF,
event_CF,
compare_CF )
)
showNotification(paste('New POV named', input$EventMapName2 ,'has been created'), type='message', duration=10 )
}, ignoreInit = TRUE )
# get the data that will be the input for this tab
regexInputEvents <- reactive({
req(input$RegExInputMapID)
get_POV(input$RegExInputMapID)
})
# get the input values and return data frame with regex & label
regexInput <- reactive({
req(input$numRegexInputRows)
data.frame(
pattern <- unlist(lapply(1:input$numRegexInputRows,function(i){input[[paste0('regex', i)]]})),
label <- unlist(lapply(1:input$numRegexInputRows,function(i){input[[paste0('regexLabel', i)]]})),
stringsAsFactors = FALSE
)
})
# this function runs when you push the button to create a new mapping
observeEvent(input$EventButton3,
if (check_POV_name(input$EventMapName3)){
mapName3 = input$EventMapName3
showNotification(paste('POV Name', mapName3 , 'already exists, please select a different name'), type='message', duration=10 )
} else {
rv$newmap <- rv$newmap+1 # trigger reactive value
thread_cf = get_POV_THREAD_CF(input$RegExInputMapID)
event_cf = get_POV_EVENT_CF(input$RegExInputMapID)
comp_cf = get_POV_COMPARISON_CF(input$RegExInputMapID, get_CF())
isolate(
OccToEvents3(
regexInputEvents(),
input$EventMapName3,
thread_cf,
event_cf,
comp_cf,
'threadNum',
get_Zoom_REGEX(),
regexInput(),
input$KeepIrregularEvents
)
)
showNotification(paste('New POV named', input$EventMapName3 ,'has been created'), type='message', duration=10 )
}, ignoreInit = TRUE )
# get the data that will be the input for this tab
freqNgramInputEvents <- reactive({
req(input$freqNgramInputMapID)
get_POV( input$freqNgramInputMapID)
})
fng_select <- reactive(
support_level(
thread_text_vector(
freqNgramInputEvents(),
'threadNum',
get_Zoom_freqNgram(),
' '
),
frequent_ngrams(
freqNgramInputEvents() ,
'threadNum',
get_Zoom_freqNgram(),
input$freqNgramRange[1],
input$freqNgramRange[2],
TRUE
)
)
)
# The bottom example shows a server-side table. Make sure you have included row names in the table (as the first column of the table).
# In the case of server-side processing, the row names of the selected rows are available in input$x3_rows_selected as a character vector.
selected_ngrams <- reactive({
req(input$freqnGramTable_rows_selected)
s <- as.integer(input$freqnGramTable_rows_selected)
data.frame(
pattern <- unlist(lapply(1:length(s),function(i){ str_replace_all(fng_select()[i,'ngrams'],' ',',') })),
label <- unlist(lapply(1:length(s),function(i){paste0("<",str_replace_all(fng_select()[i,'ngrams'],' ','_'),">")})),
stringsAsFactors=FALSE
)
})
# this function runs when you push the button to create a new mapping
observeEvent(input$EventButton4,
if (check_POV_name(input$EventMapName4)){
mapName4 = input$EventMapName4
showNotification(paste('POV Name', mapName4 , 'already exists, please select a different name'), type='message', duration=10 )
} else {
rv$newmap <- rv$newmap+1 # trigger reactive value
thread_cf = get_POV_THREAD_CF(input$freqNgramInputMapID)
event_cf = get_POV_EVENT_CF(input$freqNgramInputMapID)
comp_cf = get_POV_COMPARISON_CF(input$freqNgramInputMapID, get_CF())
isolate(
OccToEvents3(
freqNgramInputEvents(),
input$EventMapName4,
thread_cf,
event_cf,
comp_cf,
'threadNum',
get_Zoom_freqNgram(),
selected_ngrams(),
input$KeepIrregularEvents_2
)
)
showNotification(paste('New POV named', input$EventMapName4 ,'has been created'), type='message', duration=10 )
}, ignoreInit = TRUE)
# separate the cluster calculation from the dendrogram display
cluster_result <- eventReactive(input$EventButton6,{
validate(need(!(check_POV_name(input$EventMapName6)), paste0('Map Name ',input$EventMapName6,' already exists. Please select a different name.')))
rv$newmap <- rv$newmap+1 # trigger reactive value
# return a list with both the cluster_result and the new POV
# store the POV and return the cluster_result for display
thread_CF = get_POV_THREAD_CF(input$ClusterEventsInputID)
event_CF = get_POV_EVENT_CF(input$ClusterEventsInputID)
cluster_POV = clusterEvents(
get_POV(input$ClusterEventsInputID),
input$EventMapName6,
input$ClusterMethodID,
thread_CF,
event_CF,
'cluster')
e = cluster_POV[['POV']]
store_POV(input$EventMapName6,
e[order(e[['threadNum']],e[['seqNum']]),] ,
thread_CF,
event_CF )
showNotification(paste('New POV named', input$EventMapName6 ,'has been created'), type='message', duration=10 )
cluster_POV[['cluster_result']]
} )
# Get data for the Visualize tab.Need parallel functions for the other tabs.
subsetEventsViz <- reactive({
req(input$SelectSubsetMapInputID)
get_POV( input$SelectSubsetMapInputID )
})
# reactive functions for the export and delete buttons
observeEvent(input$DeleteMappingButton,{
rv$newmap <- rv$newmap+1 # trigger reactive value
delete_POV(input$ManageEventMapInputID)
showNotification(paste(input$ManageEventMapInputID, " deleted."), type='message', duration=10 )
}, ignoreInit = TRUE)
observeEvent(input$ExportMappingRData,{
export_POV(input$ManageEventMapInputID )
showNotification(paste(input$ManageEventMapInputID, " exported as .RData file"), type='message', duration=10)
})
observeEvent(input$ExportMappingCsv,{
export_POV_csv( input$ManageEventMapInputID )
showNotification(paste(input$ManageEventMapInputID, " exported as .csv file"), type='message', duration=10)
})
CurrentNetwork <- reactiveValues()
observe({
if(!is.null(viz_net()))
isolate(
CurrentNetwork <<- viz_net()
)
})
output$downloadNetwork <- downloadHandler(
filename = paste("CurrentNetwork_POV.Rdata"),
# content = function(file) { save( eval(assign('CurrentNetwork',viz_net())) , file = file) }
content = function(file) { save( CurrentNetwork , file = file) }
)
# This is on visualize tab, but logically it fits better here
observeEvent(input$save_edge_list_button,{
export_network(input$VisualizeEventMapInputID, viz_net() )
showNotification("Exported to CurrentNetwork_POV.Rdata", type='message', duration=10)
})
# Another opportunity to make subsets...
observeEvent(input$SelectSubsetButton,
if (check_POV_name(input$SelectSubsetMapName)){
SubsetMapName = input$SelectSubsetMapName
showNotification(paste('POV Name', SubsetMapName , 'already exists, please select a different name'), type='message', duration=10 )
} else {
rv$newmap <- rv$newmap+1 # trigger reactive value
store_POV( input$SelectSubsetMapName,
subsetEventsViz()[input$SelectSubsetDataTable_rows_all,],
get_POV_THREAD_CF(input$SelectSubsetMapInputID),
get_POV_EVENT_CF(input$SelectSubsetMapInputID))
showNotification(paste('New POV named', input$SelectSubsetMapName ,'has been created'), type='message', duration=10 )
}, ignoreInit = TRUE)
# Get data for the Visualize tab.Need parallel functions for the other tabs.
# threadedEventsViz <- reactive({get_POV( input$VisualizeEventMapInputID ) })
# Get data for the Visualize tab. Need parallel functions for the other tabs.
threadedEventsViz_ALL <- reactive({
req(input$VisualizeEventMapInputID)
get_POV( input$VisualizeEventMapInputID )
})
threadedEventsViz <- reactive({
req(input$VisualizeRangeID[1], input$VisualizeRangeID[2])
loc = input$VisualizeRangeID[1]
width=input$VisualizeRangeID[2] - input$VisualizeRangeID[1]+1
get_moving_window(threadedEventsViz_ALL(),width,loc) })
# Get data for the COMPARE tab mapping A
threadedEventsComp_A <- reactive({
req(input$CompareMapInputID_A)
get_POV(input$CompareMapInputID_A )
})
# Get data for the COMPARE tab mapping B.
threadedEventsComp_B <- reactive({
req(input$CompareMapInputID_B)
get_POV( input$CompareMapInputID_B )
})
# Get data for the Diachronic COMPARE tab.
threadedEventsDiaComp <- reactive({
req(input$DiaCompareMapInputID)
get_POV(input$DiaCompareMapInputID )
})
CF_levels <- reactive( get_CF_levels( threadedEventsDiaComp(),input$selectComparisonID) )
# Get data for the Moving Window tab.
threadedEventsMove <- reactive({
req(input$MovingWindowMapInputID)
get_POV(input$MovingWindowMapInputID )
})
threadedEventsMove_A <- reactive({
req(input$MovingWindowSizeID, input$WindowLocation_A_ID)
get_moving_window(
threadedEventsMove() ,
input$MovingWindowSizeID,
input$WindowLocation_A_ID
)
})
threadedEventsMove_B <- reactive({
req(input$MovingWindowSizeID, input$WindowLocation_B_ID)
get_moving_window(
threadedEventsMove(),
input$MovingWindowSizeID,
input$WindowLocation_B_ID
)
})
# Source tab-specific Server output functions
source(file.path("server", "readData.R"), local = TRUE)$value
source(file.path("server", "choosePOV.R"), local = TRUE)$value
source(file.path("server", "comparisons.R"), local = TRUE)$value
source(file.path("server", "visualize.R"), local = TRUE)$value
source(file.path("server", "subsets.R"), local = TRUE)$value
source(file.path("server", "movingWindow.R"), local = TRUE)$value
source(file.path("server", "parameterSettings.R"), local = TRUE)$value
})