tabScatter.R 10.5 KB
Newer Older
dmattek's avatar
dmattek committed
1 2 3 4 5 6
#
# Time Course Inspector: Shiny app for plotting time series data
# Author: Maciej Dobrzynski
#
# This module is a tab for plotting scatter plots between two time points
#
dmattek's avatar
dmattek committed
7 8 9 10 11 12 13 14 15 16
# Use:
# in ui.R
# tabPanel(
#  'Hierarchical',
#  clustHierUI('TabClustHier'))
#
# in server.R
# callModule(clustHier, 'TabClustHier', dataMod)
# where dataMod is the output from a reactive function that returns dataset ready for clustering

dmattek's avatar
dmattek committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
helpText.tabScatter = c(
  alScatter = paste0(
    "Display a relationship between measurements at two different time points as a scatter plot. ",
    "Instead of using the exact measurements at selected time points, you have an option to smooth and use local average of measurements around chosen time points."
  ),
  rBfoldChange = paste0(
    "Y-axis can display a value at a selected time point (the magnitude), ",
    "or a difference between the value at time point selected for Y-axis and the value at time point displayed on the X-axis ",
    "(i.e. the amplitude at the second time point with respect to the value at the first time point)."
  ),
  chBregression = 'Add a line with linear regression and regions of 95% confidence interval.',
  inNeighTpts = paste0(
    "Window length in time points for smoothing with the average used before plotting the scatterplot. ",
    "Useful to avoid artefacts in the scatterplot due to spurious variations at specific time points."
  ),
  inPlotHeight = 'Height in pixels of the displayed plot',
  inPlotNcolFacet = 'Number of facets in a row. Each facet displayes a scatter plot for a single group.'
)
dmattek's avatar
dmattek committed
35

dmattek's avatar
dmattek committed
36
# UI ----
dmattek's avatar
dmattek committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
tabScatterPlotUI <-
  function(id, label = "Scatter plot between two time points") {
    ns <- NS(id)
    
    tagList(
      h4("Scatter plot between two time points"),
      actionLink(ns("alScatter"), "Learn more"),
      br(),
      br(),
      fluidRow(
        column(
          4,
          uiOutput(ns('uiSelTptX')),
          uiOutput(ns('uiSelTptY')),
          bsAlert("alertAnchor2differentTpts")
dmattek's avatar
dmattek committed
52
        ),
dmattek's avatar
dmattek committed
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
        column(
          4,
          numericInput(
            ns('inNeighTpts'),
            'Smoothing',
            value = 0,
            step = 1,
            min = 0,
            width = "120px"
          ),
          bsTooltip(
            ns('inNeighTpts'),
            helpText.tabScatter[["inNeighTpts"]],
            placement = "top",
            trigger = "hover",
            options = NULL
          ),
          checkboxInput(ns('chBregression'), 'Linear regression with 95% CI'),
          bsTooltip(
            ns('chBregression'),
            helpText.tabScatter[["chBregression"]],
            placement = "top",
            trigger = "hover",
            options = NULL
          ),
          radioButtons(
            ns('rBfoldChange'),
            'Y-axis',
            choices = c("Y" = "y", "Y - X" = "diff"),
            width = "100px",
            inline = T
          ),
          bsTooltip(
            ns("rBfoldChange"),
            helpText.tabScatter[["rBfoldChange"]],
            placement = "top",
            trigger = "hover",
            options = NULL
          )
dmattek's avatar
dmattek committed
92
        ),
dmattek's avatar
dmattek committed
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
        column(
          4,
          numericInput(
            ns('inPlotHeight'),
            'Height [px]',
            value = PLOTSCATTERHEIGHT,
            min = 100,
            step = 100,
            width = "100px"
          ),
          bsTooltip(
            ns('inPlotHeight'),
            helpText.tabScatter[["inPlotHeight"]],
            placement = "top",
            trigger = "hover",
            options = NULL
          ),
          
          numericInput(
            ns('inPlotNcolFacet'),
            '#columns',
            value = PLOTNFACETDEFAULT,
            min = 1,
            step = 1,
            width = "100px"
          ),
          bsTooltip(
            ns('inPlotNcolFacet'),
            helpText.tabScatter[["inPlotNcolFacet"]],
            placement = "top",
            trigger = "hover",
            options = NULL
          )
        )
      ),
      
      br(),
      checkboxInput(ns('plotInt'),
                    'Interactive Plot',
                    value = FALSE),
133
      actionButton(ns('butPlot'), 'Plot!'),
dmattek's avatar
dmattek committed
134 135
      uiOutput(ns("plotInt_ui")),
      downPlotUI(ns('downPlotScatter'), "Download Plot")
dmattek's avatar
dmattek committed
136 137
    )
  }
