[R] Clustering Functions used by Reverse-Dependencies

Leo Mada |eo@m@d@ @end|ng |rom @yon|c@eu
Fri Mar 1 01:28:18 CET 2024


Dear Ivan,

Thank you very much for this interesting information.

Regarding:
"For well-behaved packages that declare their dependencies correctly,
parsing the NAMESPACE for importFrom() and import() calls should give
you the explicit imports."

I did learn something new (I am not very experienced in package writing). Unfortunately, Roxygen2 as of the current version still suggests to use the pkg::fname approach:
"If you are using just a few functions from another package, we recommending adding the package to the Imports: field of the DESCRIPTION file and calling the functions explicitly using ::, e.g., pkg::fun()."
https://roxygen2.r-lib.org/articles/namespace.html

Regarding analysing the actual code: it is good to know that CMD check has also some functionality. I will look into it, when I find some free time.

tools:::.check_packages_used is a few pages of code. On the other hand, the help page for codetools::checkUsage is quite cryptic. But it's good to know at least where to look.

Sincerely,

Leonard

________________________________
From: Ivan Krylov <ikrylov using disroot.org>
Sent: Wednesday, February 28, 2024 10:36 AM
To: Leo Mada via R-help <r-help using r-project.org>
Cc: Leo Mada <leo.mada using syonic.eu>
Subject: Re: [R] Clustering Functions used by Reverse-Dependencies

� Sat, 24 Feb 2024 03:08:26 +0000
Leo Mada via R-help <r-help using r-project.org> �����:

> Are there any tools to extract the function names called by
> reverse-dependencies?

For well-behaved packages that declare their dependencies correctly,
parsing the NAMESPACE for importFrom() and import() calls should give
you the explicit imports. (What if the package imports the whole
dependency? The safe assumption is that all functions are used, but it
comes with false positives. You could also walk the package code
looking for function names that may belong to the imported package, but
that may involve both false positives and false negatives.)

For the rest of the imports and uses of weak dependencies, you'll have
to walk the package code looking for the uses of the `::` operator. See
how R CMD check walks the package code in functions
tools:::.check_packages_used and codetools::checkUsage.

A less-well-behaved package can always load a namespace during runtime
and choose the functions to call depending on the phase of the moon or
weather on Jupiter. For these, like for the halting problem, there's no
general solution: the package could be written to say, "if Leonard's
function says I'm about to call foo::bar, I won't do it, otherwise I
will".

--
Best regards,
Ivan

	[[alternative HTML version deleted]]



More information about the R-help mailing list