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

前端 未结 5 462
星月不相逢
星月不相逢 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条回答
  •  既然无缘
    2020-11-22 04:14

    Just to add to Shane and Dirk's responses: it is interesting to compare

    get_list_from_ellipsis1 <- function(...)
    {
      list(...)
    }
    get_list_from_ellipsis1(a = 1:10, b = 2:20) # returns a list of integer vectors
    
    $a
     [1]  1  2  3  4  5  6  7  8  9 10
    
    $b
     [1]  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20
    

    with

    get_list_from_ellipsis2 <- function(...)
    {
      as.list(substitute(list(...)))[-1L]
    }
    get_list_from_ellipsis2(a = 1:10, b = 2:20) # returns a list of calls
    
    $a
    1:10
    
    $b
    2:20
    

    As it stands, either version appears suitable for your purposes in my_ellipsis_function, though the first is clearly simpler.

提交回复
热议问题