r-lib / rlang

Low-level API for programming with R
https://rlang.r-lib.org
Other
502 stars 140 forks source link

Use `check_installed()` for remote packages #1650

Open rossellhayes opened 1 year ago

rossellhayes commented 1 year ago

Is it possible to use check_installed() to check for and install a remote package? If I use a remote only package, e.g. rlang::check_installed("hadley/emo"), it fails to detect when the package is installed. If I try rlang::check_installed("emo"), it fails to install the package when it is not installed.

With hadley/emo uninstalled

options(rlang_interactive = TRUE)
mockery::stub(rlang::check_installed, "utils::menu", 1)

rlang::check_installed("emo")
#> ℹ The package "emo" is required.
#> ✖ Would you like to install it?
#> ℹ Loading metadata database
#> ✔ Loading metadata database ... done
#> 
#> Error: ! error in pak subprocess
#> Caused by error: 
#> ! Could not solve package dependencies:
#> * emo: Can't find package called emo.

Created on 2023-08-11 with reprex v2.0.2

With hadley/emo installed

rlang::check_installed("hadley/emo")
#> Error:
#> ! The package "hadley/emo" is required.

Created on 2023-08-11 with reprex v2.0.2

olivroy commented 1 year ago

What I do is the following.

rlang::check_installed("emo", action = \(pkg, ...) pak::pak("hadley/emo")) 

You could pass a more sophisticated function, but at least, it directs you to the correct location.

Edit: The check_installed() docs could probably be improved, https://github.com/r-lib/rlang/issues/1480#issuecomment-1238451659, #1628