JD Long
JD Long

Reputation: 60746

Creating R data frames and .rdata files from Java

What is the most memory efficient and easiest (yes, I know those are sometimes mutually exclusive) way to create an R data frame then save it to an .Rdata file using Java?

Go easy on me though, I'm not a Java developer.

Upvotes: 2

Views: 2375

Answers (3)

J. Win.
J. Win.

Reputation: 6771

How about building a text datafile with structure() and retrieving it with dget()?

data.frame(x= 1:5, y= as.factor(1:5), z= as.character(1:5))

gives the same result as:

structure(list(x = 1:5, y = structure(1:5, .Label = c("1", "2", 
"3", "4", "5"), class = "factor"), z = structure(1:5, .Label = c("1", 
"2", "3", "4", "5"), class = "factor")), .Names = c("x", "y", 
"z"), row.names = c(NA, -5L), class = "data.frame")

It is not memory efficient per se, but you have more control over the data types. From R, you can show a data frame in the above long format by using dput() and retrieve it from a text file with dget(), and it shouldn't take too much parsing to write it from Java.

Upvotes: 2

Jeffrey Breen
Jeffrey Breen

Reputation: 463

My first inclination is to throw stuff in MySQL, but the overhead of creating tables, etc. probably doesn't make sense if these files are temporary in nature.

I agree with the others that if you want to run R from Java, rJava is the way to go, but this solution seems a little clumsy.

Along the lines of the simplicity of CSV files, but how about using a portable data format like NetCDF http://en.wikipedia.org/wiki/NetCDF instead? They should preserve data formats better and can be accessed from Java ( http://www.unidata.ucar.edu/software/netcdf-java/ ), R ( http://cran.r-project.org/web/packages/RNetCDF/ ), and even GDAL.

(My astro background forces me to mention FITS as an option too.)

Upvotes: 0

Dr G
Dr G

Reputation: 4017

It might be a bit of an overkill but rJava/JRI (http://rosuda.org/rJava/) give you a Java API to R. Essentially you get an R process that you can control programmatically from your Java code and obviously you can share data and create a .RData file through R calls.

Upvotes: 0

Related Questions