dmattek's avatar
dmattek committed
138

dmattek's avatar
dmattek committed
139 140 141 142
# SERVER ----
tabScatterPlot <-
  function(input, output, session, in.data, in.fname) {
    ns <- session$ns
dmattek's avatar
dmattek committed
143
    
dmattek's avatar
dmattek committed
144 145 146 147 148 149 150 151 152 153 154 155 156
    # UI rendering ----
    # 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[[COLRT]]))
    })
157
    
dmattek's avatar
dmattek committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
    output$uiSelTptX = renderUI({
      cat(file = stderr(), 'tabScatter:uiSelTptX\n')
      
      ns <- session$ns
      
      loc.v = getDataTpts()
      if (!is.null(loc.v)) {
        selectInput(
          ns('inSelTptX'),
          'Time point for X-axis',
          loc.v,
          width = '180px',
          selected = 0,
          multiple = FALSE
        )
      }
    })
175
    
dmattek's avatar
dmattek committed
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
    output$uiSelTptY = renderUI({
      cat(file = stderr(), 'tabScatter:uiSelTptY\n')
      
      ns <- session$ns
      
      loc.v = getDataTpts()
      if (!is.null(loc.v)) {
        selectInput(
          ns('inSelTptY'),
          'Time point for Y-axis',
          loc.v,
          width = '180px',
          selected = 1,
          multiple = FALSE
        )
      }
    })
193 194
    
    
dmattek's avatar
dmattek committed
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
    data4scatterPlot <- reactive({
      cat(file = stderr(), 'data4scatterPlot\n')
      
      loc.dt.in = in.data()
      if (is.null(loc.dt.in))
        return(NULL)
      
      # obtain selected time points from UI
      loc.tpts.x = as.integer(input$inSelTptX)
      loc.tpts.y = as.integer(input$inSelTptY)
      
      if (loc.tpts.x == loc.tpts.y) {
        createAlert(
          session,
          "alertAnchor2differentTpts",
          "alert2differentTpts",
          title = "Error",
          content = "Select two different time points.",
          append = FALSE,
          style = "danger"
        )
        return(NULL)
        
      } else {
        closeAlert(session, "alert2differentTpts")
      }
      
      # if neigbbouring points selected, obtain time points for which the aggregation will be calculated
      if (input$inNeighTpts > 0) {
        # get all time points in the dataset
        loc.dt.in.tpts = unique(loc.dt.in[[COLRT]])
        
        # get indices of time points around selected time points
        loc.tpts.x.id = seq(
          which(loc.dt.in.tpts == loc.tpts.x) - input$inNeighTpts,
          which(loc.dt.in.tpts == loc.tpts.x) + input$inNeighTpts,
          1
        )
        loc.tpts.y.id = seq(
          which(loc.dt.in.tpts == loc.tpts.y) - input$inNeighTpts,
          which(loc.dt.in.tpts == loc.tpts.y) + input$inNeighTpts,
          1
        )
        
        # get only indices of time points that are greater than 0
        loc.tpts.x.id = loc.tpts.x.id[loc.tpts.x.id > 0]
        loc.tpts.y.id = loc.tpts.y.id[loc.tpts.y.id > 0]
        
        # update time points used for aggregation
        loc.tpts.x = loc.dt.in.tpts[loc.tpts.x.id]
        loc.tpts.y = loc.dt.in.tpts[loc.tpts.y.id]
        
        # aggregate separately each time point sets
        loc.dt.x = loc.dt.in[get(COLRT) %in% loc.tpts.x, .(y.aggr = mean(get(COLY))), by = c(COLGR, COLID)]
        loc.dt.y = loc.dt.in[get(COLRT) %in% loc.tpts.y, .(y.aggr = mean(get(COLY))), by = c(COLGR, COLID)]
        
        loc.dt = merge(loc.dt.x, loc.dt.y, by = COLID)
        loc.dt[, group.y := NULL]
        
        setnames(loc.dt,
                 c('group.x', 'y.aggr.x', 'y.aggr.y'),
                 c(COLGR, 'x', 'y'))
        
        #cat(loc.tpts.x.id, '\n')
        #cat(loc.tpts.y.id, '\n')
      } else {
        # get data from selected time points
        loc.dt = loc.dt.in[get(COLRT) %in% c(loc.tpts.x, loc.tpts.y)]
        
        # convert to wide, such that two selected time points are in two columns
        loc.dt = dcast(loc.dt[, c(COLGR, COLID, COLY, COLRT), with = F],
                       as.formula(paste0(COLGR, "+", COLID, "~", COLRT)),
                       value.var = COLY)
        
        setnames(loc.dt, c(COLGR, COLID, "x", "y"))
      }
      
      
      
      if (input$rBfoldChange == "diff") {
        loc.dt[, y := y - x]
      }
      return(loc.dt)
      
    })
