Created
August 21, 2011 18:29
-
-
Save jseabold/1160958 to your computer and use it in GitHub Desktop.
R matrix and vector to numpy array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # This function respects the digits option | |
| mkarray <- function(X, name) { | |
| cat(name); cat(" = np.array(["); cat(X, sep=","); cat("])") | |
| if (is.matrix(X)) { | |
| i <- as.character(nrow(X)) | |
| j <- as.character(ncol(X)) | |
| cat(".reshape("); cat(i); cat(","); cat(j); cat(", order='F')") | |
| } | |
| cat("\n\n") | |
| } | |
| R2array <- function(..., fname, append=FALSE) { | |
| if (!is.list(...)) { | |
| to_write <- list(...) | |
| } | |
| else { | |
| to_write <- (...) | |
| } | |
| sink(file=fname, append=append) | |
| # assumes appended file already imports numpy | |
| if (file.info(fname)$size == 0) { | |
| cat("import numpy as np\n\n") | |
| } | |
| for (i in c(1:length(to_write))) { | |
| name <- names(to_write)[i] | |
| X <- to_write[[i]] | |
| name <- gsub("\\.", "_", name) # make name pythonic | |
| mkarray(X=X, name=name) | |
| } | |
| sink() | |
| } | |
| #fname <- "RResults.py" | |
| #R2array(A=A,B=B,params=params,fname=fname) | |
| #also takes a lsit | |
| #R2array(list(A=A,B=B,params=params),fname=fname) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment