Skip to contents

Scope

Reading .npy and .npz files in R.

Writing is out of scope. When working across multiple languages, one should prefer high-performance interoperable formats (parquet, Zarr, etc.).

Dependencies

Because grumpy is a intended to be used deep in the dependency graph of other packages, we want to minimize the number of dependencies. There is currently no dependency, and no new dependency should be added unless it provides a significant performance improvement.

Function signatures

Inputs

Outputs

  • The output of read_npy() is always an array (i.e., is.array() is TRUE), even for structured datatypes. This is to keep the output consistent and as conceptually close as possible to the original NumPy array.
  • The output of read_npz() is always a named list of arrays, even if the .npz file contains only one array. A stable output type is important for downstream analysis.

FAQ

Why not use reticulate?

  • When reading .npy files with reticulate, at some point in time, two (one in Python and one in R) or three copies of the data are held in memory. This can be problematic for large files. With grumpy, one (for types matching R native types; i.e., int32, float64/double) or two copies of the data are held in memory.

    f <- tempfile(fileext = ".npy")
    np <- reticulate::import("numpy")
    x <- np$random$rand(1000L, 1000L)
    np$save(f, x)
    
    bench::mark(
      reticulate = {
        x <- np$load(f)
      },
      grumpy = {
        x <- grumpy::read_npy(f)
      },
      iterations = 1e3
    )
    # A tibble: 2 × 6
      expression      min   median `itr/sec` mem_alloc `gc/sec`
      <bch:expr> <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
    1 reticulate   1.22ms   1.44ms      564.    7.63MB     138.
    2 grumpy       2.35ms   2.67ms      228.    15.5MB     182.
    ## On platform that support it, lazy reading offers even better performance for large files
    if (Sys.info()[["sysname"]] %in% c("Linux", "Darwin")) {
      bench::mark(
        reticulate = {
          x <- np$load(f)
        },
        grumpy = {
          x <- grumpy::read_npy(f, lazy = TRUE)
        },
        iterations = 1e3
      )
    }
    # A tibble: 2 × 6
      expression      min   median `itr/sec` mem_alloc `gc/sec`
      <bch:expr> <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
    1 reticulate   1.23ms   1.41ms      609.    7.63MB     131.
    2 grumpy     487.59µs 578.83µs     1464.    7.64MB     313.
  • Reading data with reticulate requires a Python installation and additional python packages, which users in restricted environments may not have access to. grumpy is a pure R package with no external dependencies. This is especially important as we expect grumpy to be used deep in the dependency graph of other packages, and we want to minimize the number of dependencies.

  • A dedicated R package gives us more flexibility in how edge cases such as 64-bit integers are handled. reticulate automatically and silently converts 64-bit integers to double, which is a sensible default for many use cases. But we may want to have more control over this behavior, and grumpy will allow us to do that in the future. Another good example are structured data types (record arrays), which are returned as data.frames, not arrays, with reticulate.

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

loaded via a namespace (and not attached):
 [1] vctrs_0.7.3       cli_3.6.6         knitr_1.52        rlang_1.3.0
 [5] xfun_0.63         otel_0.3.0        bench_1.1.4       png_0.1-9
 [9] jsonlite_2.0.0    glue_1.8.1        grumpy_1.0.0      rprojroot_2.1.1
[13] htmltools_0.5.9   rmarkdown_2.32    rappdirs_0.3.4    grid_4.6.2
[17] tibble_3.3.1      evaluate_1.0.5    fastmap_1.2.0     profmem_0.7.0
[21] yaml_2.3.12       lifecycle_1.0.5   compiler_4.6.2    pkgconfig_2.0.3
[25] Rcpp_1.1.2        here_1.0.2        lattice_0.23-1    digest_0.6.39
[29] utf8_1.2.6        reticulate_1.47.0 pillar_1.11.1     magrittr_2.0.5
[33] Matrix_1.7-6      tools_4.6.2       withr_3.0.3