[R-pkg-devel] Analysing namespace reverse dependencies
On Sat, 9 Mar 2019, I?aki Ucar wrote:
On Sat, 9 Mar 2019 at 15:07, Roger Bivand <Roger.Bivand at nhh.no> wrote:
Hi, Is anyone aware of tools permitting the listing of functions in the namespace of package A used by package B when package B declares that it depends on A, suggests A, or imports(A) without specifying the functions from A that B is using? If B imports functions from A, and uses importsFrom() in its NAMESPACE, the information can be recovered by inspecting its NAMESPACE file (assuming that the entries are correct), but not otherwise.
I don't know of any specific tool, but the following function may help:
inspect_functions <- function(pkg, from) {
regex <- paste0("(", paste(from, collapse="|"), ")::[[:alnum:]._]*")
ns <- getNamespace(pkg)
x <- sapply(ls(ns, all.names=TRUE), function(x) {
f <- get(x, envir=ns)
if (!is.function(f))
return(NULL)
b <- unlist(strsplit(as.character(body(f)), "\n"))
unique(regmatches(b, regexpr(regex, b)))
})
x[sapply(x, Negate(length))] <- NULL
x
}
Thanks! This gives function calls directly using inspect_functions("B",
"A"), but as of now does not catch methods, which are reported by
pkgapi::map_package(). Very useful to compare insights!
Roger
Example:
inspect_functions("units", c("pillar", "xml2"))
#> $.read_ud_db
#> [1] "xml2::read_xml"
#>
#> $.read_ud_db_symbols
#> [1] "xml2::read_xml" "xml2::xml_find_all" "xml2::xml_contents"
#>
#> $.ud_db_xml_list_as_dataframe
#> [1] "xml2::xml_children" "xml2::xml_length" "xml2::xml_find_all"
#> [4] "xml2::xml_text"
#>
#> $format_type_sum.type_sum_units
#> [1] "pillar::style_subtle"
#>
#> $pillar_shaft.mixed_units
#> [1] "pillar::new_pillar_shaft_simple"
#>
#> $pillar_shaft.units
#> [1] "pillar::new_pillar_shaft_simple"
#>
#> $valid_udunits_prefixes
#> [1] "xml2::xml_children" "xml2::xml_find_all" "xml2::xml_text"
#> [4] "xml2::xml_double"
I?aki
Roger Bivand Department of Economics, Norwegian School of Economics, Helleveien 30, N-5045 Bergen, Norway. voice: +47 55 95 93 55; e-mail: Roger.Bivand at nhh.no https://orcid.org/0000-0003-2392-6140 https://scholar.google.no/citations?user=AWeghB0AAAAJ&hl=en