/** * The webR JavaScript API. * @module WebR */ import { ChannelMain } from './chan/channel'; import { ChannelType } from './chan/channel-common'; import { Message } from './chan/message'; import { EmPtr } from './emscripten'; import { newRClassProxy } from './proxy'; import { RCharacter, RComplex, RDouble } from './robj-main'; import { REnvironment, RSymbol, RInteger } from './robj-main'; import { RList, RLogical, RNull, RObject, RPairlist, RRaw, RString, RCall } from './robj-main'; import * as RWorker from './robj-worker'; import { EvalROptions, InstallPackagesOptions } from './webr-chan'; export { Console, ConsoleCallbacks } from './console'; export * from './robj-main'; export * from './error'; export { ChannelType } from './chan/channel-common'; /** * The webR FS API for interacting with the Emscripten Virtual File System. */ export interface WebRFS { /** * Lookup information about a file or directory node in the Emscripten * virtual file system. * @param {string} path Path to the requested node. * @returns {Promise} The requested node. */ lookupPath: (path: string) => Promise; /** * Create a directory on the Emscripten virtual file system. * @param {string} path Path of the directory to create. * @returns {Promise} The newly created directory node. */ mkdir: (path: string) => Promise; /** * Get the content of a file on the Emscripten virtual file system. * @param {string} path Path of the file to read. * @param {string} [flags] Open the file with the specified flags. * @returns {Promise} The content of the requested file. */ readFile: (path: string, flags?: string) => Promise; /** * Remove a directory on the Emscripten virtual file system. * @param {string} path Path of the directory to remove. */ rmdir: (path: string) => Promise; /** * Write a new file to the Emscripten virtual file system. * @param {string} path Path of the new file. * @param {Uint8Array} data The content of the new file. * @param {string} [flags] Open the file with the specified flags. */ writeFile: (path: string, data: ArrayBufferView, flags?: string) => Promise; /** * Unlink a node on the Emscripten virtual file system. If that node was the * last link to a file it is is deleted. * @param {string} path Path of the target node. */ unlink: (path: string) => Promise; } /** A filesystem entry in the Emscripten Virtual File System */ export type FSNode = { id: number; name: string; mode: number; isFolder: boolean; contents?: { [key: string]: FSNode; }; mounted: null | { mountpoint: string; root: FSNode; }; }; /** An Emscripten Filesystem type */ export type FSType = 'NODEFS' | 'WORKERFS'; /** * Configuration settings to be used when mounting Filesystem objects with * Emscripten * */ export type FSMountOptions = T extends 'NODEFS' ? { root: string; } : { blobs?: Array<{ name: string; data: Blob; }>; files?: Array; packages?: Array<{ metadata: any; blob: Blob; }>; }; /** * The configuration settings to be used when starting webR. */ export interface WebROptions { /** * Command line arguments to be passed to R. * Default: `[]`. */ RArgs?: string[]; /** * Environment variables to be made available for the R process. * Default: `{ R_HOME: '/usr/lib/R', R_ENABLE_JIT: 0 }`. */ REnv?: { [key: string]: string; }; /** * The base URL used for downloading R WebAssembly binaries. * Default: `'https://webr.r-wasm.org/[version]/'` */ baseUrl?: string; /** * The repo URL to use when downloading R WebAssembly packages. * Default: `'https://repo.r-wasm.org/` */ repoUrl?: string; /** * The base URL from where to load JavaScript worker scripts when loading * webR with the ServiceWorker communication channel mode. * Default: `''` */ serviceWorkerUrl?: string; /** * The WebAssembly user's home directory and initial working directory. * Default: `'/home/web_user'` */ homedir?: string; /** * Start R in interactive mode? * Default: `true`. */ interactive?: boolean; /** * Set the communication channel type to be used. * Default: `channelType.Automatic` */ channelType?: (typeof ChannelType)[keyof typeof ChannelType]; /** * Create the lazy virtual filesystem entries before starting R? * Default: `true`. */ createLazyFilesystem?: boolean; } /** * The webR class is used to initialize and interact with the webR system. * * Start webR by constructing an instance of the WebR class, optionally passing * an options argument of type {@link WebROptions}. WebR will begin to download * and start a version of R built for WebAssembly in a worker thread. */ export declare class WebR { #private; globalShelter: Shelter; RObject: ReturnType>; RLogical: ReturnType>; RInteger: ReturnType>; RDouble: ReturnType>; RCharacter: ReturnType>; RComplex: ReturnType>; RRaw: ReturnType>; RList: ReturnType>; RPairlist: ReturnType>; REnvironment: ReturnType>; RSymbol: ReturnType>; RString: ReturnType>; RCall: ReturnType>; objs: { baseEnv: REnvironment; globalEnv: REnvironment; null: RNull; true: RLogical; false: RLogical; na: RLogical; }; Shelter: new () => Promise; constructor(options?: WebROptions); /** * @returns {Promise} A promise that resolves once webR has been * intialised. */ init(): Promise; /** * Close the communication channel between the main thread and the worker * thread cleanly. Once this has been executed, webR will be unable to * continue. */ close(): void; /** * Read from the communication channel and return an output message. * @returns {Promise} The output message */ read(): Promise; /** * Flush the output queue in the communication channel and return all output * messages. * @returns {Promise} The output messages */ flush(): Promise; /** * Send a message to the communication channel input queue. * @param {Message} msg Message to be added to the input queue. */ write(msg: Message): void; /** * Send a line of standard input to the communication channel input queue. * @param {string} input Message to be added to the input queue. */ writeConsole(input: string): void; /** Attempt to interrupt a running R computation. */ interrupt(): void; /** * Install a list of R packages from a Wasm binary package repo. * @param {string[]} packages An array of R package names. * @param {InstallPackagesOptions} [options] Options to be used when * installing webR packages. */ installPackages(packages: string[], options?: InstallPackagesOptions): Promise; /** * Destroy an R object reference. * @param {RObject} x An R object reference. */ destroy(x: RObject): Promise; /** * Evaluate the given R code. * * Stream outputs and any conditions raised during exectution are written to * the JavaScript console. * @param {string} code The R code to evaluate. * @param {EvalROptions} [options] Options for the execution environment. * @returns {Promise} The result of the computation. */ evalR(code: string, options?: EvalROptions): Promise; evalRVoid(code: string, options?: EvalROptions): Promise; evalRBoolean(code: string, options?: EvalROptions): Promise; evalRNumber(code: string, options?: EvalROptions): Promise; evalRString(code: string, options?: EvalROptions): Promise; /** * Evaluate the given R code, returning the result as a raw JavaScript object. * @param {string} code The R code to evaluate. * @param {EvalRMessageOutputType} outputType JavaScript type to return the result as. * @param {EvalROptions} [options] Options for the execution environment. * @returns {Promise} The result of the computation. */ evalRRaw(code: string, outputType: 'void', options?: EvalROptions): Promise; evalRRaw(code: string, outputType: 'boolean', options?: EvalROptions): Promise; evalRRaw(code: string, outputType: 'boolean[]', options?: EvalROptions): Promise; evalRRaw(code: string, outputType: 'number', options?: EvalROptions): Promise; evalRRaw(code: string, outputType: 'number[]', options?: EvalROptions): Promise; evalRRaw(code: string, outputType: 'string', options?: EvalROptions): Promise; evalRRaw(code: string, outputType: 'string[]', options?: EvalROptions): Promise; invokeWasmFunction(ptr: EmPtr, ...args: number[]): Promise; FS: { lookupPath: (path: string) => Promise; mkdir: (path: string) => Promise; mount: (type: T, options: FSMountOptions, mountpoint: string) => Promise; readFile: (path: string, flags?: string) => Promise; rmdir: (path: string) => Promise; writeFile: (path: string, data: ArrayBufferView, flags?: string) => Promise; unlink: (path: string) => Promise; unmount: (mountpoint: string) => Promise; }; } /** WebR shelters provide fine-grained control over the lifetime of R objects. */ export declare class Shelter { #private; RObject: ReturnType>; RLogical: ReturnType>; RInteger: ReturnType>; RDouble: ReturnType>; RCharacter: ReturnType>; RComplex: ReturnType>; RRaw: ReturnType>; RList: ReturnType>; RPairlist: ReturnType>; REnvironment: ReturnType>; RSymbol: ReturnType>; RString: ReturnType>; RCall: ReturnType>; /** @internal */ constructor(chan: ChannelMain); /** @internal */ init(): Promise; purge(): Promise; destroy(x: RObject): Promise; size(): Promise; /** * Evaluate the given R code. * * Stream outputs and any conditions raised during exectution are written to * the JavaScript console. The returned R object is protected by the shelter. * @param {string} code The R code to evaluate. * @param {EvalROptions} [options] Options for the execution environment. * @returns {Promise} The result of the computation. */ evalR(code: string, options?: EvalROptions): Promise; /** * Evaluate the given R code, capturing output. * * Stream outputs and conditions raised during exectution are captured and * returned as part of the output of this function. Returned R objects are * protected by the shelter. * @param {string} code The R code to evaluate. * @param {EvalROptions} [options] Options for the execution environment. * @returns {Promise<{result: RObject, output: unknown[]}>} An object * containing the result of the computation and and array of captured output. */ captureR(code: string, options?: EvalROptions): Promise<{ result: RObject; output: unknown[]; }>; }