Skip to content

Commit

Permalink
simplify codes
Browse files Browse the repository at this point in the history
  • Loading branch information
Yunuuuu committed Aug 3, 2024
1 parent 0b2c112 commit cfc86e5
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 54 deletions.
26 changes: 6 additions & 20 deletions R/align-group.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,15 @@ align_group <- function(group, set_context = FALSE, name = NULL) {

AlignGroup <- ggplot2::ggproto("AlignGroup", Align,
setup_params = function(self, data, params) {
if (nrow(data) != length(group <- .subset2(params, "group"))) {
cli::cli_abort(paste(
"{.arg group} of {.fn {snake_class(self)}} must be",
sprintf(
"the same length of layout %s-axis (%d)",
to_matrix_axis(.subset2(self, "direction")),
nrow(data)
)
), call = .subset2(self, "call"))
}
assert_mismatch_nobs(self, nrow(data),
length(.subset2(params, "group")),
arg = "group",
msg = "must be an atomic vector"
)
params
},
layout = function(self, panels, index, group) {
if (!is.null(panels)) {
direction <- .subset2(self, "direction")
cli::cli_abort(c(
"{.fn {snake_class(self)}} cannot do sub-split",
i = sprintf(
"group of layout %s-axis already exists",
to_matrix_axis(direction)
)
), call = self$call)
}
assert_sub_split(self, panels)
list(group, index)
}
)
11 changes: 1 addition & 10 deletions R/align-kmeans.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,7 @@ AlignKmeans <- ggplot2::ggproto("AlignKmeans", Align,
stats::kmeans(data, centers, iter.max, nstart, algorithm, trace)
},
layout = function(self, panels, index) {
if (!is.null(panels)) {
direction <- .subset2(self, "direction")
cli::cli_abort(c(
"{.fn {snake_class(self)}} cannot do sub-split",
i = sprintf(
"group of layout %s-axis already exists",
to_matrix_axis(direction)
)
), call = self$call)
}
assert_sub_split(self, panels)
list(.subset2(.subset2(self, "statistics"), "cluster"), index)
}
)
25 changes: 5 additions & 20 deletions R/align-reorder.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,12 @@ AlignReorder <- ggplot2::ggproto("AlignReorder", Align,
setup_data = function(self, data, params) data,
compute = function(self, panels, index, fun, fun_params, strict) {
data <- .subset2(self, "data")
direction <- .subset2(self, "direction")
if (!is.null(panels) && strict) {
axis <- to_matrix_axis(direction)
cli::cli_abort(c(
paste(
"{.fn {snake_class(self)}} cannot reordering {axis}-axis",
"since group of layout {axis}-axis exists"
),
i = "try to set `strict = FALSE` to reorder within each group"
), call = .subset2(self, "call"))
}
assert_reorder(self, panels, strict)
weights <- rlang::inject(fun(data, !!!fun_params))
if (nrow(data) != length(weights)) {
cli::cli_abort(paste(
"{.arg fun} of {.fn {snake_class(self)}} must return an atomic",
sprintf(
"vector with the same length of %s-axis (%d)",
to_matrix_axis(direction), nrow(data)
)
), call = .subset2(self, "call"))
}
assert_mismatch_nobs(self,
nrow(data), length(weights),
msg = "must return an atomic vector"
)
weights
},
layout = function(self, panels, index, decreasing) {
Expand Down
9 changes: 5 additions & 4 deletions R/align-title.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ AlignTitle <- ggplot2::ggproto("AlignTitle", Align,
titles <- as.character(data$.panel)
} else if (length(titles) != nlevels(panels)) {
cli::cli_abort(
sprintf(
"{.arg titles} must be of length of %s %s %s (%d)",
"the same number with heatmap",
to_matrix_axis(position), "panels", nlevels(panels)
paste(
"{.arg titles} must be of length of",
"the same number of the layout",
to_matrix_axis(direction), "-axis panels (",
nlevels(panels), ")"
),
call = .subset2(self, "call")
)
Expand Down
37 changes: 37 additions & 0 deletions R/utils-assert.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,40 @@ assert_mapping <- function(mapping, arg = caller_arg(mapping),
), call = call)
}
}

assert_mismatch_nobs <- function(align, n, nobs, msg, arg) {
if (n != nobs) {
cli::cli_abort(paste(
"{.arg {arg}} of {.fn {snake_class(align)}}", msg,
sprintf(
"the same length of layout %s-axis (%d)",
to_coord_axis(.subset2(align, "direction")), n
)
), call = .subset2(align, "call"))
}
}

assert_sub_split <- function(align, panels) {
if (!is.null(panels)) {
cli::cli_abort(c(
"{.fn {snake_class(align)}} cannot do sub-split",
i = sprintf(
"Group of layout %s-axis already exists",
to_coord_axis(.subset2(align, "direction"))
)
), call = .subset2(align, "call"))
}
}

assert_reorder <- function(align, panels, strict) {
if (!is.null(panels) && strict) {
cli::cli_abort(c(
"{.fn {snake_class(align)}} cannot reordering {axis}-axis",
i = sprintf(
"Group of layout %s-axis already exists",
to_coord_axis(.subset2(align, "direction"))
),
i = "try to set {.code strict = FALSE} to reorder within each group"
), call = .subset2(align, "call"))
}
}

0 comments on commit cfc86e5

Please sign in to comment.