8

I have a package that uses the trim() function in the gdata package. I literally use nothing else from the package and (as some of you may have seen) it overwrites some functionality of base R that I need.

Is there a way to load only one function rather than the whole package?

Brandon Bertelsen
  • 43,807
  • 34
  • 160
  • 255

2 Answers2

15

Sure. Just use an importFrom directive in your NAMESPACE file (as described here in R-exts).

importFrom(gdata, trim)

OP EDIT: As of R 3.2.0 there's now a base function: trimws()

Brandon Bertelsen
  • 43,807
  • 34
  • 160
  • 255
Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
-1

Now I get it: from another post: It means the package (In this case R) can access the package functions/objects, but the user can not without explicitly loading the tools package where as stats, graphics, etc. are loaded and ready to go for the user.

So -- the formally specified import (in which you specify 'Imports: survival' in the description file and also 'importFrom(survival, Surv)' in the NAMESPACE file does indeed work, but then, without also adding 'Surv' to the list of 'export' -ed objects, the function 'Surv' is only available to the code inside the package but not to the user (and hence not available to examples in vignettes either).

izmirlig
  • 9
  • 4