@@ -175,22 +175,49 @@ setup_plot_labels <- function(plot, layers, data) {
175
175
# ' p +
176
176
# ' labs(title = "title") +
177
177
# ' 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
+ }
187
193
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 )
194
221
}
195
222
)
196
223
0 commit comments