How to load only specific functions from a package

2020-07-09 02:27发布

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?

标签: r
2条回答
狗以群分
2楼-- · 2020-07-09 03:05

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()

查看更多
一纸荒年 Trace。
3楼-- · 2020-07-09 03:22

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).

查看更多
登录 后发表回答