Demo with scanpy

  collapse = TRUE,
  comment = "#>"

knitr::opts_chunk$set(eval = reticulate::py_module_available("anndata") && reticulate::py_module_available("scanpy"))

We've found that by using anndata for R, interacting with other anndata-based Python packages becomes super easy!

Download and load dataset

Let's use a 10x dataset from the 10x genomics website. You can download it to an anndata object with scanpy as follows:

sc <- import("scanpy")

url <- ""
ad <- sc$read_10x_h5("dataset.h5", backup_url = url)


Preprocessing dataset

The resuling dataset is a wrapper for the Python class but behaves very much like an R object:

ad[1:5, 3:5]

But you can still call scanpy functions on it, for example to perform preprocessing.

sc$pp$filter_cells(ad, min_genes = 200)
sc$pp$filter_genes(ad, min_cells = 3)

Analysing your dataset in R

You can seamlessly switch back to using your dataset with other R functions, for example by calculating the rowMeans of the expression matrix.

rowMeans(ad$X[1:10, ])

Try the anndata package in your browser

Any scripts or data that you put into this service are public.

anndata documentation built on March 31, 2023, 10:02 p.m.