Skip to content

Commit c84b66a

Browse files
committed
Allow NULL labels
1 parent 0a5b739 commit c84b66a

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# ggtibble 1.0.0.9000
22

3+
* `labs` argument to `ggtibble()` can now include `NULL (#6)
34
* `guides()` can now be added to `gglist` objects.
45
* Labels created with the `labs` argument to `ggtibble()` will not longer all be
56
the same (#3)

R/labs_glue.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ labs_glue <- function(p, ...) {
1515
labs_args <- list()
1616
for (nm in names(args)) {
1717
labs_args[[nm]] <- glue::glue_data(p, args[[nm]])
18+
if (length(labs_args[[nm]]) == 0) {
19+
# handle NULL inputs
20+
labs_args[[nm]] <- rep(list(labs_args[[nm]]), nrow(p))
21+
}
1822
}
1923
purrr::pmap(.l = labs_args, .f = ggplot2::labs)
2024
}

tests/testthat/test-ggtibble.R

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,35 @@ test_that("ggtibble", {
6565
regexp = "Unary operations are not defined for ggtibble objects",
6666
fixed = TRUE
6767
)
68-
v5 <- v4 + ggplot2::geom_point()
68+
v5 <-
69+
ggtibble(
70+
data.frame(A = 1:2, B = 3:4),
71+
ggplot2::aes(x = B, y = B),
72+
outercols = "A",
73+
labs = list(x = "A is {A}")
74+
) +
75+
ggplot2::geom_point()
6976
expect_equal(nrow(v5), 2)
7077
expect_equal(v4$figure[[1]]$layers, list())
7178
expect_s3_class(v5$figure[[1]]$layers[[1]]$geom, "GeomPoint")
79+
80+
# NULL labels work (#6)
81+
v6 <-
82+
ggtibble(
83+
data.frame(A = 1:2, B = 3:4),
84+
ggplot2::aes(x = B, y = B),
85+
outercols = "A",
86+
labs = list(x = "A is {A}", y = NULL)
87+
) +
88+
ggplot2::geom_point()
89+
expect_equal(
90+
v6$figure[[1]]$labels$x,
91+
"A is 1"
92+
)
93+
expect_equal(
94+
v6$figure[[1]]$labels$y,
95+
character(0)
96+
)
7297
})
7398

7499
test_that("knit_print.ggtibble", {

tests/testthat/test-labs_glue.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,11 @@ test_that("labs_glue", {
2222
ggplot2::labs(y = "B")
2323
)
2424
)
25+
expect_equal(
26+
labs_glue(d_ggtibble, x = NULL, y = "{A}"),
27+
list(
28+
ggplot2::labs(x = character(0), y = "A"),
29+
ggplot2::labs(x = character(0), y = "B")
30+
)
31+
)
2532
})

0 commit comments

Comments
 (0)