---
title: "R Interoperability"
---


# Reading R Data with `rds2py`

The `rds2py` package allows Python users to read `.rds` files generated by R. This is critical for pipelines where preprocessing is done in R and modeling in Python (or vice versa).

## Reading .rds Files

`rds2py` parses the serialized R object and attempts to reconstruct the equivalent Python container (e.g., `SingleCellExperiment` or `BiocFrame`).

```{python}
#| eval: false
import rds2py

# Read a SingleCellExperiment saved from R
# The result is a native Python object (e.g. SingleCellExperiment)
sce = rds2py.read_rds("data/processed_data.rds")

# Access components as native Python objects
print(sce.assays["logcounts"])
print(sce.row_data)
```

## Parsing Complex Objects

If the object does not have a direct Python equivalent, you can parse it into a dictionary structure containing the raw data and attributes.

```{python}
#| eval: false
# 'as_dict=True' forces the result to be a dictionary structure
# This is useful for debugging or accessing S4 slots that aren't mapped yet
raw_data = rds2py.parse_rds("custom_model.rds")

# Explore the structure
print(raw_data.keys())
```

## Performance Considerations

*   **Memory:** `rds2py` reads the entire object into memory. For extremely large datasets (hundreds of GBs), consider using `ArtifactDB` (which supports lazy loading of HDF5 matrices) instead of `.rds` files.
*   **Completeness:** While standard Bioconductor classes (`SCE`, `SE`, `GRanges`) are well-supported, custom S4 classes may require manual parsing via the dictionary interface.

