16
16
# ' @param data A data frame to pivot.
17
17
# ' @param cols <[`tidy-select`][tidyr_tidy_select]> Columns to pivot into
18
18
# ' longer format.
19
- # ' @param names_to A string specifying the name of the column to create
20
- # ' from the data stored in the column names of `data`.
19
+ # ' @param names_to A character vector specifying the new column or columns to
20
+ # ' create from the information stored in the column names of `data` specified
21
+ # ' by `cols`.
21
22
# '
22
- # ' Can be a character vector, creating multiple columns, if `names_sep`
23
- # ' or `names_pattern` is provided. In this case, there are two special
24
- # ' values you can take advantage of:
23
+ # ' * If length 0, or if `NULL` is supplied, no columns will be created.
25
24
# '
26
- # ' * `NA` will discard that component of the name.
27
- # ' * `.value` indicates that component of the name defines the name of the
28
- # ' column containing the cell values, overriding `values_to`.
25
+ # ' * If length 1, a single column will be created which will contain the
26
+ # ' column names specified by `cols`.
27
+ # '
28
+ # ' * If length >1, multiple columns will be created. In this case, one of
29
+ # ' `names_sep` or `names_pattern` must be supplied to specify how the
30
+ # ' column names should be split. There are also two additional character
31
+ # ' values you can take advantage of:
32
+ # '
33
+ # ' * `NA` will discard the corresponding component of the column name.
34
+ # '
35
+ # ' * `".value"` indicates that the corresponding component of the column
36
+ # ' name defines the name of the output column containing the cell values,
37
+ # ' overriding `values_to` entirely.
29
38
# ' @param names_prefix A regular expression used to remove matching text
30
39
# ' from the start of each variable name.
31
40
# ' @param names_sep,names_pattern If `names_to` contains multiple values,
@@ -262,7 +271,8 @@ pivot_longer_spec <- function(data,
262
271
263
272
# ' @rdname pivot_longer_spec
264
273
# ' @export
265
- build_longer_spec <- function (data , cols ,
274
+ build_longer_spec <- function (data ,
275
+ cols ,
266
276
names_to = " name" ,
267
277
values_to = " value" ,
268
278
names_prefix = NULL ,
@@ -282,30 +292,41 @@ build_longer_spec <- function(data, cols,
282
292
names <- gsub(vec_paste0(" ^" , names_prefix ), " " , names(cols ))
283
293
}
284
294
285
- if (length(names_to ) > 1 ) {
286
- if (! xor(is.null(names_sep ), is.null(names_pattern ))) {
295
+ if (is.null(names_to )) {
296
+ names_to <- character (0L )
297
+ }
298
+ if (! is.character(names_to )) {
299
+ abort(" `names_to` must be a character vector or `NULL`." )
300
+ }
301
+
302
+ n_names_to <- length(names_to )
303
+ has_names_sep <- ! is.null(names_sep )
304
+ has_names_pattern <- ! is.null(names_pattern )
305
+
306
+ if (n_names_to == 0L ) {
307
+ names <- tibble :: new_tibble(x = list (), nrow = length(names ))
308
+ } else if (n_names_to == 1L ) {
309
+ if (has_names_sep ) {
310
+ abort(" `names_sep` can't be used with a length 1 `names_to`." )
311
+ }
312
+ if (has_names_pattern ) {
313
+ names <- str_extract(names , names_to , regex = names_pattern )[[1 ]]
314
+ }
315
+
316
+ names <- tibble(!! names_to : = names )
317
+ } else {
318
+ if (! xor(has_names_sep , has_names_pattern )) {
287
319
abort(glue :: glue(
288
320
" If you supply multiple names in `names_to` you must also supply one" ,
289
321
" of `names_sep` or `names_pattern`."
290
322
))
291
323
}
292
324
293
- if (! is.null( names_sep ) ) {
325
+ if (has_names_sep ) {
294
326
names <- str_separate(names , names_to , sep = names_sep )
295
327
} else {
296
328
names <- str_extract(names , names_to , regex = names_pattern )
297
329
}
298
- } else if (length(names_to ) == 0 ) {
299
- names <- tibble :: new_tibble(x = list (), nrow = length(names ))
300
- } else {
301
- if (! is.null(names_sep )) {
302
- abort(" `names_sep` can not be used with length-1 `names_to`" )
303
- }
304
- if (! is.null(names_pattern )) {
305
- names <- str_extract(names , names_to , regex = names_pattern )[[1 ]]
306
- }
307
-
308
- names <- tibble(!! names_to : = names )
309
330
}
310
331
311
332
if (" .value" %in% names_to ) {
0 commit comments