-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathheatmapPlateMod.R
More file actions
139 lines (112 loc) · 4.55 KB
/
heatmapPlateMod.R
File metadata and controls
139 lines (112 loc) · 4.55 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
# shiny module of heatmap plate
# This module is used in a shiny app for plotting data as a heatmap in microplate layout
# Usage:
# 1. source this file in app.R or server.R
# 2. add hmplateUI('MODULE_ID') in ui
# 3. add callModule(hmplate, id = 'MODULE_ID', data, nrow, ncol)
# in server, where:
# data: a df with at least Well column and Value column
# nrow, ncol: microplate type (eg: nrow = 8, ncol = 12 for 96well plate)
library(magrittr)
library(dplyr)
library(tidyr)
library(highcharter)
hmplate_formatData <- function(df, nrow, ncol) {
stopifnot('Well' %in% names(df))
stopifnot('Value' %in% names(df))
if(!'Name' %in% names(df)) df %<>% mutate(Name = round(Value, 2))
row_names <- LETTERS[1:nrow]
col_names <- as.character(1:ncol)
full_plate <- expand.grid(Row = row_names, Col = col_names) %>%
unite(Well, Row, Col, sep = '', remove = F)
full_plate %>%
left_join(df, by = 'Well') %>%
rename(value = Value, name = Name) %>%
mutate(Row = factor(Row, levels = row_names),
Col = factor(Col, levels = col_names),
x = Col %>% as.integer() %>% subtract(1),
y = Row %>% as.integer() %>% subtract(1)) %>%
select(x, y, value, name, Well, Row, Col)
}
hmplate_buildHeatmap <- function(data) {
tooltip_format <-"function(){return '<span style=\"color: ' +
this.color + '\">\u25CF</span> ' +
this.Well + ': <b>' + this.name + '</b><br/>';}"
highchart() %>%
hc_xAxis(categories = data$Col %>% levels(),
title = list(), opposite = T, tickLength = 0) %>%
hc_yAxis(categories = data$Row %>% levels(),
title = list(), reversed = T) %>%
hc_add_series(data = list_parse(data),
type = 'heatmap',
dataLabels = list(enabled = T, format = '{point.name}',
color = 'black'),
states = list(
hover = list(color = "#023858"),
select = list(color = "yellow")
),
tooltip = list(headerFormat = '', pointFormatter = JS(tooltip_format)),
events = list(
click = JS("
function(event){
var chart = this.chart;
if (event.ctrlKey) {
event.point.select(null,true);
} else if (event.shiftKey){
var selectedPoints = chart.getSelectedPoints();
var lastPoint = selectedPoints[selectedPoints.length - 1];
var x_min = Math.min(lastPoint.x, event.point.x);
var x_max = Math.max(lastPoint.x, event.point.x);
var y_min = Math.min(lastPoint.y, event.point.y);
var y_max = Math.max(lastPoint.y, event.point.y);
var points = chart.series[0].data;
$.each(points, function (i,p) {
if (p.x >= x_min && p.x <= x_max && p.y >= y_min && p.y <= y_max) {
p.select(true, true);
}
});
} else {
event.point.select(null,false);
}
var selectedPoints = chart.getSelectedPoints();
var selectedWells = selectedPoints.map(function(a) {return a.Well;});
var id = chart.container.parentNode.id;
Shiny.onInputChange(id + '_selected', selectedWells);
}
")
)) %>%
hc_colorAxis(minColor = '#ece7f2', maxColor = '#045a8d')
}
hmplateUI <- function(id) {
ns <- NS(id)
tagList(
tags$script(sprintf("
Shiny.addCustomMessageHandler('hmplate_clearSelectedCallbackHandler',
function(dummy){Shiny.onInputChange('%s' + '_selected', null);}
)
", ns('hmplate'))),
highchartOutput(ns('hmplate'))
)
}
hmplate <- function(input, output, session,
data, nrow = 8, ncol = 12) {
output$hmplate <- renderHighchart({
validate(need(data(), ''))
data() %>%
hmplate_formatData(nrow, ncol) %>%
hmplate_buildHeatmap()
})
observeEvent(data(), {
session$sendCustomMessage(type = "hmplate_clearSelectedCallbackHandler", '')
})
selected_data <- reactive({
if(is.null(input$hmplate_selected)) return(NULL)
isolate({
out <- data() %>%
filter(Well %in% input$hmplate_selected)
if(nrow(out) == 0) return(NULL)
return(out)
})
})
return(selected_data)
}