eddelbuettel / rcppcnpy

Rcpp bindings for NumPy files
GNU General Public License v2.0
26 stars 16 forks source link

New feature: Loading .npz #8

Closed mdnunez closed 6 years ago

mdnunez commented 7 years ago

The ability to load .npz in R has been a long desired addition to the rcppcnpy repository. An adept C++ programmer should tackle this issue and submit a pull request.

OmarAbdElNasser commented 6 years ago

Did you find a way to read .npz in R ?

eddelbuettel commented 6 years ago

You can use reticulate for this now and get the files directly from Python.

Generate Files in Python

Examples based on numpy documentation:

import numpy as np

x = np.arange(10)
y = np.sin(x)

outfile = "foo1.npz"
np.savez(outfile, x, y)

outfile = "foo2.npz"
np.savez(outfile, x=x, y=y)

Read in R

library(reticulate)
np <- import("numpy")

npz1 <- np$load("foo1.npz")
npz1$files
npz1$f[["arr_0"]]
npz1$f[["arr_1"]]

npz2 <- np$load("foo2.npz")
npz2$files
npz2$f[["x"]]
npz2$f[["y"]]

Output

edd@rob:/tmp$ Rscript npz.R
[1] "arr_1" "arr_0"
 [1] 0 1 2 3 4 5 6 7 8 9
 [1]  0.000000  0.841471  0.909297  0.141120 -0.756802 -0.958924 -0.279415  0.656987  0.989358  0.412118
[1] "y" "x"
 [1] 0 1 2 3 4 5 6 7 8 9
 [1]  0.000000  0.841471  0.909297  0.141120 -0.756802 -0.958924 -0.279415  0.656987  0.989358  0.412118
edd@rob:/tmp$