How to use R's ellipsis feature when writing your own function?

前端 未结 5 534
星月不相逢
星月不相逢 2020-11-22 03:43

The R language has a nifty feature for defining functions that can take a variable number of arguments. For example, the function data.frame takes any number of

5条回答
  •  Happy的楠姐
    2020-11-22 04:22

    This works as expected. The following is an interactive session:

    > talk <- function(func, msg, ...){
    +     func(msg, ...);
    + }
    > talk(cat, c("this", "is", "a","message."), sep=":")
    this:is:a:message.
    > 
    

    Same, except with a default argument:

    > talk <- function(func, msg=c("Hello","World!"), ...){
    +     func(msg, ...);
    + }
    > talk(cat,sep=":")
    Hello:World!
    > talk(cat,sep=",", fill=1)
    Hello,
    World!
    >
    

    As you can see, you can use this to pass 'extra' arguments to a function within your function if the defaults are not what you want in a particular case.

提交回复
热议问题