2

I have a bunch of .R files which contain functions which call other functions. These functions are spread across multiple files, and my life is complicated by the fact that there are lots of leftover functions which are not required (i.e. not called by the main scripting file).

For example:

main_file.R:

source("sub_file1.R")
source("sub_file2.R")


#let's call a function which will call other functions...

main_function()

sub_file1.R:

main_function = function(){

sub_function1() # here is a function which can be found in sub_file2.R

}

sub_file2.R

sub_function1 = function(){
sub_function2() #oh no, do these nested functions ever end?

}

sub_function2 = function(){
"You've reached the end!"
}

sub_function3 = function(){
"This is a useless subfunction"
}

sub_function4 = function(){
"This is another useless subfunction"
}

My Question

How would you look at these files as a collective and return the minimum set of functions required? For example, in less complicated cases, it would be appropriate to get a summary file like

summary_file.R:

main_function()


main_function = function(){

sub_function1() # here is a function which can be found in sub_file2.R

}

sub_function1 = function(){
sub_function2() #oh no, do these nested functions ever end?

}

sub_function2 = function(){
"You've reached the end!"
}

Are there any good practices to stop this happening in the first place, without resorting to building a package? E.g. do people usually just have one main scripting file and one extra file which contains all the necessary functions? What if a function is really long? Does that necessitate its own .R file?

Alex
  • 15,186
  • 15
  • 73
  • 127
  • 2
    If you use the `devtools` package you can work with a package without having to build/install/load on every change. Its the right way to develop functions, its quick, easy, and you can build the code into a package archive file to give to people to share the code. Use that. – Spacedman Jul 31 '14 at 06:34
  • 2
    You can never know which functions will be *called* at runtime because it's always possible to `eval` a string with a function name or use `get()` to call a function. If you just want to see all the function calls explicitly written in a function, you may consider an approach like [the one here](http://stackoverflow.com/questions/24481868/regular-expression-to-find-function-calls-in-a-function-body/24482345#24482345) – MrFlick Jul 31 '14 at 07:07
  • Oh I like that answer you gave. All I need to do then is to modify it to give name of the `.R` as well! – Alex Jul 31 '14 at 07:12
  • 1
    How about this? http://stackoverflow.com/questions/8761857/identifying-dependencies-of-r-functions-and-scripts – Roman Luštrik Jul 31 '14 at 07:31
  • Thanks, that looks helpful, but complicated! – Alex Jul 31 '14 at 07:53
  • Seriously, packages. Keep the code in one place, don't pollute your global namespace, devtools makes it easy to edit/load/edit/load. Real easy. Don't try and kludge some function dependency aggregator, you'll be at it forever, and it won't work. – Spacedman Jul 31 '14 at 08:37

0 Answers0