Skip to content

Commit

Permalink
rename link_range to range_link
Browse files Browse the repository at this point in the history
  • Loading branch information
Yunuuuu committed Dec 27, 2024
1 parent c2cfc72 commit 58c4aef
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 34 deletions.
4 changes: 2 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ S3method(deparse_link,"NULL")
S3method(deparse_link,AsIs)
S3method(deparse_link,character)
S3method(deparse_link,integer)
S3method(deparse_link,link_range)
S3method(deparse_link,range_link)
S3method(deparse_link,waiver)
S3method(element_grob,element_polygon)
S3method(format,ggalign_area)
Expand Down Expand Up @@ -368,7 +368,6 @@ export(layout_annotation)
export(layout_design)
export(layout_expand)
export(layout_title)
export(link_range)
export(mark_draw)
export(mark_line)
export(mark_tetragon)
Expand All @@ -388,6 +387,7 @@ export(quad_free)
export(quad_init)
export(quad_layout)
export(quad_switch)
export(range_link)
export(raster_magick)
export(read_example)
export(scale_draw_manual)
Expand Down
39 changes: 22 additions & 17 deletions R/link.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#' Helper function to create a pair of links
#'
#' - `pair_link`: Helper function to create a pair of links.
#' - `link_range`: Helper function to create a range of observations.
#' - `range_link`: Helper function to create a range of observations.
#'
#' @param ... A list of formula, each side of the formula should be an integer
#' or character index, or a `link_range()` object to define the linked
#' observations. For integer indices, you can wrap them with [`I()`] to
#' indicate the order based on the layout. You can also use `waiver()` to
#' inherit the values from the opposite link argument.
#' or character index, or a `range_link()` object to define the linked
#' observations. You can use `NULL` to indicate none. For integer indices, you
#' can wrap them with [`I()`] to indicate the order based on the layout. You
#' can also use `waiver()` to inherit the values from the opposite link
#' argument.
#' @export
pair_links <- function(...) {
pairs <- rlang::dots_list(..., .ignore_empty = "all", .named = NULL)
Expand All @@ -24,7 +25,7 @@ pair_links <- function(...) {
)
if (!is_valid_link(link1) || !is_valid_link(link2)) {
cli_abort(c(
"Input must be either an integer or a character index, or a {.fn link_range} object.",
"Input must be either an integer or a character index, or a {.fn range_link} object.",
i = "Location: {i}"
))
}
Expand Down Expand Up @@ -105,7 +106,7 @@ is_valid_link <- function(x) {
is.null(x) ||
is.numeric(x) ||
is.character(x) ||
inherits(x, "link_range")
is_range_link(x)
}

#' @export
Expand Down Expand Up @@ -161,9 +162,9 @@ deparse_link.AsIs <- function(x, ...) {
}

#' @export
deparse_link.link_range <- function(x, ...) {
deparse_link.ggalign_range_link <- function(x, ...) {
sprintf(
"link_range(%s, %s)",
"range_link(%s, %s)",
deparse_link(.subset2(x, "point1"), ...),
deparse_link(.subset2(x, "point2"), ...)
)
Expand All @@ -177,7 +178,7 @@ deparse_link.waiver <- function(x, ...) "waiver()"
#' to indicate the ordered index by the layout.
#' @export
#' @rdname pair_links
link_range <- function(point1, point2) {
range_link <- function(point1, point2) {
if (!is_scalar(point1) ||
(!is.character(point1) && !is.numeric(point1))) {
cli_abort("{.arg ...} must be a single numeric or character index")
Expand All @@ -188,9 +189,13 @@ link_range <- function(point1, point2) {
}
point1 <- new_link(point1)
point2 <- new_link(point2)
structure(list(point1 = point1, point2 = point2), class = "link_range")
structure(list(point1 = point1, point2 = point2),
class = "ggalign_range_link"
)
}

is_range_link <- function(x) inherits(x, "ggalign_range_link")

make_link_data <- function(link, design, labels = NULL,
arg = caller_arg(link)) {
if (is.null(link)) {
Expand All @@ -200,15 +205,15 @@ make_link_data <- function(link, design, labels = NULL,
n <- .subset2(design, "nobs")
if (!inherits(link, "AsIs") || is.character(link)) {
# match the original data index
if (inherits(link, "link_range")) {
if (is_range_link(link)) {
point1 <- .subset2(link, "point1")
if (!inherits(point1, "AsIs") || is.character(point1)) {
point1 <- vec_as_location(
point1,
n = n,
names = labels,
arg = "point1",
call = quote(link_range())
call = quote(range_link())
)
}
point2 <- .subset2(link, "point2")
Expand All @@ -218,7 +223,7 @@ make_link_data <- function(link, design, labels = NULL,
n = n,
names = labels,
arg = "point2",
call = quote(link_range())
call = quote(range_link())
)
}
link <- match(c(point1, point2), .subset2(design, "index"))
Expand All @@ -233,7 +238,7 @@ make_link_data <- function(link, design, labels = NULL,
)
link <- match(link, .subset2(design, "index"))
}
} else if (inherits(link, "link_range")) {
} else if (is_range_link(link)) {
point1 <- .subset2(link, "point1")
# for character, we always match the original data
if (is.character(point1)) {
Expand All @@ -242,7 +247,7 @@ make_link_data <- function(link, design, labels = NULL,
n = n,
names = labels,
arg = "point1",
call = quote(link_range())
call = quote(range_link())
)
point1 <- match(point1, .subset2(design, "index"))
}
Expand All @@ -253,7 +258,7 @@ make_link_data <- function(link, design, labels = NULL,
n = n,
names = labels,
arg = "point2",
call = quote(link_range())
call = quote(range_link())
)
point2 <- match(point2, .subset2(design, "index"))
}
Expand Down
7 changes: 7 additions & 0 deletions R/mark.R
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ mark_tetragon <- function(..., element = NULL) {
}, ...)
}

# Not implemented completely
#' @importFrom rlang arg_match0
mark_triangle <- function(..., orientation = "plot", element = NULL) {
assert_s3_class(element, "element_polygon", allow_null = TRUE)
Expand Down Expand Up @@ -431,12 +432,18 @@ makeContent.ggalignMarkGtable <- function(x) {
y = panel_y, yend = panel_y
)
}
hand <- switch(
link,
link1 = switch_direction(direction, "left", "top"),
link2 = switch_direction(direction, "right", "bottom")
)
coords[[link]] <- lapply(seq_along(link_index), function(panel_index) {
l_index <- .subset2(link_index, panel_index)
if (is.null(l_index)) return(NULL) # styler: off
d_index <- .subset2(data_index, panel_index)
link <- vec_slice(link_coord, l_index)
link$link_index <- l_index
link$.hand <- hand
link$.index <- d_index
panel <- vec_slice(panel_coord, panel_index)
list(panel = panel, link = link)
Expand Down
9 changes: 5 additions & 4 deletions man/dot-mark_draw.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions man/mark_draw.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 8 additions & 7 deletions man/pair_links.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 58c4aef

Please sign in to comment.