Remi.b
Remi.b

Reputation: 18239

Why can't we loop over `...`?

Why does the following not work?

f = function(...) for (i in ...) print(i)
f(1:3)
# Error in f(1:3) : '...' used in an incorrect context

while this work fine

f = function(...) for (i in 1:length(...)) print(...[i])
f(1:3)
# [1] 1
# [1] 2
# [1] 3

Upvotes: 9

Views: 121

Answers (1)

pcantalupo
pcantalupo

Reputation: 2226

It does not work because the ... object type is not accessible in interpreted code. You need to capture the object as a list as nongkrong showed:

for(i in list(...))

Take a look at the R manual here

Upvotes: 8

Related Questions