assertthat icon indicating copy to clipboard operation
assertthat copied to clipboard

feature request: has_package()

Open ijlyttle opened this issue 7 years ago • 3 comments

This idea follows from the "Suggests" section of the R-pkgs book, where you provide the code:

if (!requireNamespace("pkg", quietly = TRUE)) {
  stop("Package \"pkg\" needed for this function to work. Please install it.",
    call. = FALSE)
}

Could this be useful as a new assertion,has_package()?

assertthat::has_package("dplyr")

I'd be happy to make the PR if you think this might be useful. If so, some questions:

  1. Name: has_package(), has_packages(), other?
  2. Should the argument be ... as unnamed, or packages which would be a character-vector of package names, or other?

ijlyttle avatar Jun 27 '18 20:06 ijlyttle

Upon further reflection, I see has_package() would not be the right name. What I am proposing is more like assert_package().

(has_package() would be a vectorized wrapper to requireNamespace())

There are not any assert_ functions beyond assert_that(), so I can see where this might not be the right way to go about this.

ijlyttle avatar Jun 27 '18 20:06 ijlyttle

I have developed this function, now called assert_packages(), in one of my packages: https://github.com/vegawidget/vegawidget/blob/cran-release-0.1.0/R/utils-package.R

I have found it useful, so I'd be happy to make a PR here if you like.

ijlyttle avatar Jan 27 '19 13:01 ijlyttle

Sure, a PR would be great

hadley avatar Jan 28 '19 15:01 hadley