[R] import function without overwriting function with the same name
Ken Knoblauch
ken.knoblauch at inserm.fr
Sat Aug 3 22:42:33 CEST 2013
Martin Batholdy <batholdy <at> googlemail.com> writes:
> 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?
There is not really enough of an example to suggest a
definite solution, but could you read the files with
lapply and return the environment with the functions?
That way you would have a list of environments that
you could access as list elements. I haven't done this
before but
env.lst <- lapply(1:5, new.env)
seems to work just fine
env.lst
[[1]]
<environment: 0x108928b20>
[[2]]
<environment: 0x1089295f8>
[[3]]
<environment: 0x10892a908>
[[4]]
<environment: 0x10892a438>
[[5]]
<environment: 0x10892aea0>
> Thanks!
>
--
Kenneth Knoblauch
Inserm U846
Stem-cell and Brain Research Institute
Department of Integrative Neurosciences
18 avenue du Doyen Lépine
69500 Bron
France
tel: +33 (0)4 72 91 34 77
fax: +33 (0)4 72 91 34 61
portable: +33 (0)6 84 10 64 10
http://www.sbri.fr/members/kenneth-knoblauch.html
More information about the R-help
mailing list