Konrad
Konrad

Reputation: 18585

Count number of arguments passed to function

I'm interested in counting a number of arguments passed to a function. length can't be used for that purpose:

>> length(2,2,2,2,2)
Error in length(2, 2, 2, 2, 2) : 
  5 arguments passed to 'length' which requires 1

This is obvious as length takes 1 argument so:

length(c(2,2,2,2,2))

would produce the desired result - 5.

Solution

I want to call my function like that myFunction(arg1, arg2, arg3). This can be done with use of an ellipsis:

myCount <- function(...) {length(list(...))}

myCount would produce the desired result:

>> myCount(2,2,2,2,2)
[1] 5

Problem

This is awfully inefficient. I'm calling this function on substantial number of arguments and creating lists just to count number of objects is wasteful. What's the better way of returning the number of arguments passed to a function?

Upvotes: 4

Views: 4096

Answers (3)

Arafath Nihar
Arafath Nihar

Reputation: 306

nargs returns the number of arguments supplied to that function

myCount <- function(...) {
  nargs()
}

> myCount(2,2,2,2,2)
[1] 5

Reference https://stat.ethz.ch/R-manual/R-devel/library/base/html/nargs.html

Upvotes: 4

Xiurui Zhu
Xiurui Zhu

Reputation: 43

Here is a somewhat elegant way using length() with purrr::lift_*() familiy functions.

Generally you are passing multiple arguments to length(), which is not working because length() takes a vector or a list as input.

So what we need is to convert the input from a vector/list to ... (dots). purrr::lift_*() family provides a series of functions that do so.

One option can be converting from vector to dots:

> lift_vd(length)(2, 2, 2, 2, 2)
[1] 5

Another option can be converting from list to dots:

> lift_ld(length)(2, 2, 2, 2, 2)
[1] 5

Both options are working perfectly well, and what you need is using one of the purrr::lift_*() functions on length() before passing spliced arguments to it.

Upvotes: 1

MrFlick
MrFlick

Reputation: 206253

How about

myCount <- function(...) {length(match.call())-1}

This just inspects the passed call (and removes 1 for the function name itself)

Upvotes: 7

Related Questions