Skip to content

Commit 073696f

Browse files
authored
Merge pull request #5 from billdenney/export-more
export `new_gglist()`
2 parents 42bb522 + d784bca commit 073696f

File tree

7 files changed

+90
-1
lines changed

7 files changed

+90
-1
lines changed

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ export(ggtibble)
2020
export(group_vars)
2121
export(knit_print)
2222
export(labs_glue)
23+
export(new_gglist)
24+
export(new_ggtibble)
2325
export(vec_arith)
2426
importFrom(dplyr,group_vars)
2527
importFrom(ggplot2,ggplot)

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22

33
* Labels created with the `labs` argument to `ggtibble()` will not longer all be
44
the same (#3)
5+
* `new_gglist()` and `new_ggtibble()` are now exported making it easier to
6+
create objects.

R/gglist.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ gglist <- function(data = NULL, mapping = ggplot2::aes(), ..., environment = par
1818
)
1919
}
2020

21+
#' Create a new `gglist` object
22+
#'
23+
#' @param x A list of ggplot2 objects to convert into a gglist
24+
#' @returns The list verified to be a gglist and with the gglist class
25+
#' @family New ggtibble objects
26+
#' @examples
27+
#' new_gglist(list(NULL, ggplot2::ggplot(data = data.frame())))
28+
#' @export
2129
new_gglist <- function(x = list()) {
2230
if (!inherits(x, "list")) {
2331
stop("`x` must be a list")

R/ggtibble.R

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ ggtibble.data.frame <- function(data, mapping = ggplot2::aes(), ..., outercols =
5555
d_plot <- tidyr::nest(.data = data, data_plot = !tidyr::all_of(outercols))
5656
d_plot$figure <- gglist(data = d_plot$data_plot, mapping = mapping, ...)
5757
d_plot$caption <- glue::glue_data(d_plot, caption)
58-
class(d_plot) <- c("ggtibble", class(d_plot))
58+
d_plot <- new_ggtibble(d_plot)
5959
if (length(labs) > 0) {
6060
d_plot$figure <-
6161
d_plot$figure +
@@ -67,6 +67,25 @@ ggtibble.data.frame <- function(data, mapping = ggplot2::aes(), ..., outercols =
6767
d_plot
6868
}
6969

70+
#' Create a new `ggtibble` object
71+
#'
72+
#' @param x A data.frame with a column named "figure" and "caption", and where
73+
#' the "figure" column is a ggtibble.
74+
#' @returns The object with a ggtibble class
75+
#' @family New ggtibble objects
76+
#' @examples
77+
#' new_ggtibble(tibble::tibble(figure = list(ggplot2::ggplot()), caption = ""))
78+
#' @export
79+
new_ggtibble <- function(x) {
80+
stopifnot(is.data.frame(x))
81+
stopifnot(c("figure", "caption") %in% names(x))
82+
if (!inherits(x$figure, "gglist")) {
83+
x$figure <- new_gglist(x$figure)
84+
}
85+
class(x) <- unique(c("ggtibble", class(x)))
86+
x
87+
}
88+
7089
#' @describeIn knit_print.gglist Print the plots in a `ggtibble` object
7190
#' @export
7291
knit_print.ggtibble <- function(x, ...) {

man/new_gglist.Rd

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/new_ggtibble.Rd

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-ggtibble.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,10 @@ test_that("labels are not always the same (#3)", {
141141

142142
expect_true(fig1$labels$x != fig2$labels$x)
143143
})
144+
145+
test_that("new_ggtibble() works", {
146+
expect_s3_class(
147+
new_ggtibble(tibble::tibble(figure = list(ggplot2::ggplot()), caption = "")),
148+
"ggtibble"
149+
)
150+
})

0 commit comments

Comments
 (0)