Martin Batholdy
2013-Aug-03 20:27 UTC
[R] import function without overwriting function with the same name
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!
Ken Knoblauch
2013-Aug-03 20:42 UTC
[R] import function without overwriting function with the same name
Martin Batholdy <batholdy <at> googlemail.com> writes:> I have to import multiple R-files. > Each file consists of several functions with the samefunction 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 (althoughthey have the name, they solve a certain task differently).> Now I thought about creating a separate environmentfor each file / function-group, but I do not know> upfront how many files I have to import and thereforehow many different environments I need.> But since the creation of an environment workslike 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 importingfunctions 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
Jeff Newmiller
2013-Aug-03 21:26 UTC
[R] import function without overwriting function with the same name
This kind of thing gets dealt with as part of building packages. You can patch together your own namespace management (?attachNamespace) but you are better served to start developing your functions into packages once your projects get large enough that you start worrying about name collisions. --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. 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
2013-Aug-03 21:32 UTC
[R] import function without overwriting function with the same name
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
Michael Friendly
2013-Aug-04 20:05 UTC
[R] import function without overwriting function with the same name
On 03/08/2013 10:27 PM, Martin Batholdy wrote:> 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).Others have taken your request literally, but since you haven't told anyone what your functions do or listed one, people can only guess. Is it at all possible that your functions could be generalized to do the 'certain task', by including other arguments allowing one function to do that 'differently'? Very often in R, as in other things, if you find that you need to do something so bizarre as reading many functions with the same name, it's time to re-think a different approach that would end up not requiring such machinations. -Michael