dmattek's avatar
dmattek committed
280
    
dmattek's avatar
dmattek committed
281
    
dmattek's avatar
dmattek committed
282 283 284 285 286 287 288
    # Plotting ----
    # 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
    
    plotScatter <- function() {
      cat(file = stderr(), "plotScatter\n")
289
      locBut = input$butPlot
dmattek's avatar
dmattek committed
290
      
291 292 293 294 295 296 297
      # Check if main data exists
      # Thanks to solate all mods in the left panel are delayed 
      # until clicking the Plot button
      loc.dt = isolate(in.data())
      validate(
        need(!is.null(loc.dt), "Nothing to plot. Load data first!")
      )    
dmattek's avatar
dmattek committed
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
      
      cat(file = stderr(), 'plotScatter:dt not NULL\n')
      
      p.out = LOCggplotScat(
        dt.arg = loc.dt,
        plotlab.arg = NULL,
        facet.arg = COLGR,
        facet.ncol.arg = input$inPlotNcolFacet,
        alpha.arg = 0.5,
        trend.arg = input$chBregression,
        ci.arg = 0.95
      )
      return(p.out)
    }
    
    # Plot rendering ----
    output$outPlotScatter <- renderPlot({
      plotScatter()
    })
    
    output$outPlotScatterInt <- renderPlotly({
      # This is required to avoid
      # "Warning: Error in <Anonymous>: cannot open file 'Rplots.pdf'"
      # When running on a server. Based on:
      # https://github.com/ropensci/plotly/issues/494
      
      if (names(dev.cur()) != "null device")
        dev.off()
      pdf(NULL)
      
      return(plotly_build(plotScatter()))
      
    })
    
    # download pdf
    callModule(downPlot, "downPlotScatter", in.fname, plotScatter, TRUE)
    
    # Scatter plot - choose to display regular or interactive plot
    output$plotInt_ui <- renderUI({
      ns <- session$ns
      if (input$plotInt)
        tagList(withSpinner(plotlyOutput(
          ns("outPlotScatterInt"),
          height = paste0(input$inPlotHeight, "px")
        )))
      else
        tagList(withSpinner(plotOutput(
          ns('outPlotScatter'), height = paste0(input$inPlotHeight, "px")
        )))
    })
    
    # Pop-overs ----
    addPopover(
      session,
      id = ns("alScatter"),
      title = "Scatter plot",
      content = helpText.tabScatter[["alScatter"]],
      trigger = "click"
    )
  }