Search code examples
rr-package

Optional dependency in R package


I have some code like this in my R package:

#' Update font family for geom_text and geom_label
#' @param family A valid font family name
#' @importFrom ggplot2 update_geom_defaults
#' @export
#' 
update_geom_font <- function(family=""){
  update_geom_defaults("text", list(family = family))
  update_geom_defaults("label", list(family = family))

  if (requireNamespace("ggrepel", quietly = TRUE)) {
    update_geom_defaults(ggrepel::GeomTextRepel, list(family = family))
    update_geom_defaults(ggrepel::GeomLabelRepel, list(family = family))
  }
}

The two lines of code should only run if package ggrepel is installed on the user's system else it's ignored. I do not want to add a dependency on ggrepel by declaring it under imports or suggests. The package works, but it does not pass R CMD check.

❯ checking dependencies in R code ... WARNING
  '::' or ':::' import not declared from: ‘ggrepel’
  'loadNamespace' or 'requireNamespace' call not declared from: ‘ggrepel’

How can I get R CMD check to ignore this?


Solution

  • "How can I get R CMD check to ignore this?" You can't. If you use another package in yours, you need to mention it in the DESCRIPTION file. Your usage corresponds to listing it in the Suggests: list.

    You said in a comment that "If I include it in the DESCRIPTION, then that package has to be available on the system for checks to work, and I do not want to increase dependencies." That's true in the sense that you'll get a note during the check if it's not available, but that doesn't matter to your users. They don't need it to install your package. And if it's a package that's not even available on CRAN, then you need to tell your users where to get it, by listing an alternative source in Additional_repositories:.