8

Concrete example:

In my package, one of my functions uses grep form the base package.

Should I explicitly import grep or would that just introduce useless dependencies? I mean, everyone already has the base package, right?

For the sake of illustration:

#' Group_by with regex based column selection
#' Similar to `group_by` but grouping columns are selected with a regex.
#' @importFrom dplyr group_by
#' @importFrom base grep
#' @export
group_at <- function(df, pattern)
  group_by_(df, .dots=grep(names(df), pattern=pattern, value=T))
asachet
  • 6,620
  • 2
  • 30
  • 74
  • 1
    If you needed to import `grep`, you'd need to import everything, even `function` and `(`, which also live in base. That would become tedious pretty soon. – Roland Dec 01 '16 at 07:19

2 Answers2

11

From the Writing R Extensions manual sec.1.1.3:

It makes no sense to declare a dependence on R without a version specification, nor on the package base: this is an R package and package base is always available.

Hong Ooi
  • 56,353
  • 13
  • 134
  • 187
  • 1
    Thanks! I actually discovered that roxygen throws an error message saying that `base` should not be imported. The package then fails to load because of an illegal NAMESPACE containing base (`asNamespace` : `operation not allowed on base namespace`). – asachet Nov 30 '16 at 12:15
2

By deleting the following terms associated to the package base from NAMESPACE

  import(base)  , 
 importFrom(base,system.file)

and in the DESCRIPTION file, delete the following term.

 Imports: base  

Then the following error vanished:

 preparing package for lazy loading
Error in asNamespace(ns, base.OK = FALSE) : 
  operation not allowed on base namespace
ERROR: lazy loading failed for package 'aa'
Jean Billie
  • 189
  • 10
  • There is something odd going on here. I am writing a package that needs `month.abb`. RMD check then says: : no visible global function definition for ‘month.abb’ Undefined global functions or variables: month.abb Consider adding importFrom("base", "month.abb") to your NAMESPACE file. – JeremyC Apr 27 '19 at 06:43