Modular Programming in R

Completely new to R, I wonder if it is possible to compress my own hacked R-scripts by packing parts into sub-scripts, such as modules or global native functions. This is somewhat common in other translator languages.

What is the best way to separate reading data, plotting, designing, exporting, etc. in different R-modules?

+7
function module r package
source share
2 answers

Yes, you can make indexes containing functions, for example, and use source for their source.

I think the best way to do this depends on the case, but just write your functions, save them as .R files and source them in a script

+3
source share

Theres a package for this purpose called "modules" .

The package provides an import function that easily replaces source . For most purposes

 source('x.r') 

can simply be replaced by

 x = import('x') # or: import('x', attach = TRUE) 

However, import does a lot of things better than source - for example, you can arrange your modules hierarchically within a project:

 cmdline = import('sys/cmdline') args = cmdline$parse() # or: sys = import('sys') args = sys$cmdline$parse() 

See vignette for details .

The modules module was created precisely because I was unsatisfied with Rs support for modulation.

+5
source share

All Articles