Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 779
Remi.b Avatar asked Sep 06 '15 18:09

Remi.b


1 Answers

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

like image 93
pcantalupo Avatar answered Oct 05 '22 23:10

pcantalupo