5

I'm looking for a better way to organize my R code. Ideally I'm hoping to

  • Put all auxiliary functions at the end of the script. It will help me to focus on the main part of the code without being distracted by lots of helper functions at the beginning of the script.
  • Allow each variable to only exist in certain scope. e.g If I accidentally assign values to certain variables, I don't want these variables to be picked up by functions defined later than them and make a mess.

In Python, the two goals can be achieved easily by:

def main():
...

def helper_func(x,y):
...

if __name__ == '__main__':
    main()

Is it possible in R? Any advice on making it similar to this if not possible?

BlueFeet
  • 2,407
  • 4
  • 21
  • 24
  • 2
    There's no `main`. Perhaps you should put your functions in a library. – Matthew Lundberg Aug 27 '15 at 18:20
  • For the record, it's certainly possible to define custom functions (i.e. something like `helpers <- function (x,y) {...}`). – maj Aug 27 '15 at 18:20
  • 1
    R doesn't work like that if you are trying to just have a script. You would want to build a package, load the package and then have your 'main' code. – cdeterman Aug 27 '15 at 18:29
  • @cdeterman You don't have to build a package - for most cases sourcing a .R file will do just fine. – Señor O Aug 27 '15 at 18:42
  • @SeñorO you are correct that you don't have to but it is often cleaner and more extensible if/when you want to add more features instead of having a large source.R or multiple source.R files. That is my main point. – cdeterman Aug 27 '15 at 18:43
  • Also possible dup of : http://stackoverflow.com/questions/2968220/is-there-an-r-equivalent-of-the-pythonic-if-name-main-main – hrbrmstr Aug 27 '15 at 18:48

1 Answers1

1

To your two points:

1) Since scripts are run top to bottom in a command-line fashion, anything you put at the bottom of a script will not be available to lines run above it. You can put auxillary functions in a different file and source it at the top of your "main" file.

2) Anything done in a function will be forgotten by the end:

> a = 2
> f = function(x) x <- x + 2
> b = f(a)
> b
[1] 4
> a
[1] 2

Alternatively, you can specify the environment you want to use anywhere:

> CustomEnv = new.env()
> assign("a", 2, envir = CustomEnv)
> a = 3
> a
[1] 3
> get("a", CustomEnv)
[1] 2

See ?environment for more details

Señor O
  • 17,049
  • 2
  • 45
  • 47