js-stream-rdata reads R data frames from .RData and .rds files through the librdata C library.
The public TypeScript method signatures remain the same:
getMetadata()getData()readRecords()getUniqueValues()
The metadata shape still follows the Dataset-JSON style used by the existing wrapper.
.RData.rds
- Read metadata from R data frames
- Read rows as arrays or keyed objects
- Stream records with async iteration
- Select columns
- Filter rows with
js-array-filter - Compute unique values per column
npm install js-stream-rdataCompile the TypeScript layer:
npm run buildRebuild the local native addon:
npm run build:nativeGenerate packaged prebuild binaries:
npm run build:prebuildsUse createDatasetReader() when you want the library to infer .RData vs .rds from the file extension.
import { createDatasetReader } from 'js-stream-rdata';
const dataset = createDatasetReader('/path/to/sample.RData');
const metadata = await dataset.getMetadata();
const { data, lastRow, endReached } = await dataset.getData({
start: 0,
length: 10,
});Use DatasetRData when the dataset is already known to be .RData or .rds.
import DatasetRData, { DatasetReader } from 'js-stream-rdata';
const dataset = new DatasetRData('/path/to/sample.rds');
const genericDataset = new DatasetReader('/path/to/sample.RData', {
format: 'rdata',
});const metadata = await dataset.getMetadata();const result = await dataset.getData({
start: 0,
length: 500,
type: 'object',
filterColumns: ['Name', 'Age'],
});
console.log(result.data);
console.log(result.lastRow);
console.log(result.endReached);getData() returns:
{
data: (ItemDataArray | ItemDataObject)[];
lastRow: number;
endReached: boolean;
}for await (const record of dataset.readRecords({
start: 10,
bufferLength: 1000,
type: 'object',
filterColumns: ['Name', 'Age'],
})) {
console.log(record);
}Filtering is supported by getData() through js-array-filter.
import Filter from 'js-array-filter';
import { createDatasetReader } from 'js-stream-rdata';
const dataset = createDatasetReader('/path/to/sample.RData');
const metadata = await dataset.getMetadata();
const filter = new Filter('dataset-json1.1', metadata.columns, {
conditions: [
{ variable: 'Age', operator: 'gt', value: 55 },
{ variable: 'Sex', operator: 'eq', value: 'F' },
],
connectors: ['or'],
});
const filtered = await dataset.getData({
start: 0,
length: 100,
type: 'object',
filter,
filterColumns: ['Name', 'Sex', 'Age'],
});
console.log(filtered.data);A plain BasicFilter object can also be passed as filter.
const uniqueValues = await dataset.getUniqueValues({
columns: ['Name', 'Sex'],
limit: 100,
bufferLength: 1000,
sort: true,
addCount: true,
});
console.log(uniqueValues);Creates a dataset reader by detecting the file format from the extension.
Creates the generic dataset reader.
Options:
encoding?: BufferEncodingdefaultutf8checkExists?: booleandefaultfalseformat?: 'rdata' | 'rds'
Returns Promise<DatasetMetadata>.
Parameters:
start?: numberlength?: numberuse-1to read to the endtype?: 'array' | 'object'filterColumns?: string[]filter?: Filter | BasicFilterchunkSize?: number
Returns a promise resolving to an object with data, lastRow, and endReached.
Parameters:
start?: numberbufferLength?: numbertype?: 'array' | 'object'filterColumns?: string[]
Returns AsyncGenerator<ItemDataArray | ItemDataObject, void, undefined>.
Parameters:
columns: string[]limit?: numberbufferLength?: numbersort?: booleanaddCount?: boolean
Returns Promise<UniqueValues>.
npm testThis project is licensed under the MIT License. See the LICENSE file for details.
Dmitry Kolosov
Contributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes.
For more details, refer to the source code and the documentation.