Skip to content

Declare when Suggested Packages are Needed

License

Unknown, MIT licenses found

Licenses found

Unknown
LICENSE
MIT
LICENSE.md
Notifications You must be signed in to change notification settings

owenjonesuob/suggests

Repository files navigation

suggests: Declare when Suggested Packages are Needed

CRAN status R-CMD-check Codecov test coverage

By adding dependencies to the "Suggests" field of a package's DESCRIPTION file, and then declaring that they are needed within any dependent functionality, you can often significantly reduce the number of "hard" dependencies (i.e. Depends/Imports) required by your package.

{suggests} aims to make that workflow as minimal and painless as possible, primarily via the need() function, which provides a lightweight, simple, speedy way to prompt your users to install missing suggested packages.

Installation

The current release is available from CRAN:

install.packages("suggests")

Or if you'd like all the latest updates, you can install the development version:

# From GitHub...
remotes::install_github("owenjonesuob/suggests")

# ... or from R-universe
install.packages("suggests", repos = "https://owenjonesuob.r-universe.dev")

Usage

You can declare that one or more packages are needed for subsequent functionality with need():

read_data <- function(path, clean_names = FALSE) {

  # Call suggests::need() as early as possible, to avoid wasted work
  if (isTRUE(clean_names))
    suggests::need("janitor")

  output <- utils::read.csv(path)

  if (isTRUE(clean_names))
    output <- janitor::clean_names(output)

  output
 }

You can also make sure a minimum version is available by appending >=[version]:

need(
  "dplyr>=1.0.0",
  "tidyr"
)

Additionally, find_pkgs() is a quick-and-dirty diagnostic tool to find dependency usage within top-level expressions (e.g. declared functions) in R scripts within a development package. This can be useful when looking for good candidates for dependencies which could be moved from Imports to Suggests in the DESCRIPTION file.

Given a path, it returns a data frame with one row per distinct top-level expression where a package is used. Packages used in the fewest places are listed first.

find_deps(system.file("demopkg", package = "suggests"))
  package_used              in_file          in_expr
1        stats R/median_first_ten.R median_first_ten
2        tools R/median_first_ten.R median_first_ten
3        utils R/median_first_ten.R median_first_ten

Alternatives

The *_installed() functions from {rlang} provide similar functionality - more flexible, but less lightweight than this package.

You could avoid the need for any package-checking dependencies, if you're willing to write slightly more code yourself! See the Dependencies: In Practice chapter of R Packages (Wickham & Bryan).

About

Declare when Suggested Packages are Needed

Resources

License

Unknown, MIT licenses found

Licenses found

Unknown
LICENSE
MIT
LICENSE.md

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages