Usage Search :: in inested expression

What is an elegant way to search through a nested expression for a namespace referenced by an operator ::? For instance:

findnamespaces <- function(expr){
  namespaces <- character()
  if(is.call(expr) && identical(expr[[1]], as.name("::"))){
    namespaces <- deparse(expr[[2]])
  }
  if(!is.name(expr) && !is.atomic(expr)){
    for(i in seq_along(expr)){
      namespaces <- c(namespaces, findnamespaces(expr[[i]]))     
    }
  }
  return(unique(namespaces))
}

This seems to be a trick for the main cases:

code <- "foo::test(bar::test)\n function(x){return(baz::test)}"
findnamespaces(parse(text=code))
[1] "foo" "bar" "baz"

However, it feels a bit hacked. Is there a more proprietary method that does something like this? It seems to be CMD checkneeded to make sure all Suggests:packages are correctly declared in DESCRIPTION.

+4
source share

Source: https://habr.com/ru/post/1548285/


All Articles