Skip to content

Commit 0afda4e

Browse files
committed
abort for empty string
1 parent 32f9d31 commit 0afda4e

File tree

3 files changed

+65
-28
lines changed

3 files changed

+65
-28
lines changed

Diff for: NAMESPACE

+1
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ S3method(obj_print_data,ggalign_pair_links)
252252
S3method(obj_print_footer,ggalign_area)
253253
S3method(obj_print_footer,ggalign_pair_links)
254254
S3method(obj_print_header,ggalign_pair_link)
255+
S3method(obj_print_header,ggalign_pair_links)
255256
S3method(object_name,AlignGg)
256257
S3method(object_name,AlignProto)
257258
S3method(object_name,CircleLayout)

Diff for: R/pair-links.R

+58-27
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,13 @@
2020
#' @examples
2121
#' x <- pair_links(
2222
#' # group on the left hand only
23-
#' 1:2,
2423
#' c("a", "b"),
24+
#' # normally, integer index will be interpreted as the index of the
25+
#' # origianl data
26+
#' 1:2,
27+
#' # wrapped with `I()` indicate` the integer index is ordering of the
28+
#' # layout
29+
#' I(1:2),
2530
#' range_link(1, 6),
2631
#' range_link("a", "b"),
2732
#' # group on the right hand only
@@ -49,16 +54,32 @@
4954
#' @export
5055
pair_links <- function(...) {
5156
pairs <- rlang::dots_list(..., .ignore_empty = "all", .named = NULL)
52-
new_pair_links(lapply(pairs, as_pair_link, x_arg = "..."))
57+
new_pair_links(
58+
lapply(pairs, as_pair_link, x_arg = "...", call = current_call())
59+
)
5360
}
5461

5562
new_pair_links <- function(x = list(), ..., class = character()) {
5663
new_vctr(x, ..., class = c(class, "ggalign_pair_links"))
5764
}
5865

66+
#' @export
67+
obj_print_header.ggalign_pair_links <- function(x, ...) {
68+
cat("<", vec_ptype_full(x), ">", "\n", sep = "")
69+
cat(
70+
sprintf(
71+
"A total of %d pair%s of link groups",
72+
vec_size(x), if (vec_size(x) > 1L) "s" else ""
73+
),
74+
"\n",
75+
sep = ""
76+
)
77+
invisible(x)
78+
}
79+
5980
#' @export
6081
obj_print_data.ggalign_pair_links <- function(x, ...) {
61-
if (length(x) > 0L) {
82+
if (vec_size(x) > 0L) {
6283
hand1 <- vapply(x, function(hand) {
6384
deparse_link(hand, ..., hand = "hand1")
6485
}, character(1L), USE.NAMES = FALSE)
@@ -67,25 +88,31 @@ obj_print_data.ggalign_pair_links <- function(x, ...) {
6788
}, character(1L), USE.NAMES = FALSE)
6889
nms <- c("", paste0(names_or_index(x), ": "))
6990
nms <- format(nms, justify = "right")
70-
empty <- character(length(hand2))
91+
empty <- character(vec_size(hand2))
7192
empty[hand1 == "" & hand2 == ""] <- " <empty>"
7293
empty <- format(c("", empty), justify = "left")
7394
hand1 <- format(c("hand1", hand1), justify = "right")
7495
hand2 <- format(c("hand2", hand2), justify = "left")
75-
content <- paste0(" ", nms, hand1, " ~ ", hand2, empty)
76-
cat("", content, "", sep = "\n")
96+
cat("\n")
97+
cat(paste0(" ", nms, hand1, " ~ ", hand2, empty), sep = "\n")
98+
cat("\n")
7799
}
78100
invisible(x)
79101
}
80102

81103
#' @export
82104
obj_print_footer.ggalign_pair_links <- function(x, ...) {
83105
NextMethod()
106+
# `lengths`: will call `length.ggalign_pair_link()` method
84107
n <- sum(lengths(x, use.names = FALSE))
85-
cat(sprintf(
86-
"A total of %d group%s", n,
87-
if (n > 1L) "s" else ""
88-
), sep = "\n")
108+
cat(
109+
sprintf(
110+
"A total of %d link group%s", n,
111+
if (n > 1L) "s" else ""
112+
),
113+
"\n",
114+
sep = ""
115+
)
89116
invisible(x)
90117
}
91118

@@ -169,18 +196,17 @@ print.ggalign_pair_link <- function(x, ...) obj_print(x, ...)
169196

