Skip to content

Commit

Permalink
Fix using character.only = TRUE in package shims (#417)
Browse files Browse the repository at this point in the history
* Fix using `character.only = TRUE` in package shims

* Add test for installing packages via webr shim

* Update NEWS.md
  • Loading branch information
georgestagg committed Apr 24, 2024
1 parent 58f4d16 commit 6865be3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Expand Up @@ -6,6 +6,10 @@

* An `RObject.class()` method has been added, returning an `RCharacter` object with the names of the classes from which the given R object inherits. This has been implemented using R's `class()` function, and so the implicit class is similarly returned when the R object has no `class` attribute.

## Bug Fixes

* Fix installing packages via shim with `character.only = TRUE` (#413).

# webR 0.3.2

## New features
Expand Down
8 changes: 6 additions & 2 deletions packages/webr/R/library.R
Expand Up @@ -33,7 +33,9 @@ library_shim <- function(pkg, ..., show_menu = getOption("webr.show_menu")) {
return(invisible(NULL))
}
}
base::library(package, character.only = TRUE, ...)
args <- list(package, character.only = TRUE, ...)
args <- args[!duplicated(names(args))]
do.call(base::library, args)
}

#' @rdname library_shim
Expand All @@ -46,5 +48,7 @@ require_shim <- function(pkg, ..., show_menu = getOption("webr.show_menu")) {
return(invisible(NULL))
}
}
base::require(package, character.only = TRUE, ...)
args <- list(package, character.only = TRUE, ...)
args <- args[!duplicated(names(args))]
do.call(base::require, args)
}
19 changes: 13 additions & 6 deletions src/tests/webR/webr-worker.test.ts
Expand Up @@ -15,8 +15,7 @@ beforeAll(async () => {

describe('Download and install binary webR packages', () => {
test('Install packages via evalR', async () => {
// eslint-disable-next-line @typescript-eslint/no-empty-function
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => { });
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => null);

// Downloading and extracting a .tgz package
await webR.evalR(
Expand All @@ -36,8 +35,7 @@ describe('Download and install binary webR packages', () => {
});

test('Install packages quietly', async () => {
// eslint-disable-next-line @typescript-eslint/no-empty-function
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => { });
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => null);
await webR.evalR(
'webr::install("Matrix", repos="https://repo.r-wasm.org/", mount = FALSE, quiet = TRUE)'
);
Expand All @@ -46,13 +44,22 @@ describe('Download and install binary webR packages', () => {
});

test('Install packages via API', async () => {
// eslint-disable-next-line @typescript-eslint/no-empty-function
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => { });
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => null);
await webR.installPackages(['MASS'], { mount: false });
const pkg = (await webR.evalR('"MASS" %in% library(MASS)')) as RLogical;
expect(await pkg.toBoolean()).toEqual(true);
warnSpy.mockRestore();
});

test('Install packages via package shim', async () => {
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => null);
const pkg = (await webR.evalR(`
webr::shim_install()
"rlang" %in% library("rlang", character.only = TRUE, show_menu = FALSE, quiet = TRUE)
`)) as RLogical;
expect(await pkg.toBoolean()).toEqual(true);
warnSpy.mockRestore();
});
});

describe('Test webR virtual filesystem', () => {
Expand Down

0 comments on commit 6865be3

Please sign in to comment.