This package uses thecnpy library written by CarlRogers to provide read and write facilities for files created with (orfor) the NumPy extension for Python. Vectors and matrices of numerictypes can be read or written to and from files as well as compressedfiles. Support for integer files is available if the package has beenbuilt with-std=c++11 which is the default starting withrelease 0.2.3 following the release of R 3.1.0, and available on allplatforms following the release of R 3.3.0 with the updated‘Rtools’.
The following Python code
>>>import numpyas np>>> fm= np.arange(12).reshape(3,4)*1.1>>> fmarray([[0. ,1.1,2.2,3.3], [4.4,5.5,6.6,7.7], [8.8,9.9,11. ,12.1]])>>> np.save("fmat.npy", fm)>>>>>> im= np.arange(12).reshape(3,4)>>> imarray([[0,1,2,3], [4,5,6,7], [8,9,10,11]])>>> np.save("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.01.12.23.3[2,]4.45.56.67.7[3,]8.89.911.012.1R>R> imat<-npyLoad("imat.npy","integer")R> imat [,1] [,2] [,3] [,4][1,]0123[2,]4567[3,]891011R>Going the opposite way by saving in R and reading in Python worksequally well. An extension not present inCNPy allows reading andwriting of gzip-compressed files.
The package has been tested and used on several architecture, andcopes correctly with little-vs-big endian switches.
More details are available in thepackagevignette.
The package is onCRAN andcan be installed per:
R> install.packages("RcppCNPy")OnCRAN,stable and mostly feature-complete.
Thereticulatepackage can also provide easy and comprehensive access to NumPy data;see theadditionalvignette in RcppCNPy for examples and more details.
Contributions are welcome, please use the GitHubissue trackerfor bug reports, feature requests or general discussions before sendingpullrequests.
Dirk Eddelbuettel and Wush Wu
GPL (>= 2)
Initially created: Tue Jan 6 21:24:50 CST 2015
Last modified: Sun May 26 10:09:43 CDT 2024