Skip to content

Commit

Permalink
Use find.package() in library/require shim
Browse files Browse the repository at this point in the history
Ensures that errors encountered during require() are displayed to
the end user rather than hidden during package existence testing.

Also, early return if package not installed during hook.
  • Loading branch information
georgestagg committed Mar 6, 2024
1 parent 06fbb3c commit 108a4a3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/webr/R/handlers.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ prompt_download <- function(package, show_menu) {

if (download == 1) {
webr::install(package)
return(invisible(TRUE))
return(invisible(length(find.package(package, quiet = TRUE)) > 0))
}

return(invisible(FALSE))
Expand Down
12 changes: 8 additions & 4 deletions packages/webr/R/library.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
library_shim <- function(pkg, ..., show_menu = getOption("webr.show_menu")) {
show_menu <- ifelse(is.null(show_menu), FALSE, show_menu)
package <- as.character(substitute(pkg))
if (!base::require(package, character.only = TRUE, quietly = TRUE)) {
prompt_download(package, show_menu)
if (length(find.package(package, quiet = TRUE)) == 0) {
if (!prompt_download(package, show_menu)) {
return(invisible(NULL))
}
}
base::library(package, character.only = TRUE, ...)
}
Expand All @@ -39,8 +41,10 @@ library_shim <- function(pkg, ..., show_menu = getOption("webr.show_menu")) {
require_shim <- function(pkg, ..., show_menu = getOption("webr.show_menu")) {
show_menu <- ifelse(is.null(show_menu), FALSE, show_menu)
package <- as.character(substitute(pkg))
if (!base::require(package, character.only = TRUE, quietly = TRUE)) {
prompt_download(package, show_menu)
if (length(find.package(package, quiet = TRUE)) == 0) {
if (!prompt_download(package, show_menu)) {
return(invisible(NULL))
}
}
base::require(package, character.only = TRUE, ...)
}

0 comments on commit 108a4a3

Please sign in to comment.