3

I have been working for a few months on a project, and there are now about 120 functions. About one third of these functions are no longer necessary to run the final version the script. The functions are all loaded from a function folder into a separate namespace.

Is there a way in R to see which functions are called? The alternative is to go to the script and write down the functions that are now actually used.

Henk
  • 3,634
  • 5
  • 28
  • 54
  • I think Rprof would be a good start – baptiste Dec 12 '14 at 12:37
  • 1
    How about loading your script file as a text (character) object into `R` and running a loop over all function names, doing `grepl` on each one? – Carl Witthoft Dec 12 '14 at 12:43
  • 1
    See http://stackoverflow.com/questions/8761857/identifying-dependencies-of-r-functions-and-scripts for ideas on how to see relationships between functions. – Roman Luštrik Dec 12 '14 at 12:47
  • @baptiste - that sounds promising. When I run rprof ny functions are marked . – Henk Dec 12 '14 at 12:55
  • Using Carl's method, you could get all of the function names from the local environment using: `as.character(lsf.str(".GlobalEnv"))`. Then use grepl on each one – Stefan Avey Dec 12 '14 at 13:00
  • @av1 - all my functions are loaded into a namespace FUN. I will try Carl's method, but it involves some programming time [for me]. – Henk Dec 12 '14 at 13:05
  • @baptiste - it looks as if I get meaningful results [for my question] out of rprof only when i group all my functions into a R package, is that right? – Henk Dec 12 '14 at 13:10
  • @Henk I'm afraid I don't know, it was just a hint in passing. That being said, packages are very useful and trivial to make. – baptiste Dec 12 '14 at 13:17
  • Is this one script with all functions? Please provide a minimal reproducible example so we know what we're dealing with. – Tyler Rinker Dec 12 '14 at 16:13
  • @Tyler Rinker - it is a collection of 5 scripts. There is, as explained in the question, a folder with the functions that is loaded in a separate environment. Making a reproducable example is in particular case a bigger operation than weeding out the redundant functions by hand. – Henk Dec 12 '14 at 17:20
  • @Henk I said **minimal** reproducible. You could do this with 5 dummy functions in less than 3 minutes. It you wanted to make it more realistic you could use `readLines` + `cat` to output to separate files. `dir.create` would make the folder. In 6 minutes you could have this as a reproducible problem that would benefit the community. Right now you're getting hints as comments because folks don't wanna take the time to reproduce your problem. Keep in mind R can run code not explanations of code. Try the MWE and I bet a cup of coffee that within an hour you get an answer. – Tyler Rinker Dec 12 '14 at 17:34

0 Answers0