Package 'RcppCNPy'

Title: Read-Write Support for 'NumPy' Files via 'Rcpp'
Description: The 'cnpy' library written by Carl Rogers provides 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 as C++11 which should be the default on all platforms since the release of R 3.3.0.
Authors: Dirk Eddelbuettel and Wush Wu
Maintainer: Dirk Eddelbuettel <[email protected]>
License: GPL (>= 2)
Version: 0.2.12
Built: 2024-07-16 02:42:58 UTC

Help Index

File access to data files written by (or for) NumPy (Numeric Python) modules


This package provides access to the cnpy library by Carl Rogers which provides read and write facilities for files created with (or for) the NumPy extension for Python.

Support is provided to reading and writing of either vectors or matrices of numeric or integer types.

Files with gzip compression can be transparently read and written as well.


npyLoad(filename, type="numeric", dotranspose=TRUE)
  npySave(filename, object, mode="w", checkPath=TRUE)



string with (path and) filename for a npy object file. If the string ends with .gz, compressed files can be read or written.


string with type 'numeric' (default) or 'integer'.


an R object, currently limited to a vector or matrix of either integer or numeric type


a boolean variable indicating whether a two-dimensional object should be transposed after reading, default is true


a one-character string indicating whether files are appended to ("a") or written ("w", the default). In case of writing gzip-ed file, this option is not supported as such files can only be (over-)written, and bot appended.


a boolean variable indicating whether a path implied in the filename argument is to be checked for existing directories, default is true.


The package uses Rcpp modules to provide R bindings npyLoad() and npySave() which wrap the npy_load() and npy_save() functions. Currently, only one- and two-dimensional vectors and matrices are suppported; higher-dimensional arrays could be added.

Integer support requires access to the long long type which is available if the package is built using the C++11 standard; this is the default since release 0.2.3 which came out after R 3.1.0 permitted use of C++11 in CRAN packages.

Note that R uses only one integer type (which uses 32 bits) and one double floating point type (which uses 64 bits). If Python data of either type with a different bitsize is to be shared with R, it has be cast to the corresponding width used by R first.


Dirk Eddelbuettel provided the binding to R (using the Rcpp package).

Carl Rogers wrote the underlying cnpy library, which is released under the MIT license.

Maintainer: Dirk Eddelbuettel <[email protected]>


Rcpp, in particular the Rcpp modules documentation.

The cnpy repository:

See Also



## Not run: 

    ## load NumPy file with floating-point data
    fmat <- npyLoad("fmat.npy")
    ## load NumPy file with integer data
    imat <- npyLoad("imat.npy", "integer")

    ## save floating-point data: matrix and vector
    M <- matrix(0:11, 3, 4, byrow=TRUE) * 1.1
    v <- v <- 0:4 * 1.1
    npySave("fmat.npy", M)
    npySave("fvec.npy", v)

    ## save integer data: matrix and vector
    M <- matrix(0:11, 3, 4, byrow=TRUE)
    v <- v <- 0:4 
    npySave("imat.npy", M)
    npySave("ivec.npy", v)
## End(Not run)