Skip to content

Commit 1cd396d

Browse files
authored
Merge pull request #53 from stemangiola/fix-tidyr
fix tidyr
2 parents 2c9db6a + 3069ba2 commit 1cd396d

File tree

3 files changed

+102
-61
lines changed

3 files changed

+102
-61
lines changed

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: tidySingleCellExperiment
33
Title: Brings SingleCellExperiment to the Tidyverse
4-
Version: 1.9.1
4+
Version: 1.9.2
55
Authors@R: c(person("Stefano", "Mangiola", email = "[email protected]",
66
role = c("aut", "cre")) )
77
Description: tidySingleCellExperiment is an adapter that abstracts the 'SingleCellExperiment' container
@@ -57,7 +57,7 @@ Biarch: true
5757
biocViews: AssayDomain, Infrastructure, RNASeq, DifferentialExpression, GeneExpression, Normalization, Clustering, QualityControl, Sequencing
5858
Encoding: UTF-8
5959
LazyData: true
60-
RoxygenNote: 7.2.1
60+
RoxygenNote: 7.2.3
6161
Roxygen: list(markdown = TRUE)
6262
URL: https://github.com/stemangiola/tidySingleCellExperiment
6363
BugReports: https://github.com/stemangiola/tidySingleCellExperiment/issues

R/tidyr_methods.R