170197
#' @export
171198
obj_print_header.ggalign_pair_link <- function(x, ...) {
172-
cat(sprintf("<%s>", vec_ptype_full(x)), sep = "\n")
199+
cat(sprintf("<%s>", vec_ptype_full(x)), "\n", sep = "")
173200
invisible(x)
174201
}
175202

176203
#' @export
177204
obj_print_data.ggalign_pair_link <- function(x, ...) {
178205
if (length(x) > 0L) {
179-
content <- c(
206+
cat(c(
180207
sprintf(" hand1: %s", deparse_link(.subset2(x, "hand1"), ...)),
181208
sprintf(" hand2: %s", deparse_link(.subset2(x, "hand2"), ...))
182-
)
183-
cat(content, sep = "\n")
209+
), sep = "\n")
184210
}
185211
invisible(x)
186212
}
@@ -280,12 +306,12 @@ vec_ptype2.ggalign_range_link.ggalign_pair_link <- function(x, y, ...) {
280306

281307
#' @export
282308
vec_ptype2.ggalign_pair_link.AsIs <- function(x, y, ...) {
283-
vec_ptype2(x, remove_class(y, "AsIs"))
309+
vec_ptype2(x, remove_class(y, "AsIs"), ...)
284310
}
285311

286312
#' @export
287313
vec_ptype2.AsIs.ggalign_pair_link <- function(x, y, ...) {
288-
vec_ptype2(remove_class(x, "AsIs"), y)
314+
vec_ptype2(remove_class(x, "AsIs"), y, ...)
289315
}
290316

291317
#' @export
@@ -311,15 +337,10 @@ vec_cast.ggalign_pair_link.numeric <- function(x, to, ...,
311337
vec_cast.ggalign_pair_link.double <- vec_cast.ggalign_pair_link.numeric
312338

313339
#' @export
314-
vec_cast.ggalign_pair_link.integer <- function(x, to, ...,
315-
x_arg = caller_arg(x),
316-
to_arg = "",
317-
call = caller_env()) {
318-
new_pair_link(x)
319-
}
340+
vec_cast.ggalign_pair_link.integer <- vec_cast.ggalign_pair_link.numeric
320341

321342
#' @export
322-
vec_cast.ggalign_pair_link.character <- vec_cast.ggalign_pair_link.integer
343+
vec_cast.ggalign_pair_link.character <- vec_cast.ggalign_pair_link.numeric
323344

324345
#' @export
325346
vec_cast.ggalign_pair_link.ggalign_range_link <-
@@ -333,7 +354,11 @@ vec_cast.ggalign_pair_link.AsIs <- function(x, to, ...,
333354
x_arg = caller_arg(x),
334355
to_arg = "",
335356
call = caller_env()) {
336-
I(vec_cast(remove_class(x, "AsIs"), to, x_arg = x_arg, call = call))
357+
I(vec_cast(
358+
remove_class(x, "AsIs"),
359+
to = to, ...,
360+
x_arg = x_arg, call = call
361+
))
337362
}
338363

339364
#' @export
@@ -366,7 +391,7 @@ as_obs_link.NULL <- function(x, ...) x
366391

367392
#' @export
368393
as_obs_link.AsIs <- function(x, ...) {
369-
I(as_obs_link(remove_class(x, "AsIs")))
394+
I(as_obs_link(remove_class(x, "AsIs"), ...))
370395
}
371396

372397
#' @export
@@ -382,7 +407,13 @@ as_obs_link.integer <- as_obs_link.NULL
382407
as_obs_link.double <- as_obs_link.numeric
383408

384409
#' @export
385-
as_obs_link.character <- as_obs_link.NULL
410+
as_obs_link.character <- function(x, ..., arg = caller_arg(x),
411+
call = caller_env()) {
412+
if (any(x == "")) {
413+
cli_abort("empty string is not allowed", call = call)
414+
}
415+
x
416+
}
386417

387418
#' @export
388419
as_obs_link.waiver <- as_obs_link.NULL
@@ -469,7 +500,7 @@ deparse_link2.ggalign_pair_link <- function(x, ..., hand) {
469500

470501
#' @export
471502
deparse_link2.AsIs <- function(x, ...) {
472-
ans <- NextMethod()
503+
ans <- deparse_link2(remove_class(x, "AsIs"), ...)
473504
if (!is.null(ans)) ans <- sprintf("I(%s)", ans)
474505
ans
475506
}

Diff for: man/pair_links.Rd

+6-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)