S3 Methods for AbstractAnnData Objects
Source:R/AbstractAnnData-s3methods.R
AbstractAnnData-s3methods.Rd
These S3 methods provide standard R interfaces for AbstractAnnData objects, making them behave like native R objects with familiar syntax.
Usage
# S3 method for class 'AbstractAnnData'
dim(x)
# S3 method for class 'AbstractAnnData'
nrow(x)
# S3 method for class 'AbstractAnnData'
ncol(x)
# S3 method for class 'AbstractAnnData'
dimnames(x)
# S3 method for class 'AbstractAnnData'
dimnames(x) <- value
# S3 method for class 'AbstractAnnData'
x[i, j, drop = TRUE, ...]
Arguments
- x
An AbstractAnnData object
- value
For
dimnames<-
: A list of two character vectors (obs_names, var_names)- i
Row indices (observations). Can be numeric, logical, or character.
- j
Column indices (variables). Can be numeric, logical, or character.
- drop
Ignored (for compatibility with generic)
- ...
Additional arguments passed to methods
Value
dim
: Numeric vector of length 2 (n_obs, n_vars)nrow
,ncol
: Integer countdimnames
: List with obs_names and var_namesrownames
,colnames
: Character vectordimnames<-
,rownames<-
,colnames<-
: The modified object (invisibly)[
: A AnnDataView object with the specified subset
Details
Subsetting behavior: The [
method supports logical, integer, and character
subsetting for both observations (rows) and variables (columns). However, unlike
standard R behavior:
Logical vectors are not recycled and must have the exact same length as the dimension being subset
Negative indices are not supported (R's "exclude these" syntax)
These design choices ensure clear and predictable subsetting behavior for biological data matrices, avoiding potential confusion from accidental recycling or exclusion patterns.
The following S3 methods are available:
dim(x)
: Get dimensions (n_obs, n_vars), equivalent tox$shape()
nrow(x)
: Get number of observations, equivalent tox$n_obs()
ncol(x)
: Get number of variables, equivalent tox$n_vars()
dimnames(x)
: Get dimension names, (obs_names, var_names)rownames(x)
: Get observation names, equivalent tox$obs_names()
colnames(x)
: Get variable names, equivalent tox$var_names()
dimnames(x) <- value
: Set dimension namesrownames(x) <- value
: Set observation names, equivalent tox$obs_names() <- ...
colnames(x) <- value
: Set variable names, equivalent tox$var_names() <- ...
x[i, j]
: Subset observations and/or variables
Examples
if (FALSE) { # \dontrun{
# Create example data
ad <- generate_dataset(n_obs = 100, n_vars = 50)
# Standard R methods work
dim(ad)
nrow(ad)
ncol(ad)
dimnames(ad)
rownames(ad)
colnames(ad)
# Set names using dimnames
dimnames(ad) <- list(
paste0("cell_", 1:nrow(ad)),
paste0("gene_", 1:ncol(ad))
)
# Or set names individually (uses dimnames<- internally)
rownames(ad) <- paste0("cell_", 1:nrow(ad))
colnames(ad) <- paste0("gene_", 1:ncol(ad))
# Subsetting creates AnnDataView
subset_ad <- ad[1:10, 1:5]
subset_ad <- ad[rep(c(TRUE, FALSE), length.out = nrow(ad)), ] # logical subsetting (no recycling)
subset_ad <- ad[c("cell_1", "cell_2"), c("gene_1", "gene_2")] # name subsetting
} # }