Skip to contents

In the other vignettes, we mentioned that whenever possible, we would recommend users to use more advanced and performant formats such as Zarr. Zarr is particularly well-suited for arrays or matrixes in R.

In this short vignette, we show how one would write and read Zarr files in R, using the Rarr package.

We also quickly and informally benchmark the performance of these formats against .npy files, using the grumpy package.

Let’s start by generating a sample .npy file to use in our benchmarks.

x <- matrix(0L, nrow = 1000L, ncol = 1000L)
x[300L:400L, 700L:800L] <- seq_len(101L * 101L)
library(reticulate)
np <- import("numpy")
Downloading uv...Done!
f_npy <- withr::local_tempfile(fileext = ".npy")
np$save(f_npy, x)

Introduction to Zarr

If you now understand how the .npy file format works, the Zarr format is a natural extension of it. Zarr is a chunked, compressed, N-dimensional array storage format. Each chunk of data corresponds to a small sub-array of the original array, and is stored in a separate file, in a format nearly identical to .npy. The chunk is then usually compressed with high-performance compression algorithms such as gzip, blosc, or zstd.

One benefit of this chunked approach is that it allows for efficient reading and writing of large arrays, as only the necessary chunks need to be read or written, rather than the entire array. This is particularly useful for large datasets that do not fit into memory, as it allows for out-of-core processing of the data. It also provides strong benefits when some chunks are “empty” (e.g. filled with zeros, such as the white/black background of an image), as these chunks can be skipped during reading and writing, and thus reduce the overall storage requirements of the dataset.

f_zarr <- withr::local_tempfile(fileext = ".zarr")
Rarr::write_zarr_array(x, f_zarr, chunk_dim = c(100L, 100L), compressor = NULL)

Storage size comparison

Let’s look at the size of the Zarr file on disk:

size_zarr <- list.files(f_zarr, full.names = TRUE, recursive = TRUE) |>
  file.info() |>
  subset(select = "size") |>
  sum()
size_npy <- file.info(f_npy)$size
size_zarr
[1] 80576
size_npy
[1] 4000128

Without compression, the equivalent Zarr data is thus 80.6 kB on disk, so 50 times smaller than the .npy file (4 MB). We could also use compression to further reduce the size of the Zarr file on disk, but this is out of scope for this vignette.

Decoding speed comparison

We can also compare the speed of reading the .npy file with grumpy and the Zarr file with Rarr. To do so, we need to actually generate the data. We do it via reticulate because writing .npy files is out of scope for grumpy.

bench::mark(
  grumpy = read_npy(f_npy),
  zarr = read_zarr_array(f_zarr),
  iterations = 50L
)
# A tibble: 2 × 6
  expression      min   median `itr/sec` mem_alloc `gc/sec`
  <bch:expr> <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
1 grumpy        1.3ms   4.22ms      292.    7.76MB     82.4
2 zarr          3.5ms   3.57ms      278.     4.3MB     24.1

There is a small time penalty for reading the Zarr file, since the various chunks need to be read and concatenated together, but the memory footprint is much smaller, and the Zarr file is much smaller on disk. This is particularly important for large datasets that do not fit into memory, as it allows for out-of-core processing of the data.

R version 4.6.2 (2026-10-29)
Platform: x86_64-pc-linux-gnu
Running under: Ubuntu 26.04.1 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.32.so;  LAPACK version 3.12.0

locale:
 [1] LC_CTYPE=C.UTF-8       LC_NUMERIC=C           LC_TIME=C.UTF-8
 [4] LC_COLLATE=C.UTF-8     LC_MONETARY=C.UTF-8    LC_MESSAGES=C.UTF-8
 [7] LC_PAPER=C.UTF-8       LC_NAME=C              LC_ADDRESS=C
[10] LC_TELEPHONE=C         LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C

time zone: UTC
tzcode source: system (glibc)

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] reticulate_1.47.0 Rarr_2.3.10       grumpy_1.0.0

loaded via a namespace (and not attached):
 [1] Matrix_1.7-6        jsonlite_2.0.0      compiler_4.6.2
 [4] crayon_1.5.3        Rcpp_1.1.2          IRanges_2.46.0
 [7] png_0.1-9           yaml_2.3.12         fastmap_1.2.0
[10] lattice_0.23-1      here_1.0.2          R6_2.6.1
[13] S4Arrays_1.12.1     generics_0.1.4      curl_8.2.0
[16] httr2_1.3.0         knitr_1.52          BiocGenerics_0.58.1
[19] tibble_3.3.1        paws.storage_0.11.0 rprojroot_2.1.1
[22] pillar_1.11.1       paws.common_0.9.0   R.utils_2.13.0
[25] rlang_1.3.0         utf8_1.2.6          xfun_0.63
[28] otel_0.3.0          cli_3.6.6           withr_3.0.3
[31] magrittr_2.0.5      digest_0.6.39       grid_4.6.2
[34] rappdirs_0.3.4      lifecycle_1.0.5     vctrs_0.7.3
[37] R.methodsS3_1.8.2   R.oo_1.27.1         S4Vectors_0.50.3
[40] bench_1.1.4         evaluate_1.0.5      glue_1.8.1
[43] abind_1.4-8         stats4_4.6.2        profmem_0.7.0
[46] rmarkdown_2.32      pkgconfig_2.0.3     tools_4.6.2
[49] htmltools_0.5.9