diff --git a/R/htanno-.R b/R/htanno-.R index 55d1b26e..ccbce764 100644 --- a/R/htanno-.R +++ b/R/htanno-.R @@ -188,7 +188,10 @@ HtannoProto <- ggplot2::ggproto("HtannoProto", # otherwise, they won't be collected. extra_params = character(), setup_params = function(self) .subset2(self, "params"), - setup_data = function(self) .subset2(self, "data"), + + # use `NULL`, if you don't need any data from the heatmap + # use `.subset2(self, "data")` if you want to attach the matrix + setup_data = function(self) NULL, # Following fields should be defined for the new `Htanno` object. # argument name in the function doesn't matter. diff --git a/R/htanno-gg.R b/R/htanno-gg.R index 8e0c8535..02414b5b 100644 --- a/R/htanno-gg.R +++ b/R/htanno-gg.R @@ -44,6 +44,18 @@ gganno_left <- function(...) htanno_gg(position = "left", ...) gganno_right <- function(...) htanno_gg(position = "right", ...) HtannoGG <- ggplot2::ggproto("HtannoGG", HtannoProto, + setup_data = function(self) { + data <- .subset2(self, "data") + # matrix: will be reshaped to the long-format data.frame + # data.frame: won't do any thing special + if (is.matrix(data)) { + data <- melt_matrix(data) + } else { + data <- as_tibble0(data, rownames = ".row_names") + data$.row_index <- seq_len(nrow(data)) + } + data + }, ggplot = function(self, mapping) { ans <- ggplot2::ggplot(mapping = mapping) + ggplot2::theme_bw() @@ -57,14 +69,6 @@ HtannoGG <- ggplot2::ggproto("HtannoGG", HtannoProto, draw = function(self, panels, index) { data <- .subset2(self, "data") axis <- to_coord_axis(.subset2(self, "position")) - # matrix: will be reshaped to the long-format data.frame - # data.frame: won't do any thing special - if (is.matrix(data)) { - data <- melt_matrix(data) - } else { - data <- as_tibble0(data, rownames = ".row_names") - data$.row_index <- seq_len(nrow(data)) - } coords <- data_frame0(.panel = panels[index], .index = index) coords[[paste0(".", axis)]] <- seq_along(index) data <- merge(data, coords, diff --git a/R/htanno-reorder.R b/R/htanno-reorder.R index b9b784be..ee664a4a 100644 --- a/R/htanno-reorder.R +++ b/R/htanno-reorder.R @@ -38,6 +38,7 @@ htanno_reorder <- function(fun = rowMeans, ..., strict = TRUE, } HtannoReorder <- ggplot2::ggproto("HtannoReorder", HtannoProto, + setup_data = function(self) .subset2(self, "data"), compute = function(self, panels, index, fun, fun_params, strict) { data <- .subset2(self, "data") position <- .subset2(self, "position") diff --git a/R/initialize-htanno.R b/R/initialize-htanno.R index 30192207..0f31d34e 100644 --- a/R/initialize-htanno.R +++ b/R/initialize-htanno.R @@ -66,9 +66,12 @@ initialize_htanno_layout <- function(object, heatmap, object_name) { params <- object$setup_params() object$params <- params - # prepare data -------------------------------------- - data <- object$setup_data() - object$data <- data + # rows are observations, this data came from `htanno_setup_data`, which can + # be used to check the axis dimention + data <- .subset2(object, "data") + + # set up data -------------------------------------- + object$data <- object$setup_data() # prepare old layout -------------------------------- old_panels <- slot(heatmap, paste0(axis, "_panels")) diff --git a/tests/testthat/_snaps/htanno/dendro.png b/tests/testthat/_snaps/htanno/dendro.png index 91e52c60..d9b27ce1 100644 Binary files a/tests/testthat/_snaps/htanno/dendro.png and b/tests/testthat/_snaps/htanno/dendro.png differ diff --git a/tests/testthat/_snaps/htanno/dendro_cutree.png b/tests/testthat/_snaps/htanno/dendro_cutree.png index 4e63f5d4..581a2ef1 100644 Binary files a/tests/testthat/_snaps/htanno/dendro_cutree.png and b/tests/testthat/_snaps/htanno/dendro_cutree.png differ diff --git a/tests/testthat/_snaps/htanno/dendro_left_between_group_reorder.png b/tests/testthat/_snaps/htanno/dendro_left_between_group_reorder.png index 7304a5b3..dcd78e65 100644 Binary files a/tests/testthat/_snaps/htanno/dendro_left_between_group_reorder.png and b/tests/testthat/_snaps/htanno/dendro_left_between_group_reorder.png differ diff --git a/tests/testthat/_snaps/htanno/dendro_top_between_group.png b/tests/testthat/_snaps/htanno/dendro_top_between_group.png index b4745746..883463aa 100644 Binary files a/tests/testthat/_snaps/htanno/dendro_top_between_group.png and b/tests/testthat/_snaps/htanno/dendro_top_between_group.png differ