[R] Network from package functions

Romain Francois romain.francois at dbmail.com
Thu Jul 23 09:49:11 CEST 2009


Hi,

This post might get you started:
http://romainfrancois.blog.free.fr/index.php?post/2009/03/07/What-functions-are-called-by-my-function

 > tail( callees( nls ), 20 )
functions
            names          new.env      nls.control         nlsModel
                8                1                2                2
nlsModel.plinear     nls_port_fit             nrow             NROW
                1                1                1                1
          numeric     parent.frame            paste            range
                1                2                7                1
              rep           sapply           sQuote             stop
                3                2                2                6
       substitute           switch         tryCatch          warning
                3                2                1                5

Note that it gives you the special and builtins as well, so this like 
"(", "[", "{" which you can filter out using something like this :

 > funs <- callees( nls )
 > funs[ grepl( "[.[:alpha:]]", names(funs) ) ]
functions
         all.vars              any     as.character       as.formula
                2                3                3                2
       as.integer          as.list          as.name           assign
                1                4                3                1
             attr                c            .Call            class
                6                1                1                1
             diff      environment             eval      eval.parent
                1                4                5                1

A bit more work would be required to filter out the ones you consider 
"trivial", maybe this can help :

 > sapply( names(funs), find )
$`<`
[1] "package:base"

$`<-`
[1] "package:base"

...

Too bad find only works with one object at once ... I have this 
simplified version in package svTools :

# Similar to "find" but `what` can be a vector
# also, this one only searches in packages (position of the search path
# matching '^package:') and only gives one result per what
".find.multiple" <- function (what){
     stopifnot(is.character(what))
     sp <- grep( "^package:", search(), value = TRUE)
     out <- rep( "" , length(what))
     for (i in sp) {
         ok <- what %in% ls(i, all.names = TRUE) & out == ""
         out[ok] <- i
         if (all(out!="")) break
     }
     names(out) <- what
     sub("^package:", "", out)
}

 > .find.multiple( names( funs ) )
                <               <-               ==                >
           "base"           "base"           "base"           "base"
                |               ||                -                :
           "base"           "base"           "base"           "base"
...

but this is probably not good enough since it ignores namespaces, 
lexical scoping, ...


Another way is to use the -- about to be released -- "parser" package 
(see highlight project on r-forge: 
http://r-forge.r-project.org/projects/highlight/)

 > require( parser )
 > data <- attr( parser( "/tmp/nls.txt" ), "data" )
 > data[ data$token.desc == "SYMBOL_FUNCTION_CALL", "text" ]
   [1] "parent.frame"     "nls.control"      "c"
   [4] "as.formula"       "match.arg"        "is.list"
   [7] "is.environment"   "stop"             "match.call"
  [10] "all.vars"         "length"           "all.vars"
...

but again this is only based on the parser and does not care about 
namespaces or scoping

Anyway, hope this helps.

Romain


On 07/23/2009 08:28 AM, Andrej-Nikolai Spiess wrote:
> Dear R-helpers,
>
> does anyone know of some package/function that can build a network from
> the functions that are implemented in a package,
> i.e. visualize the cross-references from one function to another in the
> same or some dependent package?
> An example would be a function like 'nls' on top of the hierarchy and
> then a network of nodes from the functions that are called within 'nls'
> such as
> 'getInitial', 'nlsModel', 'nls_port_fit' etc. Important maybe also some
> way to tweak such as not to include<  trivial>  functions such as
> 'as.formula'.
>
> Many thanks in advance,
> Andrej
>
> _____________________________________
> Dr. rer. nat. Andrej-Nikolai Spiess (Dipl. Biol.)
> Department of Andrology
> University Hospital Hamburg-Eppendorf
> Martinistr. 52
> 20246 Hamburg
> phone: +49-40-7410-51585
> fax: +49-40-7410-51554
> email: a.spiess at uke.de



-- 
Romain Francois
Independent R Consultant
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://tr.im/tlNb : RGG#155, 156 and 157
|- http://tr.im/rw0p : useR! slides
`- http://tr.im/rw0b : RGG#154: demo of atomic functions




More information about the R-help mailing list