How to properly use functions from other packages

2019-01-22 19:27发布

I am a bit confused about this. I have an R package that has a small function (not a mayor part of the package) in which the principal function of the psych package is called. How do I correctly specify this in DESCRIPTION and NAMESPACE?

Setting Depends: psych in DESCRIPTION makes sure the psych package is loaded every time my package is loaded. This works, but it seems redundant for such a small part of my package.

Setting Suggests: psych and entering a require("psych") in the function is what I do now, however this does not work if psych is not installed, and seems to be the wrong way of doing this (writing R extensions says that suggest is meant mainly for examples).

I think I need to import the function. I tried setting Imports: psych in DESCRIPTION and importFrom(psych,"principal") in NAMESPACE. This works, but on a computer that does not has psych installed it gives an error when loading my package.

1条回答
Emotional °昔
2楼-- · 2019-01-22 20:03

The basic question you need to answer is: "do you want the function to be available to all users of the package without further effort?". If yes, then use imports + the appropriate namespace declarations, if no, then use suggests and print an informative error message if require("psych") returns FALSE.

I don't understand your import related complaint that: "but on a computer that does not has psych installed it gives an error when loading my package". This is also true if your package is in depends!

查看更多
登录 后发表回答