Skip to content

Commit

Permalink
Handle array arguments in webR.installPackages()
Browse files Browse the repository at this point in the history
  • Loading branch information
georgestagg committed Jun 17, 2024
1 parent 547484c commit 1dee571
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/webR/webr-chan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand All @@ -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;
};
}
Expand Down
13 changes: 6 additions & 7 deletions src/webR/webr-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
10 changes: 7 additions & 3 deletions src/webR/webr-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'}
)`);
Expand Down

0 comments on commit 1dee571

Please sign in to comment.