Skip to content


Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time

RcppCNPy: Rcpp bindings for NumPy files

CI License CRAN Dependencies Downloads Last Commit
status DOI


This package uses the cnpy library written by Carl Rogers to provide read and write facilities for files created with (or for) the NumPy extension for Python. Vectors and matrices of numeric types can be read or written to and from files as well as compressed files. Support for integer files is available if the package has been built with -std=c++11 which is the default starting with release 0.2.3 following the release of R 3.1.0, and available on all platforms following the release of R 3.3.0 with the updated 'Rtools'.


The following Python code

>>> import numpy as np
>>> fm = np.arange(12).reshape(3,4) * 1.1
>>> fm
array([[  0. ,   1.1,   2.2,   3.3],
       [  4.4,   5.5,   6.6,   7.7],
       [  8.8,   9.9,  11. ,  12.1]])
>>>"fmat.npy", fm)
>>> im = np.arange(12).reshape(3,4)
>>> im
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
>>>"imat.npy", im)

saves two matrices in floating-point and integer representation.

With this R code we can read and assign the files:

R> library(RcppCNPy)
R> fmat <- npyLoad("fmat.npy")
R> fmat
     [,1] [,2] [,3] [,4]
[1,]  0.0  1.1  2.2  3.3
[2,]  4.4  5.5  6.6  7.7
[3,]  8.8  9.9 11.0 12.1
R> imat <- npyLoad("imat.npy", "integer")
R> imat
     [,1] [,2] [,3] [,4]
[1,]    0    1    2    3
[2,]    4    5    6    7
[3,]    8    9   10   11

Going the opposite way by saving in R and reading in Python works equally well. An extension not present in CNPy allows reading and writing of gzip-compressed files.

The package has been tested and used on several architecture, and copes correctly with little-vs-big endian switches.

More details are available in the package vignette.


The package is on CRAN and can be installed per:

R> install.packages("RcppCNPy")


On CRAN, stable and mostly feature-complete.

Alternative: reticulate

The reticulate package can also provide easy and comprehensive access to NumPy data; see the additional vignette in RcppCNPy for examples and more details.


Contributions are welcome, please use the GitHub issue tracker for bug reports, feature requests or general discussions before sending pull requests.


Dirk Eddelbuettel and Wush Wu


GPL (>= 2)