Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8366847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:00:58+00:00 2026-06-09T13:00:58+00:00

Possible Duplicate: Generating a Call Graph in R I’d like to systematically analyze a

  • 0

Possible Duplicate:
Generating a Call Graph in R

I’d like to systematically analyze a given function to find out which other functions are called within that very function. If possible, recursively.

I came across this function in a blog post by milktrader with which I can do something similar for packages (or namespaces)

listFunctions <- function(
    name,
    ...
){ 
    name.0  <- name
    name    <- paste("package", ":", name, sep="")
    if (!name %in% search()) {
        stop(paste("Invalid namespace: '", name.0, "'"))
    }
    # KEEP AS REFERENCE       
#    out <- ls(name)
    funlist <- lsf.str(name)
    out     <- head(funlist, n=length(funlist))
    return(out)
}

> listFunctions("stats")
  [1] "acf"                  "acf2AR"               "add.scope"           
  [4] "add1"                 "addmargins"           "aggregate"           
  [7] "aggregate.data.frame" "aggregate.default"    "aggregate.ts"        
 [10] "AIC"                  "alias"                "anova"               
....
[499] "xtabs"   

Yet, I’d like a function where name would be the name of a function and the return value is a character vector (or a list, if done recursively) of functions that are called within name.

Motivation

I actually need some sort of character based output (vector or list). The reason for this is that I’m working on a generic wrapper function for parallelizing an abitrary “inner function” where you don’t have to go through a time consuming trial-and-error process in order to find out which other functions the inner function depends on. So the output of the function I’m after will directly be used in snowfall::sfExport() and/or snowfall::sfSouce.

EDIT 2012-08-08

As there’s been some close-votes due to duplicity, I’ll check how answers can be merged with the other question tomorrow.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T13:00:59+00:00Added an answer on June 9, 2026 at 1:00 pm

    There must be better ways out there, but here’s my attempt:

    listFunctions <- function(function.name, recursive = FALSE, 
                              checked.functions = NULL){
    
        # Get the function's code:
        function.code <- deparse(get(function.name))
    
        # break code up into sections preceding left brackets:
        left.brackets <- c(unlist(strsplit(function.code, 
                                           split="[[:space:]]*\\(")))
    
        called.functions <- unique(c(unlist(sapply(left.brackets, 
                                                   function (x) {
    
            # Split up according to anything that can't be in a function name.
            # split = not alphanumeric, not '_', and not '.'
            words <- c(unlist(strsplit(x, split="[^[:alnum:]_.]")))
    
            last.word <- tail(words, 1)
            last.word.is.function <- tryCatch(is.function(get(last.word)),
                                          error=function(e) return(FALSE))
            return(last.word[last.word.is.function])
        }))))
    
        if (recursive){
    
            # checked.functions: We need to keep track of which functions 
            # we've checked to avoid infinite loops.
            functs.to.check <- called.functions[!(called.functions %in%
                                              checked.functions)]
    
            called.functions <- unique(c(called.functions,
                do.call(c, lapply(functs.to.check, function(x) {
                    listFunctions(x, recursive = T,
                                  checked.functions = c(checked.functions,          
                                                        called.functions))
                    }))))
        }
        return(called.functions)
    }
    

    And the results:

    > listFunctions("listFunctions", recursive = FALSE)
     [1] "function"      "deparse"       "get"           "c"            
     [5] "unlist"        "strsplit"      "unique"        "sapply"       
     [9] "tail"          "tryCatch"      "is.function"   "return"       
    [13] "if"            "do.call"       "lapply"        "listFunctions"
    
    > system.time(all.functions <- listFunctions("listFunctions", recursive = TRUE))
       user  system elapsed 
      92.31    0.08   93.49 
    
    > length(all.functions)
      [1] 518
    

    As you can see, the recursive version returns a lot of functions. The problem with this is it returns every function called in the process, which obviously adds up as you go. In any case, I hope you can use this (or modify it) to suit your needs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Generating a list of which files changed between hg versions hg diff
Possible Duplicate: Find out the instance id from within an ec2 machine I am
Possible Duplicate: format xml string I'm generating an XML page like so: header('Content-Type: text/html');
Possible Duplicate: generating random numbers from skewed normal distribution i expect a long array
Possible Duplicate: Generating an Excel file in ASP.NET Here's my question: How do you
Possible Duplicate: Algorithm for generating a random number is posible to generate a random
Possible Duplicate: PHP: Script for generating Crossword game? Does anyone know any PHP-based simple
Possible Duplicate: Developing for Android in Eclipse: R.java not generating what to do if
Possible Duplicate: generating GUID without hyphen I am creating a GUID with the following
Possible Duplicate: Generating Random Numbers in Objective-C How do I generate a random number

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.