@@ -29,10 +29,123 @@ drawGrid = TRUE,
29
29
gridColor = " #808080" ,
30
30
gridThickness = 0.1 ,
31
31
drawValues = FALSE ,
32
- width = NULL , height = NULL ,
32
+ width = NULL ,
33
+ height = NULL ,
34
+ ...
35
+ ) {
36
+ payload <- create.payload(x ,
37
+ labRow = labRow ,
38
+ labCol = labCol ,
39
+ Rowv = Rowv ,
40
+ Colv = Colv ,
41
+ distfun = distfun ,
42
+ hclustfun = hclustfun ,
43
+ dendrogram = dendrogram ,
44
+ reorderfun = reorderfun ,
45
+ symm = symm ,
46
+ na.rm = na.rm ,
47
+ rowAnnotations = rowAnnotations ,
48
+ columnAnnotations = columnAnnotations ,
49
+ colorScheme = colorScheme ,
50
+ rowSize = rowSize ,
51
+ columnSize = columnSize ,
52
+ drawGrid = drawGrid ,
53
+ gridColor = gridColor ,
54
+ gridThickness = gridThickness ,
55
+ drawValues = drawValues ,
56
+ width = width ,
57
+ height = height ,
58
+ ... )
59
+ htmlwidgets :: createWidget(
60
+ name = ' morpheus' ,
61
+ payload ,
62
+ width = width ,
63
+ height = height ,
64
+ package = ' morpheus' ,
65
+ sizingPolicy = htmlwidgets :: sizingPolicy(browser.fill = TRUE )
66
+ )
67
+ }
68
+
69
+ morpheus.toJSON <- function (x ,
70
+ labRow = rownames(x ),
71
+ labCol = colnames(x ),
72
+
73
+ # dendrogram control
74
+ Rowv = TRUE ,
75
+ Colv = if (symm )" Rowv" else TRUE ,
76
+ distfun = dist ,
77
+ hclustfun = hclust ,
78
+ dendrogram = c(" both" , " row" , " column" , " none" ),
79
+ reorderfun = function (d , w ) reorder(d , w ),
80
+ symm = FALSE ,
81
+ na.rm = TRUE ,
82
+ rowAnnotations = NULL ,
83
+ columnAnnotations = NULL ,
84
+ colorScheme = NULL ,
85
+ rowSize = 13 ,
86
+ columnSize = 13 ,
87
+ drawGrid = TRUE ,
88
+ gridColor = " #808080" ,
89
+ gridThickness = 0.1 ,
90
+ drawValues = FALSE ,
91
+ width = NULL ,
92
+ height = NULL ,
33
93
...
34
94
) {
35
95
96
+ payload <- create.payload(x ,
97
+ labRow = labRow ,
98
+ labCol = labCol ,
99
+ Rowv = Rowv ,
100
+ Colv = Colv ,
101
+ distfun = distfun ,
102
+ hclustfun = hclustfun ,
103
+ dendrogram = dendrogram ,
104
+ reorderfun = reorderfun ,
105
+ symm = symm ,
106
+ na.rm = na.rm ,
107
+ rowAnnotations = rowAnnotations ,
108
+ columnAnnotations = columnAnnotations ,
109
+ colorScheme = colorScheme ,
110
+ rowSize = rowSize ,
111
+ columnSize = columnSize ,
112
+ drawGrid = drawGrid ,
113
+ gridColor = gridColor ,
114
+ gridThickness = gridThickness ,
115
+ drawValues = drawValues ,
116
+ width = width ,
117
+ height = height ,
118
+ ... )
119
+ jsonlite :: toJSON(payload $ options ,dataframe = " columns" , null = " null" , na = " null" , auto_unbox = TRUE ,
120
+ digits = getOption(" shiny.json.digits" , 16 ), use_signif = TRUE , force = TRUE ,
121
+ POSIXt = " ISO8601" , UTC = TRUE , rownames = FALSE , keep_vec_names = TRUE ,
122
+ strict_atomic = TRUE )
123
+ }
124
+
125
+ create.payload <- function (x ,
126
+ labRow = rownames(x ),
127
+ labCol = colnames(x ),
128
+ Rowv = TRUE ,
129
+ Colv = if (symm )" Rowv" else TRUE ,
130
+ distfun = dist ,
131
+ hclustfun = hclust ,
132
+ dendrogram = c(" both" , " row" , " column" , " none" ),
133
+ reorderfun = function (d , w ) reorder(d , w ),
134
+ symm = FALSE ,
135
+ na.rm = TRUE ,
136
+ rowAnnotations = NULL ,
137
+ columnAnnotations = NULL ,
138
+ colorScheme = NULL ,
139
+ rowSize = 13 ,
140
+ columnSize = 13 ,
141
+ drawGrid = TRUE ,
142
+ gridColor = " #808080" ,
143
+ gridThickness = 0.1 ,
144
+ drawValues = FALSE ,
145
+ width = NULL ,
146
+ height = NULL ,
147
+ ...
148
+ ) {
36
149
name <- deparse(substitute(x ))
37
150
# # x is a matrix!
38
151
if (! is.matrix(x )) {
@@ -170,24 +283,23 @@ width = NULL, height = NULL,
170
283
morpheusOptions $ gridThickness <- gridThickness
171
284
morpheusOptions $ drawValues <- drawValues
172
285
173
-
174
286
x <- x [rowInd , colInd ]
175
287
if (! is.null(morpheusOptions $ colorScheme $ colors )) {
176
288
morpheusOptions $ colorScheme $ colors <- lapply(morpheusOptions $ colorScheme $ colors , function (color ){
177
289
rgb <- col2rgb(color )
178
290
paste(" rgb(" , rgb [1 ], " ," , rgb [2 ], " ," , rgb [3 ], " )" , sep = ' ' )
179
291
})
180
292
}
181
-
182
- if (! is.null(morpheusOptions $ colorScheme $ colors ) && is.null(morpheusOptions $ colorScheme $ values )) {
293
+
294
+ if (! is.null(morpheusOptions $ colorScheme $ colors ) && is.null(morpheusOptions $ colorScheme $ values )) {
183
295
rng = range(x )
184
296
nvals <- length(morpheusOptions $ colorScheme $ colors )
185
297
fractionStep <- 1 / (nvals - 1 )
186
298
values <- vector(" list" , nvals )
187
299
dataRange <- rng [2 ] - rng [1 ]
188
300
values [1 ] <- rng [1 ]
189
301
values [nvals ] <- rng [2 ]
190
-
302
+
191
303
for (i in 2 : nvals - 1 ) {
192
304
fraction <- fractionStep * (i - 1 )
193
305
values [i ] <- rng [1 ] + fraction * dataRange
@@ -202,20 +314,33 @@ width = NULL, height = NULL,
202
314
is.dendrogram(ddr ) &&
203
315
dendrogram %in% c(" both" , " row" )) dendToTree(ddr ) else NULL
204
316
205
- payload <- list (rows = nrow(x ), rowDendrogram = rowDendrogram , columnDendrogram = columnDendrogram , columns = ncol(x ), name = name ,
206
- array = x , rowNames = rownames(x ), columnNames = colnames(x ), rowAnnotations = rowAnnotations , columnAnnotations = columnAnnotations , options = morpheusOptions )
207
- htmlwidgets :: createWidget(
208
- name = ' morpheus' ,
209
- payload ,
210
- width = width ,
211
- height = height ,
212
- package = ' morpheus' ,
213
- sizingPolicy = htmlwidgets :: sizingPolicy(browser.fill = TRUE )
214
- )
215
-
216
317
217
- }
218
318
319
+ rowVectors <- list ()
320
+ rowVectors [[1 ]] = list (name = ' id' , array = rownames(x ))
321
+
322
+ if (! is.null(rowAnnotations )) {
323
+ for (i in 1 : ncol(rowAnnotations )) {
324
+ rowVectors [[i + 1 ]] = list (name = names(rowAnnotations )[i ], array = rowAnnotations [,i ])
325
+ }
326
+ }
327
+ rowMetadataModel <- list (vectors = rowVectors )
328
+ columnVectors <- list ()
329
+ columnVectors [[1 ]] = list (name = ' id' , array = colnames(x ))
330
+ if (! is.null(columnAnnotations )) {
331
+ for (i in 1 : ncol(columnAnnotations )) {
332
+ columnVectors [[i + 1 ]] = list (name = names(columnAnnotations )[i ], array = columnAnnotations [,i ])
333
+ }
334
+ }
335
+ columnMetadataModel <- list (vectors = columnVectors )
336
+ dataset <- list (seriesNames = list (name ), rows = nrow(x ), columns = ncol(x ), seriesDataTypes = list (' number' ),
337
+ seriesArrays = list (x ), rowMetadataModel = rowMetadataModel , columnMetadataModel = columnMetadataModel )
338
+
339
+ morpheusOptions $ dataset = dataset
340
+ morpheusOptions $ name = name
341
+ payload <- list (rowDendrogram = rowDendrogram , columnDendrogram = columnDendrogram , options = morpheusOptions )
342
+ return (payload )
343
+ }
219
344
220
345
is.dendrogram <- function (x ) { inherits(x , " dendrogram" )}
221
346
0 commit comments