tabBoxPlot.R 8.59 KB
Newer Older
dmattek's avatar
Added:  
dmattek committed
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
require(DT)

tabBoxPlotUI =  function(id, label = "Comparing t-points") {
  ns <- NS(id)
  
  tagList(
    uiOutput(ns('varSelTpts')),
    
    DT::dataTableOutput(ns('outTabStats')),
    downloadButton(ns('downloadData4BoxPlot'), 'Download single-cell data'),
    
    fluidRow(
      column(
        6,
        radioButtons(ns('inPlotType'), 'Plot type:', list('Box-plot' = 'box', 
                                                          'Dot-plot' = 'dot', 
                                                          'Violin-plot' = 'viol',
                                                          'Line-plot' = 'line')),
        uiOutput(ns('uiPlotBoxNotches')),
        uiOutput(ns('uiPlotBoxOutliers')),
        uiOutput(ns('uiPlotDotNbins'))
      ),
      column(
        6,
        selectInput(
          ns('selPlotBoxLegendPos'),
          label = 'Select legend position',
          choices = list(
            "Top" = 'top',
            "Right" = 'right',
            "Bottom" = 'bottom'
          ),
          selected = 'top'
        )
      )
    ),

    actionButton(ns('butPlotBox'), 'Plot!'),
    plotOutput(ns('outPlotBox'), height = 800),
    downPlotUI(ns('downPlotBox'), "Download PDF")
  )
  
}

