Skip to content

Commit f8ed252

Browse files
committed
export labels class
1 parent fece790 commit f8ed252

File tree

5 files changed

+61
-17
lines changed

5 files changed

+61
-17
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ export(calc_element)
291291
export(check_device)
292292
export(class_ggplot)
293293
export(class_ggplot_built)
294+
export(class_labels)
294295
export(class_mapping)
295296
export(class_theme)
296297
export(combine_vars)

R/labels.R

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -175,22 +175,49 @@ setup_plot_labels <- function(plot, layers, data) {
175175
#' p +
176176
#' labs(title = "title") +
177177
#' labs(title = NULL)
178-
labs <- S7::new_class(
179-
"labels", parent = S7::new_S3_class("gg"),
180-
constructor = function(..., title = waiver(), subtitle = waiver(),
181-
caption = waiver(), tag = waiver(), dictionary = waiver(),
182-
alt = waiver(), alt_insight = waiver()) {
183-
# .ignore_empty = "all" is needed to allow trailing commas, which is NOT a trailing comma for dots_list() as it's in ...
184-
args <- dots_list(..., title = title, subtitle = subtitle, caption = caption,
185-
tag = tag, alt = allow_lambda(alt), alt_insight = alt_insight,
186-
dictionary = dictionary, .ignore_empty = "all")
178+
labs <- function(..., title = waiver(), subtitle = waiver(),
179+
caption = waiver(), tag = waiver(), dictionary = waiver(),
180+
alt = waiver(), alt_insight = waiver()) {
181+
# .ignore_empty = "all" is needed to allow trailing commas, which is NOT a trailing comma for dots_list() as it's in ...
182+
args <- dots_list(..., title = title, subtitle = subtitle, caption = caption,
183+
tag = tag, alt = allow_lambda(alt), alt_insight = alt_insight,
184+
dictionary = dictionary, .ignore_empty = "all")
185+
186+
is_waive <- vapply(args, is.waiver, logical(1))
187+
args <- args[!is_waive]
188+
# remove duplicated arguments
189+
args <- args[!duplicated(names(args))]
190+
args <- rename_aes(args)
191+
class_labels(args)
192+
}
187193

188-
is_waive <- vapply(args, is.waiver, logical(1))
189-
args <- args[!is_waive]
190-
# remove duplicated arguments
191-
args <- args[!duplicated(names(args))]
192-
args <- rename_aes(args)
193-
S7::new_object(args)
194+
#' The labels class
195+
#'
196+
#' The labels class holds a list with label information to display as titles
197+
#' of plot components. The preferred way to construct an object of the labels
198+
#' class is to use the [`labs()`] function.
199+
#'
200+
#' @param labels A named list.
201+
#'
202+
#' @export
203+
class_labels <- S7::new_class(
204+
"labels", parent = S7::new_S3_class("gg"),
205+
constructor = function(labels) {
206+
S7::new_object(labels)
207+
},
208+
validator = function(self) {
209+
if (!is.list(self)) {
210+
return("labels must be a list.")
211+
}
212+
if (!is_named2(self)) {
213+
return("every label must be named.")
214+
}
215+
dups <- unique(names(self)[duplicated(names(self))])
216+
if (length(dups) > 0) {
217+
dups <- oxford_comma(dups, final = "and")
218+
return(paste0("labels cannot contain duplicate names (", dups, ")."))
219+
}
220+
return(NULL)
194221
}
195222
)
196223

R/plot-construction.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ S7::method(ggplot_add, list(class_scale, class_ggplot)) <-
147147
plot
148148
}
149149

150-
S7::method(ggplot_add, list(labs, class_ggplot)) <-
150+
S7::method(ggplot_add, list(class_labels, class_ggplot)) <-
151151
function(object, plot, ...) { update_labels(plot, object) }
152152

153153
S7::method(ggplot_add, list(class_guides, class_ggplot)) <-

R/plot.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class_ggplot <- S7::new_class(
3434
coordinates = class_coord,
3535
facet = class_facet,
3636
layout = class_layout,
37-
labels = labs,
37+
labels = class_labels,
3838
plot_env = S7::class_environment
3939
),
4040
constructor = function(data = waiver(), layers = list(), scales = NULL,

man/class_labels.Rd

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

0 commit comments

Comments
 (0)