# R S4 Object Accessor Usage

> Use when you have constructed or received a SummarizedExperiment object (or similar S4 class) containing MS feature tables, counts matrices, or sample-level metadata, and need to retrieve specific slots (e.

- Skill: `holobiomicslab/r-s4-object-accessor-usage` (Agent Skill)
- Install (CLI): `npx skillmds@latest add holobiomicslab/r-s4-object-accessor-usage`
- Raw SKILL.md: https://api.skillmd.com/api/skills/holobiomicslab/r-s4-object-accessor-usage/raw
- Safety review: PASS (external: skill-scanner PASS, skillspector PASS)
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- License: CC-BY-4.0
- Author: HolobiomicsLab (https://skillmd.com/u/holobiomicslab)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/holobiomicslab/r-s4-object-accessor-usage

---


# R S4 Object Accessor Usage

## Summary

Access and extract data from S4 objects (particularly SummarizedExperiment) using standard Bioconductor accessor functions like assay() and colData(). This skill enables standardized, type-safe retrieval of counts matrices, sample metadata, and assay annotations from complex biological data structures.

## When to use

You have constructed or received a SummarizedExperiment object (or similar S4 class) containing MS feature tables, counts matrices, or sample-level metadata, and need to retrieve specific slots (e.g., abundance data, sample annotations, experimental design) in a way that respects Bioconductor conventions and maintains object integrity.

## When NOT to use

- Input is a base R data.frame or matrix—use standard subsetting ([, ]) instead.
- You need to modify object structure—use replacement accessors (assay()<- , colData()<-) rather than this extraction skill.
- S4 object does not define accessor methods (check class definition or slot names); direct @ slot access may be necessary.

## Inputs

- SummarizedExperiment object
- S4 object with defined accessor methods

## Outputs

- counts matrix (numeric, rows=features, cols=samples)
- colData DataFrame (sample metadata with condition/batch assignments)
- rowData DataFrame (feature annotations)
- assay(type) results (generic matrix or array)

## How to apply

After instantiating a SummarizedExperiment object (e.g., via mzrtsim_se()), use accessor functions to retrieve data rather than direct slot access (@ operator). Call SummarizedExperiment::assay() to extract the counts matrix, SummarizedExperiment::colData() to retrieve sample-level metadata including condition and batch labels, and other specialized accessors (e.g., rowData()) for feature-level annotations. Verify that returned objects match expected dimensions and data types—e.g., assay() returns a matrix with features as rows and samples as columns; colData() returns a DataFrame with sample identifiers as row names and experimental variables (condition, batch) as columns.

## Related tools

- **SummarizedExperiment** (S4 class providing assay(), colData(), and rowData() accessor methods for storing and retrieving counts, sample metadata, and feature annotations) — https://bioconductor.org/packages/SummarizedExperiment
- **mzrtsim** (Generates SummarizedExperiment objects via mzrtsim_se() wrapping simulated LC/GC-MS peak tables with condition and batch effects) — https://github.com/yufree/mzrtsim
- **R** (Runtime environment for S4 object instantiation and accessor method invocation)

## Examples

```
library(SummarizedExperiment); se <- mzrtsim_se(); counts_mat <- assay(se); sample_md <- colData(se)
```

## Evaluation signals

- assay() returns a numeric matrix with non-zero dimensions matching the number of features and samples in the object.
- colData() returns a DataFrame with row names equal to sample identifiers and columns including condition and batch variables.
- Accessor calls do not raise 'slot not found' or 'method not defined' errors.
- Retrieved count values and metadata are consistent with simulation parameters (e.g., expected condition contrasts visible in abundance patterns).
- Object structure remains intact after accessor calls—no unintended side effects or data corruption.

## Limitations

- Accessor methods are class-specific; SummarizedExperiment accessors will not work on other S4 classes without similar method definitions.
- Accessor functions return references or shallow copies depending on implementation; modifying returned objects may or may not affect the parent S4 object.
- Large SummarizedExperiment objects (e.g., many samples or features) may consume significant memory when fully materialized via assay().

## Evidence

- [other] mzrtsim_se() produces a SummarizedExperiment object containing a 'counts' assay and colData that can be accessed via standard Bioconductor accessors such as SummarizedExperiment::assay() and SummarizedExperiment::colData().: "mzrtsim_se() produces a SummarizedExperiment object containing a 'counts' assay and colData that can be accessed via standard Bioconductor accessors such as SummarizedExperiment::assay() and"
- [other] Verify that the resulting object exposes counts via the assay() accessor and colData via the colData() accessor per Bioconductor conventions.: "Verify that the resulting object exposes counts via the assay() accessor and colData via the colData() accessor per Bioconductor conventions."
- [intro] For seamless integration with Bioconductor workflows, use `mzrtsim_se()` which wraps the simulation in a `SummarizedExperiment`: "For seamless integration with Bioconductor workflows, use `mzrtsim_se()` which wraps the simulation in a `SummarizedExperiment`"
- [other] Construct column metadata (colData) from simulation parameters including sample identifiers, condition assignments, and batch labels.: "Construct column metadata (colData) from simulation parameters including sample identifiers, condition assignments, and batch labels."

