server.R 17.8 KB
Newer Older
dmattek's avatar
dmattek committed
1

2

dmattek's avatar
dmattek committed
3

dmattek's avatar
dmattek committed
4
5
6
7
8
9
10
11
12
13
14
15
# This is the server logic for a Shiny web application.
# You can find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com
#

library(shiny)
library(shinyjs) #http://deanattali.com/shinyjs/
library(data.table)
library(ggplot2)
library(plotly)

16
17
# increase file upload limit
options(shiny.maxRequestSize = 30 * 1024 ^ 2)
dmattek's avatar
dmattek committed
18
19
source('auxfunc.R')

dmattek's avatar
dmattek committed
20
shinyServer(function(input, output, session) {
21
  useShinyjs()
dmattek's avatar
dmattek committed
22
  
23
24
25
26
27
28
29
30
  # This is only set at session start
  # we use this as a way to determine which input was
  # clicked in the dataInBoth reactive
  counter <- reactiveValues(
    # The value of inDataGen1,2 actionButton is the number of times they were pressed
    dataGen1     = isolate(input$inDataGen1),
    dataLoadNuc  = isolate(input$inButLoadNuc)
    #dataLoadStim = isolate(input$inButLoadStim)
dmattek's avatar
dmattek committed
31
32
  )
  
dmattek's avatar
dmattek committed
33
34
35
  ####
  ## UI for side panel
  
dmattek's avatar
dmattek committed
36
  # FILE LOAD
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
  # This button will reset the inFileLoad
  observeEvent(input$inButReset, {
    reset("inFileLoadNuc")  # reset is a shinyjs function
    #reset("inButLoadStim")  # reset is a shinyjs function
  })
  
  # generate random dataset 1
  dataGen1 <- eventReactive(input$inDataGen1, {
    cat("dataGen1\n")
    
    return(userDataGen())
  })
  
  # load main data file
  dataLoadNuc <- eventReactive(input$inButLoadNuc, {
    cat("dataLoadNuc\n")
    locFilePath = input$inFileLoadNuc$datapath
    
    counter$dataLoadNuc <- input$inButLoadNuc - 1
    
    if (is.null(locFilePath) || locFilePath == '')
      return(NULL)
    else {
      return(fread(locFilePath))
    }
  })
  
dmattek's avatar
dmattek committed
64
65
66
67
68
69
70
  # This button will reset the inFileLoad
  observeEvent(input$butReset, {
    reset("inFileLoadNuc")  # reset is a shinyjs function
    #    reset("inFileStimLoad")  # reset is a shinyjs function
    
  })
  
dmattek's avatar
dmattek committed
71
72
  
  # COLUMN SELECTION
dmattek's avatar
dmattek committed
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
  output$varSelTrackLabel = renderUI({
    cat(file = stderr(), 'UI varSelTrackLabel\n')
    locCols = getDataNucCols()
    locColSel = locCols[locCols %like% 'rack'][1] # index 1 at the end in case more matches; select 1st
    
    cat(locColSel, '\n')
    selectInput(
      'inSelTrackLabel',
      'Select Track Label (e.g. objNuc_Track_ObjectsLabel):',
      locCols,
      width = '100%',
      selected = locColSel
    )
  })
  
  output$varSelTime = renderUI({
    cat(file = stderr(), 'UI varSelTime\n')
    locCols = getDataNucCols()
    locColSel = locCols[locCols %like% 'RealTime'][1] # index 1 at the end in case more matches; select 1st
    
    cat(locColSel, '\n')
    selectInput(
      'inSelTime',
      'Select time column (e.g. RealTime):',
      locCols,
      width = '100%',
      selected = locColSel
    )
  })
  
  # This is main field to select plot facet grouping
  # It's typically a column with the entire experimental description,
  # e.g. in Yannick's case it's Stim_All_Ch or Stim_All_S.
  # In Coralie's case it's a combination of 3 columns called Stimulation_...
  output$varSelGroup = renderUI({
    cat(file = stderr(), 'UI varSelGroup\n')
    locCols = getDataNucCols()
    
    if (!is.null(locCols)) {
      locColSel = locCols[locCols %like% 'ite']
      if (length(locColSel) == 0)
        locColSel = locCols[locCols %like% 'eries'][1] # index 1 at the end in case more matches; select 1st
      else if (length(locColSel) > 1) {
        locColSel = locColSel[1]
      }
      #    cat('UI varSelGroup::locColSel ', locColSel, '\n')
      selectInput(
        'inSelGroup',
        'Select one or more facet groupings (e.g. Site, Well, Channel):',
        locCols,
        width = '100%',
        selected = locColSel,
        multiple = TRUE
      )
    }
    
  })
  
  output$varSelSite = renderUI({
    cat(file = stderr(), 'UI varSelSite\n')
    locCols = getDataNucCols()
    locColSel = locCols[locCols %like% 'ite'][1] # index 1 at the end in case more matches; select 1st
    
    cat(locColSel, '\n')
    selectInput(
      'inSelSite',
      'Select FOV (e.g. Metadata_Site or Metadata_Series):',
      locCols,
      width = '100%',
      selected = locColSel
    )
  })
  
  
  
  
  output$varSelMeas1 = renderUI({
    cat(file = stderr(), 'UI varSelMeas1\n')
    locCols = getDataNucCols()
    
    if (!is.null(locCols)) {
dmattek's avatar
dmattek committed
154
155
      locColSel = locCols[locCols %like% 'objCyto_Intensity_MeanIntensity_imErkCor.*' |
                            locCols %like% 'Ratio'][1] # index 1 at the end in case more matches; select 1st
dmattek's avatar
dmattek committed
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
      #    cat(locColSel, '\n')
      selectInput(
        'inSelMeas1',
        'Select 1st measurement:',
        locCols,
        width = '100%',
        selected = locColSel
      )
    }
  })
  
  
  output$varSelMeas2 = renderUI({
    cat(file = stderr(), 'UI varSelMeas2\n')
    locCols = getDataNucCols()
    
    if (!is.null(locCols) &&
        !(input$inSelMath %in% c('', '1 / '))) {
      locColSel = locCols[locCols %like% 'objNuc_Intensity_MeanIntensity_imErkCor.*'][1] # index 1 at the end in case more matches; select 1st
      #    cat(locColSel, '\n')
      selectInput(
        'inSelMeas2',
        'Select 2nd measurement',
        locCols,
        width = '100%',
        selected = locColSel
      )
    }
  })
  
  
dmattek's avatar
dmattek committed
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
  # UI for normalization
  
  output$uiChBnorm = renderUI({
    cat(file = stderr(), 'UI uiChBnorm\n')
    
    if (input$chBnorm) {
      radioButtons(
        'rBnormMeth',
        label = 'Select method',
        choices = list('fold-change' = 'mean', 'z-score' = 'z.score')
      )
    }
  })
  
  output$uiSlNorm = renderUI({
    cat(file = stderr(), 'UI uiSlNorm\n')
    
    if (input$chBnorm) {
      locTpts  = getDataTpts()
      
      if(is.null(locTpts))
        return(NULL)
      
      locRTmin = min(locTpts)
      locRTmax = max(locTpts)
      
      sliderInput(
        'slNormRtMinMax',
        label = 'Time range for norm.',
        min = locRTmin,
        max = locRTmax,
        value = c(locRTmin, 0.1 * locRTmax)
      )
    }
  })
  
  output$uiChBnormRobust = renderUI({
    cat(file = stderr(), 'UI uiChBnormRobust\n')
    
    if (input$chBnorm) {
      checkboxInput('chBnormRobust',
                    label = 'Robust stats',
                    FALSE)
    }
  })
  
  output$uiChBnormGroup = renderUI({
    cat(file = stderr(), 'UI uiChBnormGroup\n')
    
    if (input$chBnorm) {
      radioButtons('chBnormGroup',
                  label = 'Normalisation grouping',
                  choices = list('Entire dataset' = 'none', 'Per facet' = 'group', 'Per trajectory (Korean way)' = 'id'))
    }
  })
  
  
  
dmattek's avatar
dmattek committed
245
246
247
248
249
250
251
252
253
  ####
  ## data processing
  
  # generate random dataset 1
  dataGen1 <- eventReactive(input$inDataGen1, {
    cat("dataGen1\n")
    
    return(userDataGen())
  })
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
  
  dataInBoth <- reactive({
    # Without direct references to inDataGen1,2 and inFileLoad, inDataGen2
    #    does not trigger running this reactive once inDataGen1 is used.
    # This is one of the more nuanced areas of reactive programming in shiny
    #    due to the if else logic, it isn't fetched once inDataGen1 is available
    # The morale is use direct retrieval of inputs to guarantee they are available
    #    for if else logic checks!
    
    locInGen1 = input$inDataGen1
    locInLoadNuc = input$inButLoadNuc
    #locInLoadStim = input$inButLoadStim
    
    cat(
      "dataInBoth\ninGen1: ",
      locInGen1,
      "   prev=",
      isolate(counter$dataGen1),
      "\ninDataNuc: ",
      locInLoadNuc,
      "   prev=",
      isolate(counter$dataLoadNuc),
      # "\ninDataStim: ",
      # locInLoadStim,
      # "   prev=",
      # isolate(counter$dataLoadStim),
      "\n"
    )
    
    # isolate the checks of counter reactiveValues
    # as we set the values in this same reactive
    if (locInGen1 != isolate(counter$dataGen1)) {
      cat("dataInBoth if inDataGen1\n")
      dm = dataGen1()
      # no need to isolate updating the counter reactive values!
      counter$dataGen1 <- locInGen1
    } else if (locInLoadNuc != isolate(counter$dataLoadNuc)) {
      cat("dataInBoth if inDataLoadNuc\n")
      dm = dataLoadNuc()
      # no need to isolate updating the counter reactive values!
      counter$dataLoadNuc <- locInLoadNuc
    } else {
      cat("dataInBoth else\n")
      dm = NULL
    }
    return(dm)
  })
  
  # return column names of the main dt
dmattek's avatar
dmattek committed
303
  getDataNucCols <- reactive({
304
305
306
307
308
309
310
311
312
313
314
    cat(file = stderr(), 'getDataNucCols: in\n')
    loc.dt = dataInBoth()
    
    if (is.null(loc.dt))
      return(NULL)
    else
      return(colnames(loc.dt))
  })
  
  # return dt with an added column with unique track object label
  dataMod <- reactive({
dmattek's avatar
dmattek committed
315
    cat(file = stderr(), 'dataMod\n')
316
317
    loc.dt = dataInBoth()
    
dmattek's avatar
dmattek committed
318
    if (is.null(loc.dt))
319
320
321
322
323
      return(NULL)
    
    loc.dt[, trackObjectsLabelUni := paste(sprintf("%03d", get(input$inSelSite)),
                                           sprintf("%04d", get(input$inSelTrackLabel)),
                                           sep = "_")]
dmattek's avatar
dmattek committed
324
    
325
326
327
    return(loc.dt)
  })
  
dmattek's avatar
dmattek committed
328
329
330
331
332
  # return all unique track object labels (created in dataMod)
  # This will be used to display in UI for trajectory highlighting
  getDataTrackObjLabUni <- reactive({
    cat(file = stderr(), 'getDataTrackObjLabUni\n')
    loc.dt = dataMod()
333
    
dmattek's avatar
dmattek committed
334
335
336
337
    if (is.null(loc.dt))
      return(NULL)
    else
      return(unique(loc.dt$trackObjectsLabelUni))
338
339
  })
  
dmattek's avatar
dmattek committed
340
341
342
343
344
  # return all unique time points (real time)
  # This will be used to display in UI for trajectory highlighting
  getDataTpts <- reactive({
    cat(file = stderr(), 'getDataTpts\n')
    loc.dt = dataMod()
345
    
dmattek's avatar
dmattek committed
346
347
348
349
    if (is.null(loc.dt))
      return(NULL)
    else
      return(unique(loc.dt[[input$inSelTime]]))
350
351
352
353
354
  })
  
  
  # prepare data for plotting time courses
  # returns dt with these columns:
dmattek's avatar
dmattek committed
355
  #    realtime - selected from input
dmattek's avatar
dmattek committed
356
  #    y        - measurement selected from input
dmattek's avatar
dmattek committed
357
358
359
360
361
  #               (can be a single column or result of an operation on two cols)
  #    id       - trackObjectsLabelUni (created in dataMod)
  #    group    - grouping variable for facetting from input
  #    mid.in   - column with trajectory selection status from the input file or
  #               highlight status from UI
362
  data4trajPlot <- reactive({
dmattek's avatar
dmattek committed
363
    cat(file = stderr(), 'data4trajPlot\n')
364
365
    
    loc.dt = dataMod()
dmattek's avatar
dmattek committed
366
    if (is.null(loc.dt))
367
368
369
      return(NULL)
    
    
dmattek's avatar
dmattek committed
370
    if (input$inSelMath == '')
371
372
373
374
375
376
377
378
379
      loc.s.y = input$inSelMeas1
    else if (input$inSelMath == '1 / ')
      loc.s.y = paste0(input$inSelMath, input$inSelMeas1)
    else
      loc.s.y = paste0(input$inSelMeas1, input$inSelMath, input$inSelMeas2)
    
    # create expression for parsing
    # creates a merged column based on other columns from input
    # used for grouping of plot facets
dmattek's avatar
dmattek committed
380
381
382
383
    if(length(input$inSelGroup) == 0)
      return(NULL)
    loc.s.gr = sprintf("paste(%s, sep=';')",
                       paste(input$inSelGroup, sep = '', collapse = ','))
384
385
386
    
    loc.s.rt = input$inSelTime
    
dmattek's avatar
dmattek committed
387
388
389
390
    # Assign tracks selected for highlighting in UI
    loc.tracks.highlight = input$inSelHighlight
    locBut = input$chBhighlightTraj
    
391
392
393
394
395
396
397
398
399
400
    # if dataset contains column mid.in with trajectory filtering status,
    # then, include it in plotting
    if (sum(names(loc.dt) %in% 'mid.in') > 0) {
      loc.out = loc.dt[, .(
        y = eval(parse(text = loc.s.y)),
        id = trackObjectsLabelUni,
        group = eval(parse(text = loc.s.gr)),
        realtime = eval(parse(text = loc.s.rt)),
        mid.in = mid.in
      )]
dmattek's avatar
dmattek committed
401
402
403
404
405
406
407
      
      # add 3rd level with status of track selection
      # to a column with trajectory filtering status
      if (locBut) {
        loc.out[, mid.in := ifelse(id %in% loc.tracks.highlight, 'SELECTED', mid.in)]
      }
      
408
409
410
411
412
413
414
    } else {
      loc.out = loc.dt[, .(
        y = eval(parse(text = loc.s.y)),
        id = trackObjectsLabelUni,
        group = eval(parse(text = loc.s.gr)),
        realtime = eval(parse(text = loc.s.rt))
      )]
dmattek's avatar
dmattek committed
415
416
417
418
419
      
      # add a column with status of track selection
      if (locBut) {
        loc.out[, mid.in := ifelse(id %in% loc.tracks.highlight, 'SELECTED', 'NOT SEL')]
      }
420
    }
421
    
dmattek's avatar
dmattek committed
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
    
    # Normalization
    if (input$chBnorm) {
      loc.out = myNorm(
        in.dt = loc.out,
        in.meas.col = 'y',
        in.rt.col = 'realtime',
        in.rt.min = input$slNormRtMinMax[1],
        in.rt.max = input$slNormRtMinMax[2],
        in.type = input$rBnormMeth,
        in.robust = input$chBnormRobust,
        in.by.cols = if(input$chBnormGroup %in% 'none') NULL else input$chBnormGroup
      )
      
      loc.out[, y := NULL]
      setnames(loc.out, 'y.norm', 'y')
    }
    
440
441
    # remove rows with NA
    return(loc.out[complete.cases(loc.out)])
dmattek's avatar
dmattek committed
442
443
  })
  
dmattek's avatar
dmattek committed
444
445
446
447
  # prepare data for plotting boxplots
  # uses the same dt as for trajectory plotting
  # returns dt with these columns:
  data4boxPlot <- reactive({
dmattek's avatar
dmattek committed
448
    cat(file = stderr(), 'data4trajPlot\n')
dmattek's avatar
dmattek committed
449
    
dmattek's avatar
dmattek committed
450
    loc.dt = data4trajPlot()
dmattek's avatar
dmattek committed
451
    if (is.null(loc.dt))
dmattek's avatar
dmattek committed
452
      return(NULL)
dmattek's avatar
dmattek committed
453
    
dmattek's avatar
dmattek committed
454
    loc.out = loc.dt[realtime %in% input$inSelTpts]
dmattek's avatar
dmattek committed
455
456
  })
  
dmattek's avatar
dmattek committed
457
458
  ####
  ## UI for trajectory plot
dmattek's avatar
dmattek committed
459
  
dmattek's avatar
dmattek committed
460
461
  output$varSelHighlight = renderUI({
    cat(file = stderr(), 'UI varSelHighlight\n')
dmattek's avatar
dmattek committed
462
    
dmattek's avatar
dmattek committed
463
464
465
    locBut = input$chBhighlightTraj
    if (!locBut)
      return(NULL)
dmattek's avatar
dmattek committed
466
    
dmattek's avatar
dmattek committed
467
    loc.v = getDataTrackObjLabUni()
dmattek's avatar
dmattek committed
468
    if (!is.null(loc.v)) {
469
      selectInput(
dmattek's avatar
dmattek committed
470
471
472
        'inSelHighlight',
        'Select one or more rajectories:',
        loc.v,
473
        width = '100%',
dmattek's avatar
dmattek committed
474
        multiple = TRUE
475
      )
dmattek's avatar
dmattek committed
476
477
478
    }
  })
  
dmattek's avatar
dmattek committed
479
  output$uiPlotTraj = renderUI({
dmattek's avatar
dmattek committed
480
481
482
483
484
    plotlyOutput(
      "plotTraj",
      width = paste0(input$inPlotTrajWidth, '%'),
      height = paste0(input$inPlotTrajHeight, 'px')
    )
dmattek's avatar
dmattek committed
485
486
  })
  
487
  output$plotTraj <- renderPlotly({
dmattek's avatar
dmattek committed
488
    cat(file = stderr(), 'plotTraj: in\n')
dmattek's avatar
dmattek committed
489
    locBut = input$butPlotTraj
dmattek's avatar
dmattek committed
490
491
    
    if (locBut == 0) {
dmattek's avatar
dmattek committed
492
      cat(file = stderr(), 'plotTraj: Go button not pressed\n')
dmattek's avatar
dmattek committed
493
494
495
496
      
      return(NULL)
    }
    
497
    loc.dt = isolate(data4trajPlot())
dmattek's avatar
dmattek committed
498
    
499
500
    cat("plotScatter on to plot\n\n")
    if (is.null(loc.dt)) {
dmattek's avatar
dmattek committed
501
      cat(file = stderr(), 'plotTraj: dt is NULL\n')
dmattek's avatar
dmattek committed
502
      return(NULL)
dmattek's avatar
dmattek committed
503
504
    }
    
dmattek's avatar
dmattek committed
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
    cat(file = stderr(), 'plotTraj:dt not NULL\n')
    
    # Normalization
    # if (input$chBnorm) {
    #   loc.dt = myNorm(
    #     in.dt = loc.dt,
    #     in.meas.col = 'y',
    #     in.rt.col = 'realtime',
    #     in.rt.min = input$slNormRtMinMax[1],
    #     in.rt.max = input$slNormRtMinMax[2],
    #     in.type = input$rBnormMeth,
    #     in.robust = input$chBnormRobust,
    #     in.by.cols = if(input$chBnormGroup %in% 'none') NULL else input$chBnormGroup
    #   )
    #   #cat(input$slNormRtMinMax, '\n')
    #   loc.y.arg = 'y.norm'
    # } else {
    #   loc.y.arg = 'y'
    # }
    
    
    # Future: change such that a column with colouring status is chosen by the user
527
528
    # colour trajectories, if dataset contains mi.din column
    # with filtering status of trajectory
dmattek's avatar
dmattek committed
529
    if (sum(names(loc.dt) %in% 'mid.in') > 0)
530
531
532
      loc.line.col.arg = 'mid.in'
    else
      loc.line.col.arg = NULL
dmattek's avatar
dmattek committed
533
534
    
    p.out = myGgplotTraj(
535
536
537
538
539
540
      dt.arg = loc.dt,
      x.arg = 'realtime',
      y.arg = 'y',
      group.arg = "id",
      facet.arg = 'group',
      facet.ncol.arg = input$inPlotTrajFacetNcol,
541
542
      xlab.arg = 'Time (min)',
      line.col.arg = loc.line.col.arg
dmattek's avatar
dmattek committed
543
    )
dmattek's avatar
dmattek committed
544
    
dmattek's avatar
dmattek committed
545
    
dmattek's avatar
dmattek committed
546
    # This is required to avoid
dmattek's avatar
dmattek committed
547
548
549
    # "Warning: Error in <Anonymous>: cannot open file 'Rplots.pdf'"
    # When running on a server. Based on:
    # https://github.com/ropensci/plotly/issues/494
dmattek's avatar
dmattek committed
550
551
    if (names(dev.cur()) != "null device")
      dev.off()
dmattek's avatar
dmattek committed
552
553
    pdf(NULL)
    
dmattek's avatar
dmattek committed
554
555
    p.out.ly = plotly_build(p.out)
    return(p.out.ly)
dmattek's avatar
dmattek committed
556
557
  })
  
dmattek's avatar
dmattek committed
558
  
dmattek's avatar
dmattek committed
559
560
561
562
563
564
565
  ####
  ## UI for box-plot
  
  output$varSelTpts = renderUI({
    cat(file = stderr(), 'UI varSelTpts\n')
    
    loc.v = getDataTpts()
dmattek's avatar
dmattek committed
566
    if (!is.null(loc.v)) {
dmattek's avatar
dmattek committed
567
568
569
570
      selectInput(
        'inSelTpts',
        'Select one or more timepoints:',
        loc.v,
dmattek's avatar
dmattek committed
571
        width = '100%',
dmattek's avatar
dmattek committed
572
573
574
575
576
577
578
579
580
581
582
        selected = 0,
        multiple = TRUE
      )
    }
  })
  
  # Boxplot - display
  output$outPlotBox = renderPlot({
    locBut = input$butPlotBox
    
    if (locBut == 0) {
dmattek's avatar
dmattek committed
583
      cat(file = stderr(), 'plotBox: Go button not pressed\n')
dmattek's avatar
dmattek committed
584
585
586
587
      return(NULL)
    }
    
    plotBox()
dmattek's avatar
dmattek committed
588
    
dmattek's avatar
dmattek committed
589
590
591
592
593
594
595
  }, height = 800)
  
  # Boxplot - download pdf
  output$downPlotBox <- downloadHandler(
    filename = 'boxplot.pdf',
    
    content = function(file) {
dmattek's avatar
dmattek committed
596
597
598
599
600
601
602
603
604
605
606
      cat(file = stderr(),
          input$inPlotBoxWidth,
          input$inPlotBoxHeight,
          "\n")
      ggsave(
        file,
        limitsize = FALSE,
        plotBox(),
        width  = input$inPlotBoxWidth,
        height = input$inPlotBoxHeight
      )
dmattek's avatar
dmattek committed
607
608
609
610
611
612
613
    }
  )
  
  
  # Function instead of reactive as per:
  # http://stackoverflow.com/questions/26764481/downloading-png-from-shiny-r
  # This function is used to plot and to downoad a pdf
dmattek's avatar
dmattek committed
614
615
  
  plotBox <- function() {
dmattek's avatar
dmattek committed
616
617
618
619
    cat(file = stderr(), 'plotBox\n')
    
    loc.dt = data4boxPlot()
    
dmattek's avatar
dmattek committed
620
    cat(file = stderr(), "plotBox: on to plot\n\n")
dmattek's avatar
dmattek committed
621
    if (is.null(loc.dt)) {
dmattek's avatar
dmattek committed
622
      cat(file = stderr(), 'plotBox: dt is NULL\n')
dmattek's avatar
dmattek committed
623
624
625
      return(NULL)
    }
    
dmattek's avatar
dmattek committed
626
627
628
    cat(file = stderr(), 'plotBox:dt not NULL\n')

    
dmattek's avatar
dmattek committed
629
630
    
    ggplot(loc.dt, aes(x = as.factor(realtime), y = y)) +
dmattek's avatar
dmattek committed
631
632
633
634
635
636
      geom_boxplot(
        aes(fill = group),
        #position = position_dodge(width = 1),
        notch = input$inPlotBoxNotches,
        outlier.colour = if(input$inPlotBoxOutliers) 'red' else NA
      ) +
dmattek's avatar
dmattek committed
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
      scale_fill_discrete(name = '') +
      xlab('\nTime (min)') +
      ylab('') +
      theme_bw(base_size = 18, base_family = "Helvetica") +
      theme(
        panel.grid.minor = element_blank(),
        panel.grid.major = element_blank(),
        panel.border = element_blank(),
        axis.line.x = element_line(color = "black", size = 0.25),
        axis.line.y = element_line(color = "black", size = 0.25),
        axis.text.x = element_text(size = 12),
        axis.text.y = element_text(size = 12),
        strip.text.x = element_text(size = 14, face = "bold"),
        strip.text.y = element_text(size = 14, face = "bold"),
        strip.background = element_blank(),
        legend.key = element_blank(),
        legend.key.height = unit(1, "lines"),
        legend.key.width = unit(2, "lines"),
        legend.position = input$selPlotBoxLegendPos
      )
  }
dmattek's avatar
dmattek committed
658
})