简体   繁体   中英

R function and pass optional arguments

I create a R function like:

myfun <- function(x, function) function(x)

which obviously works now is for example

myfun(rnorm(10), round)
myfun(rnorm(10), sample)

Is it possible to pass over the argument digits from the function round without specifying it in myfun ?
Or pass over the argument replace from the function sample ?

Something like

myfun(rnorm(10), round(digits=2))
myfun(rnorm(10), sample(replacement=TRUE))

I know it looks strange. I want to write a function where i can choose the distribution in the body.

myfun <- function(n, function) function(n)

now myfun(100,rnorm) works of course, but can i use it for myfun(100,rt) and define the degrees of freedom with argument df or myfun(100,rbinom) and define the size and prob ?

I think you are looking for ... .

myfun <- function(x,function2, ...){function2(x,...)}
myfun(rnorm(10),round,digits=2)
 [1] -1.70  1.34  1.27 -0.42 -1.76 -0.40  0.59  1.10  0.41 -0.18

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