diff --git a/NEWS.md b/NEWS.md index 8a38ff5e..5097676f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -14,6 +14,8 @@ * The `rwasm` R package is now installed into the system library as part of the webR development Docker container (#443, r-wasm/actions#10). +* `webR.installPackages()` now correctly handles both `string` and `string[]` arguments for package names and binary repositories (#437). + # webR 0.3.3 ## New features diff --git a/src/webR/webr-chan.ts b/src/webR/webr-chan.ts index 286a19e3..020ce09f 100644 --- a/src/webR/webr-chan.ts +++ b/src/webR/webr-chan.ts @@ -26,10 +26,10 @@ export interface CallRObjectMethodMessage extends Message { */ export interface InstallPackagesOptions { /** - * The R package repository from which to download packages. + * The R package repositories from which to download packages. * Default: The configured default webR package repository. */ - repos?: string; + repos?: string | string[]; /** * If `true`, do not output downloading messages. * Default: `false`. @@ -44,9 +44,9 @@ export interface InstallPackagesOptions { /** @internal */ export interface InstallPackagesMessage extends Message { - type: 'installPackage'; + type: 'installPackages'; data: { - name: string; + name: string | string[]; options: InstallPackagesOptions; }; } diff --git a/src/webR/webr-main.ts b/src/webR/webr-main.ts index bfb88b75..cba7f12e 100644 --- a/src/webR/webr-main.ts +++ b/src/webR/webr-main.ts @@ -363,21 +363,20 @@ export class WebR { } /** - * Install a list of R packages from a Wasm binary package repo. - * @param {string[]} packages An array of R package names. + * Install a list of R packages from Wasm binary package repositories. + * @param {string | string[]} packages An string or array of strings + * containing R package names. * @param {InstallPackagesOptions} [options] Options to be used when * installing webR packages. */ - async installPackages(packages: string[], options?: InstallPackagesOptions) { + async installPackages(packages: string | string[], options?: InstallPackagesOptions) { const op = Object.assign({ quiet: false, mount: true }, options); - for (const name of packages) { - const msg = { type: 'installPackage', data: { name, options: op } }; - await this.#chan.request(msg); - } + const msg = { type: 'installPackages', data: { name: packages, options: op } }; + await this.#chan.request(msg); } /** diff --git a/src/webR/webr-worker.ts b/src/webR/webr-worker.ts index b2bb7916..8013524d 100644 --- a/src/webR/webr-worker.ts +++ b/src/webR/webr-worker.ts @@ -421,11 +421,15 @@ function dispatch(msg: Message): void { break; } - case 'installPackage': { + case 'installPackages': { const msg = reqMsg as InstallPackagesMessage; + let pkgs = msg.data.name; + let repos = msg.data.options.repos ? msg.data.options.repos : _config.repoUrl; + if (typeof pkgs === "string") pkgs = [ pkgs ]; + if (typeof repos === "string") repos = [ repos ]; evalR(`webr::install( - "${msg.data.name}", - repos = "${msg.data.options.repos ? msg.data.options.repos : _config.repoUrl}", + c(${pkgs.map((r) => '"' + r + '"').join(',')}), + repos = c(${repos.map((r) => '"' + r + '"').join(',')}), quiet = ${msg.data.options.quiet ? 'TRUE' : 'FALSE'}, mount = ${msg.data.options.mount ? 'TRUE' : 'FALSE'} )`);