https://github.com/alipsa/rdatautil
Utility to read and write RData (rds) files in java
https://github.com/alipsa/rdatautil
jvm r
Last synced: about 1 month ago
JSON representation
Utility to read and write RData (rds) files in java
- Host: GitHub
- URL: https://github.com/alipsa/rdatautil
- Owner: Alipsa
- License: mit
- Created: 2022-02-09T16:06:06.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2022-02-11T11:54:27.000Z (over 4 years ago)
- Last Synced: 2024-05-01T11:27:03.146Z (about 2 years ago)
- Topics: jvm, r
- Language: Java
- Homepage:
- Size: 16.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# rdatautil
Utility to read and write RData (rds) files in java.
RDS files are created in R:
```R
# Save a data.frame object to a file
saveRDS(mtcars, "mtcars.rds")
```
...and they can be read in R like this:
```R
# Restore it
my_data <- readRDS("mtcars.rds")
```
This utility allows you to save and read such rsd files in java. It relies on the [Renjin project](https://github.com/bedatadriven/renjin)
for the heavy lifting.
## Usage
Add the dependency to your pom (or equivalent):
```xml
se.alipsa
rdatautil
1.0.0
```
### Read a rds file
the read method takes either a File or an InputStream as a parameter:
```groovy
import se.alipsa.rdatautils.RDataUtil;
import org.renjin.sexp.SEXP;
SEXP data = RDataUtil.read(new File("mtcars.rds"));
```
```groovy
import se.alipsa.rdatautils.RDataUtil;
import org.renjin.sexp.SEXP;
SEXP data = RDataUtil.read(getClass().getResourceAsStream("mtcars.rds"));
```
### Write to a rds file
```groovy
import se.alipsa.rdatautils.RDataUtil;
import org.renjin.sexp.SEXP;
import org.renjin.sexp.PairList;
PairList.Builder file = new PairList.Builder();
file.add("names", new StringArrayVector("Kurt", "Susan", "Zoe", StringVector.NA));
file.add("income", new IntArrayVector(120000, 220202, 303030, IntVector.NA));
SEXP sexp = file.build();
// You can write to a file
RDataUtil.write(sexp, new File("employeeSalaries.rds"));
// or an OutputStream
RDataUtil.write(sexp, System.out);
```
### Handling complex data
The [se.alipsa:renjin-client-data-utils](https://github.com/perNyfelt/renjin-client-data-utils) project enables you to
deal with tabular data and combines very well with rdatautil:
Reading data:
```groovy
import se.alipsa.rdatautils.RDataUtil;
import org.renjin.sexp.SEXP;
import se.alipsa.renjin.client.datautils.Table;
SEXP data = RDataUtil.read(getClass().getResourceAsStream("employeeSalaries.rds"));
Table table = Table.createTable(data);
// Get the data.frame as a List of rows suitable for how data is usually handled in java:
List> rows = table.getRowList();
// You can also get an individual value
String employee1 = table.getValueAsString(0,0);
```
Writing data:
```java
import se.alipsa.rdatautils.RDataUtil;
import org.renjin.sexp.SEXP;
import se.alipsa.renjin.client.datautils.Table;
import java.sql.ResultSet;
import java.sql.SQLException;
class RdataSaver {
public static void saveToRds(ResultSet rs, OutputStream os) throws SQLException {
Table table = new Table(rs);
RDataUtil.write(table.asDataframe(), os);
}
}
```