Lines changed: 60 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -326,16 +326,36 @@ extract.SingleCellExperiment <- function(data, col, into, regex="([[:alnum:]]+)"
326326
#' @param data A data frame to pivot.
327327
#' @param cols <[`tidy-select`][tidyr_tidy_select]> Columns to pivot into
328328
#' longer format.
329-
#' @param names_to A string specifying the name of the column to create
330-
#' from the data stored in the column names of `data`.
329+
#' @param cols_vary When pivoting `cols` into longer format, how should the
330+
#' output rows be arranged relative to their original row number?
331331
#'
332-
#' Can be a character vector, creating multiple columns, if `names_sep`
333-
#' or `names_pattern` is provided. In this case, there are two special
334-
#' values you can take advantage of:
332+
#' * `"fastest"`, the default, keeps individual rows from `cols` close
333+
#' together in the output. This often produces intuitively ordered output
334+
#' when you have at least one key column from `data` that is not involved in
335+
#' the pivoting process.
335336
#'
336-
#' * `NA` will discard that component of the name.
337-
#' * `.value` indicates that component of the name defines the name of the
338-
#' column containing the cell values, overriding `values_to`.
337+
#' * `"slowest"` keeps individual columns from `cols` close together in the
338+
#' output. This often produces intuitively ordered output when you utilize
339+
#' all of the columns from `data` in the pivoting process.
340+
#' @param names_to A character vector specifying the new column or columns to
341+
#' create from the information stored in the column names of `data` specified
342+
#' by `cols`.
343+
#'
344+
#' * If length 0, or if `NULL` is supplied, no columns will be created.
345+
#'
346+
#' * If length 1, a single column will be created which will contain the
347+
#' column names specified by `cols`.
348+
#'
349+
#' * If length >1, multiple columns will be created. In this case, one of
350+
#' `names_sep` or `names_pattern` must be supplied to specify how the
351+
#' column names should be split. There are also two additional character
352+
#' values you can take advantage of:
353+
#'
354+
#' * `NA` will discard the corresponding component of the column name.
355+
#'
356+
#' * `".value"` indicates that the corresponding component of the column
357+
#' name defines the name of the output column containing the cell values,
358+
#' overriding `values_to` entirely.
339359
#' @param names_prefix A regular expression used to remove matching text
340360
#' from the start of each variable name.
341361
#' @param names_sep,names_pattern If `names_to` contains multiple values,
@@ -365,19 +385,24 @@ extract.SingleCellExperiment <- function(data, col, into, regex="([[:alnum:]]+)"
365385
#' in the `value_to` column. This effectively converts explicit missing values
366386
#' to implicit missing values, and should generally be used only when missing
367387
#' values in `data` were created by its structure.
368-
#' @param names_transform,values_transform A list of column name-function pairs.
369-
#' Use these arguments if you need to change the type of specific columns.
370-
#' For example, `names_transform=list(week=as.integer)` would convert
371-
#' a character week variable to an integer.
372-
#' @param names_ptypes,values_ptypes A list of column name-prototype pairs.
373-
#' A prototype (or ptype for short) is a zero-length vector (like `integer()`
374-
#' or `numeric()`) that defines the type, class, and attributes of a vector.
375-
#' Use these arguments to confirm that the created columns are the types that
376-
#' you expect.
388+
#' @param names_transform,values_transform Optionally, a list of column
389+
#' name-function pairs. Alternatively, a single function can be supplied,
390+
#' which will be applied to all columns. Use these arguments if you need to
391+
#' change the types of specific columns. For example, `names_transform =
392+
#' list(week = as.integer)` would convert a character variable called `week`
393+
#' to an integer.
377394
#'
378395
#' If not specified, the type of the columns generated from `names_to` will
379396
#' be character, and the type of the variables generated from `values_to`
380397
#' will be the common type of the input columns used to generate them.
398+
#' @param names_ptypes,values_ptypes Optionally, a list of column name-prototype
399+
#' pairs. Alternatively, a single empty prototype can be supplied, which will
400+
#' be applied to all columns. A prototype (or ptype for short) is a
401+
#' zero-length vector (like `integer()` or `numeric()`) that defines the type,
402+
#' class, and attributes of a vector. Use these arguments if you want to
403+
#' confirm that the created columns are the types that you expect. Note that
404+
#' if you want to change (instead of confirm) the types of specific columns,
405+
#' you should use `names_transform` or `values_transform` instead.
381406
#' @param ... Additional arguments passed on to methods.
382407
#'
383408
#' @return A tidySingleCellExperiment objector a tibble depending on input
@@ -397,19 +422,11 @@ NULL
397422

398423
#' @export
399424
pivot_longer.SingleCellExperiment <- function(data,
400-
cols,
401-
names_to="name",
402-
names_prefix=NULL,
403-
names_sep=NULL,
404-
names_pattern=NULL,
405-
names_ptypes=list(),
406-
names_transform=list(),
407-
names_repair="check_unique",
408-
values_to="value",
409-
values_drop_na=FALSE,
410-
values_ptypes=list(),
411-
values_transform=list(),
412-
...) {
425+
cols, ..., cols_vary = "fastest", names_to = "name",
426+
names_prefix = NULL, names_sep = NULL, names_pattern = NULL,
427+
names_ptypes = NULL, names_transform = NULL, names_repair = "check_unique",
428+
values_to = "value", values_drop_na = FALSE, values_ptypes = NULL,
429+
values_transform = NULL) {
413430
cols <- enquo(cols)
414431

415432
message(data_frame_returned_message)
@@ -425,18 +442,19 @@ pivot_longer.SingleCellExperiment <- function(data,
425442
data %>%
426443
as_tibble() %>%
427444
tidyr::pivot_longer(!!cols,
428-
names_to=names_to,
429-
names_prefix=names_prefix,
430-
names_sep=names_sep,
431-
names_pattern=names_pattern,
432-
names_ptypes=names_ptypes,
433-
names_transform=names_transform,
434-
names_repair=names_repair,
435-
values_to=values_to,
436-
values_drop_na=values_drop_na,
437-
values_ptypes=values_ptypes,
438-
values_transform=values_transform,
439-
...
445+
...,
446+
cols_vary = cols_vary,
447+
names_to = names_to,
448+
names_prefix = names_prefix,
449+
names_sep = names_sep,
450+
names_pattern = names_pattern,
451+
names_ptypes = names_ptypes,
452+
names_transform = names_transform,
453+
names_repair = names_repair,
454+
values_to = values_to,
455+
values_drop_na = values_drop_na,
456+
values_ptypes = values_ptypes,
457+
values_transform = values_transform
440458
)
441459
}
442460

man/pivot-methods.Rd

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

0 commit comments

Comments
 (0)