Reputation: 5508
I have two files, for example "aa.R" and "bb.R", both have a function "foo()".
How can I call the two functions with the same name from my console?
For example in "aa.R"
hi <- function(){
print("hi, aa")
}
in "bb.R"
hi <- function(){
print("hi, bb")
}
I tried the following:
source("aa.R")
source("bb.R")
aa::hi()
But it doesn't work. How can I get the hi()
function from "aa.R"?
Upvotes: 0
Views: 937
Reputation: 132999
source
into separate environments:
aa <- new.env()
bb <- new.env()
source(textConnection('hi <- function(){
print("hi, aa")
}'), local = aa)
source(textConnection('hi <- function(){
print("hi, bb")
}'), local = bb)
aa$hi()
#[1] "hi, aa"
bb$hi()
#[1] "hi, bb"
However, I suggest that you seriously consider building two packages from your two files. Then you could use ::
.
Upvotes: 3
Reputation: 44708
You don't. No seriously, just don't. You're just asking for irreproducible errors and unecessary confusion.
Change one of the two names. Or, rename them when they are called so it doesn't matter if they are overwritten.
source("aa.R")
hi_aa <- hi
source("bb.r")
hi_bb <- hi
Upvotes: 2