简体   繁体   中英

assign a function to a variable in R

The question is in the title. I'd like to do something like this :

myfunc<- pexp
plot(function(x) myfunc(x, 0.5))

I'd like to call several functions given as parameters in my script. I'd like to use a foreach instead of a bunch of if-then-else statement :

Suppose I call my script this way :

R --slave -f script.R --args plnorm pnorm 

I'd like to do something like this :

#only get parameters after --args
args<-commandArgs(trailingOnly=TRUE)
for i in args {
    plot(function(x) i(x,param1,param2))
}

Use get to retrieve an object given a character string containing its name. In this case, you can also use getFunction , the specific version to retrieve functions.

for f in args {
     f <- getFunction(f)
     plot(function(x) f(x, param1, param2))
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM