Skip to contents

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 count

  • dimnames: List with obs_names and var_names

  • rownames, colnames: Character vector

  • dimnames<-, 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 to x$shape()

  • nrow(x): Get number of observations, equivalent to x$n_obs()

  • ncol(x): Get number of variables, equivalent to x$n_vars()

  • dimnames(x): Get dimension names, (obs_names, var_names)

  • rownames(x): Get observation names, equivalent to x$obs_names()

  • colnames(x): Get variable names, equivalent to x$var_names()

  • dimnames(x) <- value: Set dimension names

  • rownames(x) <- value: Set observation names, equivalent to x$obs_names() <- ...

  • colnames(x) <- value: Set variable names, equivalent to x$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
} # }