-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.R
177 lines (168 loc) · 4.54 KB
/
app.R
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
# Librairies ----
library(shiny)
library(shinyBS)
library(shinydashboard)
library(shinyWidgets)
library(BBmisc)
library(d3heatmap)
library(htmlwidgets)
library(RColorBrewer)
library(gtools)
# Import dataset ----
data_table <- t(as.matrix(read.table("inputdata.tsv", header=TRUE, check.names=FALSE, row.names=1, sep="\t")))
data_table <- na.replace(data_table,0)
# User interface ----
ui <- dashboardPage(
dashboardHeader(title="Shiny Heatmap"),
dashboardSidebar(
fluidRow(
# Center & Scale
h5(strong("Center and scale :")),
switchInput(
inputId = "scale",
label = "Center and Scale",
value = TRUE,
onLabel = "YES",
offLabel = "NO"
)
),
fluidRow(
# Range
h5(strong("Range between 0 an 1 :")),
switchInput(
inputId = "range",
label = "Range",
value = FALSE,
onLabel = "YES",
offLabel = "NO"
)
),
hr(),
fluidRow(
# Dissimilarity method
selectInput(
inputId = "dsmlrt",
label = "Dissimilarity method :",
choices = c("euclidean","maximum","manhattan","canberra","binary","minkowski","1-correlation","1-abs(correlation)"), # + binary ne marche pas
selected = "euclidean"
)
),
fluidRow(
conditionalPanel(
condition = "input.dsmlrt == '1-correlation' || input.dsmlrt == '1-abs(correlation)'",
# Correlation Method
selectInput(
inputId = "cor",
label = "Correlation Method :",
choices = c("pearson","spearman","kendall"),
selected = "pearson"
)
)
),
fluidRow(
# Agglomeration method
selectInput(
inputId = "agglo",
label = "Agglomeration method :",
choices = c("ward.D","single","complete","average","mcquitty","median","centroid"),
selected = "ward.D"
)
),
hr(),
fluidRow(
# Download Heatmap
downloadButton(
outputId = "download_html",
label = "Download"
),
bsPopover(
id = "export_png",
title = "",
content = "Export the heatmap in PNG file.",
placement = "bottom",
trigger = "hover",
options = NULL
)
)
),
dashboardBody(
includeCSS("styles.css"),
fluidRow(
column(4,
# Row clusters
numericInput(
inputId = "krow",
label = "Number of sample clusters to identify :",
value = 1,
min = 1,
max = 10,
step = 1
)
),
column(4,
# Col clusters
numericInput(
inputId = "kcol",
label = "Number of variable clusters to identify :",
value = 1,
min = 1,
max = 10,
step = 1
)
),
column(4,
# Color palette
selectInput(
inputId = "color",
label = "Color palette :",
choices = c("Blues","YellowBlue",RedBlue="RdYlBu"),
selected = "RedBlue"
)
)
),
fluidRow(
d3heatmapOutput('HEATMAP_d3')
)
)
)
# Server logic ----
server <- function(input, output) {
output$download_html <- downloadHandler(
filename = "heatmap.html",
content = function(file){
saveWidget(displayed_heatmap(), file)
}
)
displayed_heatmap <- reactive({
if(input$dsmlrt %in% c("euclidean", "maximum", "manhattan", "canberra", "binary", "minkowski")) {
hcl_row <- hclust(dist(data_table, method = input$dsmlrt), method = input$agglo)
hcl_col <- hclust(dist(t(data_table), method = input$dsmlrt), method = input$agglo)
} else if(input$dsmlrt == "1-correlation") {
hcl_row <- hclust(as.dist(1-cor(t(data_table), method = input$cor, use = "pairwise.complete.obs")), method = input$agglo)
hcl_col <- hclust(as.dist(1-cor(data_table, method = input$cor, use = "pairwise.complete.obs")), method = input$agglo)
} else if(input$dsmlrt == "1-abs(correlation)") {
hcl_row <- hclust(as.dist(1-abs(cor(t(data_table), method = input$cor, use = "pairwise.complete.obs"))), method = input$agglo)
hcl_col <- hclust(as.dist(1-abs(cor(data_table, method = input$cor, use = "pairwise.complete.obs"))), method = input$agglo)
}
if(input$scale){
data_table <- scale(data_table, center = TRUE, scale = TRUE)
}
if(input$range){
data_table <- normalize(data_table, method="range")
}
if(input$color == "YellowBlue"){
color_palette <- c("#FFCE00","#FFFFFF","#6B8BA3")
} else {
color_palette <- input$color
}
d3heatmap <- d3heatmap(data_table, scale = "none", Rowv = as.dendrogram(hcl_row), Colv = as.dendrogram(hcl_col), dendrogram = "both", k_row = input$krow, k_col = input$kcol, colors = color_palette)
})
output$HEATMAP_d3 <- renderD3heatmap({
heatmap <- displayed_heatmap()
return(heatmap)
})
}
# Run app ----
options(shiny.host="0.0.0.0")
options(shiny.port=8765)
shinyApp(ui, server)