Skip to content

Commit e6feb33

Browse files
committed
Update guidance on using suggested packages, vis-a-vis rlang
Closes #1542
1 parent af60dfe commit e6feb33

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

R/package.R

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,17 +133,30 @@ how_to_use <- function(package, type) {
133133
"Are you sure you want {ui_field('Depends')}? \\
134134
{ui_field('Imports')} is almost always the better choice."
135135
),
136-
suggests = {
137-
code <- glue("requireNamespace(\"{package}\", quietly = TRUE)")
138-
ui_todo("Use {ui_code(code)} to test if package is installed")
139-
code <- glue("{package}::fun()")
140-
ui_todo("Then directly refer to functions with {ui_code(code)}")
141-
},
136+
suggests = suggests_usage_hint(package),
142137
enhances = "",
143138
linkingto = show_includes(package)
144139
)
145140
}
146141

142+
suggests_usage_hint <- function(package) {
143+
imports_rlang <- desc::desc_has_dep("rlang", type = "Imports", proj_get())
144+
if (imports_rlang) {
145+
code1 <- glue('rlang::is_installed("{package}")')
146+
code2 <- glue('rlang::check_installed("{package}")')
147+
ui_todo("
148+
In your package code, use {ui_code(code1)} or {ui_code(code2)} to test \\
149+
if {package} is installed")
150+
code <- glue("{package}::fun()")
151+
ui_todo("Then directly refer to functions with {ui_code(code)}")
152+
} else {
153+
code <- glue("requireNamespace(\"{package}\", quietly = TRUE)")
154+
ui_todo("Use {ui_code(code)} to test if package is installed")
155+
code <- glue("{package}::fun()")
156+
ui_todo("Then directly refer to functions with {ui_code(code)}")
157+
}
158+
}
159+
147160
show_includes <- function(package) {
148161
incl <- path_package("include", package = package)
149162
h <- dir_ls(incl, regexp = "[.](h|hpp)$")

tests/testthat/_snaps/package.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,21 @@
1515
Warning:
1616
Package 'withr' is already listed in 'Imports' in DESCRIPTION, no change made.
1717

18+
# use_package(type = 'Suggests') guidance w/o and w/ rlang
19+
20+
Code
21+
use_package("withr", "Suggests")
22+
Message
23+
v Adding 'withr' to Suggests field in DESCRIPTION
24+
* Use `requireNamespace("withr", quietly = TRUE)` to test if package is installed
25+
* Then directly refer to functions with `withr::fun()`
26+
27+
---
28+
29+
Code
30+
use_package("purrr", "Suggests")
31+
Message
32+
v Adding 'purrr' to Suggests field in DESCRIPTION
33+
* In your package code, use `rlang::is_installed("purrr")` or `rlang::check_installed("purrr")` to test if purrr is installed
34+
* Then directly refer to functions with `purrr::fun()`
35+

tests/testthat/test-package.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ test_that("use_package() guides new packages but not pre-existing ones", {
1414
})
1515
})
1616

17+
test_that("use_package(type = 'Suggests') guidance w/o and w/ rlang", {
18+
create_local_package()
19+
withr::local_options(usethis.quiet = FALSE)
20+
21+
expect_snapshot(use_package("withr", "Suggests"))
22+
ui_silence(use_package("rlang"))
23+
expect_snapshot(use_package("purrr", "Suggests"))
24+
})
1725

1826
# use_dev_package() -----------------------------------------------------------
1927

0 commit comments

Comments
 (0)