[R] import function without overwriting function with the same name

Bert Gunter gunter.berton at gene.com
Sat Aug 3 23:32:11 CEST 2013


Martrin:

It sounds like you have some homework to do. Moreover, you have not
provided sufficient information (for me, anyway) to give a definitive
answer: why/how are the functions different? How are they used?

1) "Import" is not the same as "source."  "Import" is something one
does with packages. Packaging up your functions sensibly and using
Namespaces and import directives appropriately may be how you should
proceed (I would say "probably" is, except you have not provided
sufficient information). So you need to read up on R package creation
and Namespaces.

2. Alternatively, perhaps you should make your different functions
with the same name methods for the different classes to which they are
applicable using either the S3 or S4 class system. So you may need to
read up on these class systems and method invocation.Again, impossible
to say without more info.

3. Or maybe you need to combine your separate functions into one with
appropriate default and optional argument lists that alter the
execution, e.g. via having arguments that are functions that alter the
behavior. This is one of the glories of functional programming
(functions are first class objects).

4. Or, probably the least desirable approach that may nevertheless be
what you may want, is just to attach your collections of functions and
access them by fully qualified names. See ?"::" for details on how to
do that.

... or maybe something else that I haven't thought of or that would
fit your needs better. But without having those needs specified in
detail, ???

Cheers,
Bert

On Sat, Aug 3, 2013 at 1:27 PM, Martin Batholdy <batholdy at googlemail.com> wrote:
> Hi,
>
> I have to import multiple R-files.
> Each file consists of several functions with the same function name across the R-files.
>
> When I import all files one by one (with source()) I overwrite the function definition of the previous file until only the very last function definition lasts.
> However, I need them separately (although they have the name, they solve a certain task differently).
>
> Now I thought about creating a separate environment for each file / function-group, but I do not know upfront how many files I have to import and therefore how many different environments I need.
> But since the creation of an environment works like variable assignments (myEnv <- new.env()) I cannot just loop over the number of files and create x environments.
>
> What is the best way to solve this problem of importing functions without overwriting already existing functions with the same name?
>
>
> Thanks!
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm



More information about the R-help mailing list