Sergio
Sergio

Reputation: 763

return r function with numbers in expression

I want ot create a functions to reutn another functions based on parameters. As you can see in the bode below, when a method='local', it return a number which is correct, but when is selected method='emep' & n=0, it should return function(V) {(281* V ^-0.63)*det[1,1]} but it's returning function(V) {(281* V ^-0.63)*det[i,j]}. How can i fix this?

    f <- function(i,j, method,n, V,...){
      fen <- matrix(data=runif(50*25),nrow=50,ncol=25)
      det <- matrix(data=runif(50*25),nrow=50,ncol=25)
      ifelse(method=='local',
             return(fen[i,j]*det[i,j]),
             ifelse((method=='emep' & n == 0),
             return(function(V) {(281* V ^-0.63)*det[i,j]}),'na'))
    }


f(1,1,'local')
f(1,1,'emep',0)


function(V) {(281* V ^-0.63)*det[i,j]}
<environment: 0x609af48>

Many thanks!

Upvotes: 1

Views: 315

Answers (1)

lmo
lmo

Reputation: 38500

I'm not sure this is possible. R will print out the formula definition or evaluate the formula, but I don't think it will print out the formula with given arguments, but unevaluated. You could cheat and print out a character string using paste, if that suffices:

return(paste0("function(V) {(281* V ^-0.63)*det["i",",","j,"]}"),'na'))

Upvotes: 1

Related Questions