recycle
is called within a function and ensures the parameters in the
calling function are all the same length by repeating them using rep
. This
function alters the environment from which it is called. It is stricter than
R recycling in that it will not repeat vectors other than length one to match
the longer ones, and it throws more informative errors.
recycle(..., .min = 1, .env = rlang::caller_env())
the length of the longest variable
the variables to recycle
the minimum length of the results (defaults to 1)
the environment to recycle within.
NULL values are not recycled, missing values are ignored.
testfn = function(a, b, c) {
n = recycle(a,b,c)
print(a)
print(b)
print(c)
print(n)
}
testfn(a=c(1,2,3), b="needs recycling", c=NULL)
try(testfn(a=c(1,2,3), c=NULL))
testfn(a=character(), b=integer(), c=NULL)
# inconsistent to have a zero length and a non zero length
try(testfn(a=c("a","b"), b=integer(), c=NULL))
Run the code above in your browser using DataLab