Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

dependencies - For an R package, how to efficiently move a package from Depends to Imports

Say I have a package that has 5 packages in Depends of the DESCRIPTION file and I have just realised it is not a good practice to have this many packages in Depends due to inevitable import clashes that are starting to pop up as the number of function imports are increasing. I'd like to move, say only package pkg to Imports but I have no clue which functions of pkg are being used in my package. Ideally, I should have unit tests with full coverage of the package source code and by simply removing pkg from the dependencies, I will identify the pkg-specific imports from the test errors of could not find function "foo". But unfortunately, I do not have that breadth of test coverage. I was wondering if there is a more efficient way than going through all the package code to identify these imports.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

That is very straightforward. Change

Depends: pkgA, pkgB, pgC

to

Imports: pkgA, pkgB, pgC

and also add this to the NAMESPACE file:

import("pkgA")
import("pkgB")
import("pkgC")

which will globally import all exported symbols so you can continue as before.

You can also selectively import via

importFrom("pkgA", "func1", "func2", "func3")

and if you run R CMD check it will actually (very helpfully) tell you which functions need this. The second method is somewhat more precise but a little more work to set up.

And I don't think we have a tool to remove 'spurious imports'. Finding which imports may be unused may be something you have to check manually (but trying to remove one and seeing if it still builds + checks fine).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...