####
## server box-plot
tabBoxPlot = function(input, output, session, in.data) {
  # return all unique time points (real time)
  # This will be used to display in UI for box-plot
  # These timepoints are from the original dt and aren't affected by trimming of x-axis
  getDataTpts <- reactive({
    cat(file = stderr(), 'getDataTpts\n')
    loc.dt = in.data()
    
    if (is.null(loc.dt))
      return(NULL)
    else
      return(unique(loc.dt[, realtime])) # column name specified in data4trajPlot
  })
  
  # prepare data for plotting boxplots
  # uses the same dt as for trajectory plotting
  # returns dt with these columns:
  data4boxPlot <- reactive({
    cat(file = stderr(), 'data4boxPlot\n')
    
    loc.dt = in.data()
    if (is.null(loc.dt))
      return(NULL)
    
    loc.out = loc.dt[realtime %in% input$inSelTpts]
  })
  
  output$varSelTpts = renderUI({
    cat(file = stderr(), 'UI varSelTpts\n')
    
    ns <- session$ns
    
    loc.v = getDataTpts()
    if (!is.null(loc.v)) {
      selectInput(
        ns('inSelTpts'),
        'Select one or more timepoints:',
        loc.v,
        width = '100%',
        selected = 0,
        multiple = TRUE
      )
    }
  })
  
  output$uiPlotBoxNotches = renderUI({
    cat(file = stderr(), 'UI uiPlotBoxNotches\n')
    
    ns <- session$ns
    
    if(input$inPlotType == 'box')
             checkboxInput(ns('inPlotBoxNotches'), 'Box plot notches?', FALSE)
  })
  
  output$uiPlotBoxOutliers = renderUI({
    cat(file = stderr(), 'UI uiPlotBoxNotches\n')
    
    ns <- session$ns
    
    if(input$inPlotType == 'box')
      checkboxInput(ns('inPlotBoxOutliers'), 'Box plot outliers?', FALSE)
  })
  
  output$uiPlotDotNbins = renderUI({
    cat(file = stderr(), 'UI uiPlotDotNbins\n')
    
    ns <- session$ns
    
    if(input$inPlotType == 'dot')
dmattek's avatar
Mod:    
dmattek committed
116
      sliderInput(ns('inPlotDotNbins'), 'Dot-plot bin size (10^x):', min = -4, max = 4, value = 0, step = 0.1)
dmattek's avatar
Added:  
dmattek committed
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
  })
  
  
  calcStats = reactive({
    cat(file = stderr(), 'tabBoxPlot: calsStats\n')
    loc.dt = data4boxPlot()
    
    if (is.null(loc.dt))
      return(NULL)
    
    loc.dt.aggr = loc.dt[, sapply(.SD, function(x) list('Mean' = mean(x), 'CV' = sd(x)/mean(x), 'Median' = median(x), 'rCV (IQR)' = IQR(x)/median(x), 'rCV (MAD)'= mad(x)/median(x))), .SDcols = c('y'), by = .(realtime, group)]
    setnames(loc.dt.aggr, c('Time point', 'Group', 'Mean', 'CV', 'Median', 'rCV IQR', 'rCV MAD'))
    print(loc.dt.aggr)
    return(loc.dt.aggr)
  })
  
  output$downloadData4BoxPlot <- downloadHandler(
    filename = 'data4boxplot.csv',
    content = function(file) {
      write.csv(data4boxPlot(), file, row.names = FALSE)
    }
  )
  
  
  # output$outTabStats = DT::renderDataTable(calcStats(), 
  #                                          server = FALSE, 
  #                                          rownames = FALSE,
  #                                          extensions = 'Buttons', 
  #                                          options = list(
  #                                            dom = 'Bfrtip',
  #                                            buttons = list('copy', 
  #                                                           'print', 
  #                                                           list(extend = 'collection',
  #                                                                buttons = list(list(extend='csv',
  #                                                                                    filename = 'hitStats'),
  #                                                                               list(extend='excel',
  #                                                                                    filename = 'hitStats'),
  #                                                                               list(extend='pdf',
  #                                                                                    filename= 'hitStats')),
  #                                                                text = 'Download'))))
  # 
  output$outTabStats = DT::renderDataTable(server = FALSE, {
    cat(file = stderr(), 'tabBoxPlot: outTabStats\n')
    loc.dt = calcStats()

    if (is.null(loc.dt))
      return(NULL)

    datatable(loc.dt, 
              rownames = FALSE,
              extensions = 'Buttons', 
              options = list(
                dom = 'Bfrtip',
                buttons = list('copy', 
                               'print', 
                               list(extend = 'collection',
                                    buttons = list(list(extend='csv',
                                                        filename = 'hitStats'),
                                                   list(extend='excel',
                                                        filename = 'hitStats'),
                                                   list(extend='pdf',
                                                        filename= 'hitStats')),
                                    text = 'Download')))) %>% formatRound(3:7, 3)
  })
  
  # Boxplot - display
  output$outPlotBox = renderPlot({
    locBut = input$butPlotBox
    
    if (locBut == 0) {
      cat(file = stderr(), 'plotBox: Go button not pressed\n')
      return(NULL)
    }
    
    plotBox()
    
  }, height = 800)
  
  # Boxplot - download pdf
  callModule(downPlot, "downPlotBox", 'boxplot.pdf', plotBox, TRUE)
  
  # 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
  
  plotBox <- function() {
    cat(file = stderr(), 'plotBox\n')
    
    loc.dt = data4boxPlot()
    
    cat(file = stderr(), "plotBox: on to plot\n\n")
    if (is.null(loc.dt)) {
      cat(file = stderr(), 'plotBox: dt is NULL\n')
      return(NULL)
    }
    
    cat(file = stderr(), 'plotBox:dt not NULL\n')
    
    
    
    p.out = ggplot(loc.dt, aes(x = as.factor(realtime), y = y))
    
    if (input$inPlotType == 'box')
      p.out = p.out + geom_boxplot(
        aes(fill = group),
        #position = position_dodge(width = 1),
        notch = input$inPlotBoxNotches,
        outlier.colour = if (input$inPlotBoxOutliers)
          'red'
        else
          NA
      )
    
    if(input$inPlotType == 'dot')
dmattek's avatar
Mod:    
dmattek committed
231
      p.out = p.out + geom_dotplot(aes(fill = group), binaxis = "y", stackdir = "center", position = "dodge", binwidth = 10^(input$inPlotDotNbins), method = 'histodot')
dmattek's avatar
Added:  
dmattek committed
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
    
    if(input$inPlotType == 'viol')
      p.out = p.out + geom_violin(aes(fill = group))
    
    if(input$inPlotType == 'line')
      p.out = p.out + geom_path(aes(color = group, group = id))
    
    p.out = p.out +
      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
      )
    
    return(p.out)
  }
  
}