New to R. I'm trying to conditionally duplicate a function inside another function. I've tried using rlang::duplicate and data.table::copy.
library(rlang)
func1 <- function() {
print("horg1")
}
func2 <- function() {
print("horg2")
}
cond.rename <- function(horg) {
if (horg=="1") {
func<-rlang::duplicate(func1)
}
if (horg=="2") {
func<-rlang::duplicate(func2)
}
func
}
cond.rename("1")
This does not work. No function called "func" is created. However, if I run func<-duplicate(func1)
outside of a function it will create the function called "func". What am I doing wrong? If it's not obvious, this is drastically simplified from my actual code so if the purpose of the whole thing isn't clear that is why.