Closed Aariq closed 3 months ago
I think to fully close #10 we should implement this using rlang::check_installed()
.
I think we should take advantage of
rlang::check_installed("pkg")
here to provide a neater user experience
I tried using this instead, but the error message from a targets pipeline was not as nice. The code in tar_terra_*()
will never be run in an interactive session, which means the user will never see the benefits of rlang::check_installed()
(prompting to install missing package) anyway.
> foo <- function() {
+ if (!requireNamespace("asdf")) {
+ stop("package 'asdf' is required", call. = FALSE)
+ }
+ }
> callr::r(foo)
Error:
! in callr subprocess.
Caused by error:
! package 'asdf' is required
ℹ See `$stderr` for standard error.
Type .Last.error to see the more details.
> foo2 <- function() {
+ rlang::check_installed("asdf")
+ }
> callr::r(foo2)
Error:
! in callr subprocess.
Caused by error in `(function () …`:
! The package "asdf" is required.
Type .Last.error to see the more details.
Great reprex! I'd be tempted to submit that as a bug to rlang - seems like unexpected behaviour, it doesn't even pass the "reason" argument through when specifying. Good catch.
I was thinking that it might be worthwhile passing through a function to manage this (see foo3
that I wrote) - just wrapping the code you wrote. What do you think? It's a small thing, but we use it a couple of times and I think it will also make testing that component of the package easier. Let me know your thoughts :)
foo <- function() {
if (!requireNamespace("asdf")) {
stop("package 'asdf' is required", call. = FALSE)
}
}
callr::r(foo)
#> Error: ! in callr subprocess.
#> Caused by error:
#> ! package 'asdf' is required
foo2 <- function() {
rlang::check_installed("asdf",
reason = "Please install asdf")
}
callr::r(foo2)
#> Error: ! in callr subprocess.
#> Caused by error in `(function () …`:
#> ! The package "asdf" is required Please install asdf
foo3 <- function() {
.check_pkg_installed <- function(pkg){
if (!requireNamespace(pkg)) {
cli::cli_abort("package {.pkg {pkg}} is required",
call = rlang::caller_env())
}
}
.check_pkg_installed("asdf")
}
callr::r(foo3)
#> Error: ! in callr subprocess.
#> Caused by error in `(function () …`:
#> ! package asdf is required
callr::r(foo)
#> Error: ! in callr subprocess.
#> Caused by error:
#> ! package 'asdf' is required
Created on 2024-03-20 with reprex v2.1.0
I was thinking that it might be worthwhile passing through a function to manage this
Yeah! Saves the trouble of remembering to change the package name in two places. Do you want to implement this in this PR?
Yup, I'll make a couple of changes now.
Closes #10. Moves
terra
to Suggests and moves check forterra
to top oftar_*()
functions.Also moves validation of
filetype
totar_*()
functions. I think this might make error traces more readable to users, but haven't really tested thoroughly