jwilley44
jwilley44

Reputation: 173

Give Function a Name in R

I am generating functions programatically by calling into a Java library, it looks something like this

f <- getFunction("javaFunctionName")

I can generate these just fine, but what I would like to know is if it is possible to give those functions names in the R environment.

functionNames <- c("func1", "func2", "func3")

lapply(functionNames, getFunction)

After performing the lapply I would be able to call the functions I made by those names:

func1(args)
func2(args)
func3(args)

I looked at this discussion and either I am missing something or it is not the same as what I am trying to do.

Hope I have been clear and any help is appreciated. Thank you for your time.

Upvotes: 3

Views: 610

Answers (1)

Miriam
Miriam

Reputation: 471

If I understand you correctly, doesnt the following work?

functionNames <- c("func1", "func2", "func3")

yourJavaFunc <- function() "myJavaFunc"

assignFunctions <- function(fcts){
  lapply(fcts, function(x) {
    assign(x, yourJavaFunc, envir = .GlobalEnv) # add your getJavaFunction locic here
  })
}

assignFunctions(functionNames)

Upvotes: 3

Related Questions