This function can be used to apply a function to a vector
containing elements that lie outside the valid domain of
fn
. The function onlyif
differs from
ifelse
in the sense that it is not vectorized and
a closure can be used. For example, ifelse(length(x) < 10, pad(x, 10 - length(x)), x)
yields the wrong result due to the length of the first
argument. The onlyif
function is designed for
these situations.
onlyif(length(x) < 10, function(x) pad(x, 10 -
length(x)), x)
.
Note that a closure is only required if an expression
cannot be evaluated under both a TRUE or FALSE scenario.
The alternative would be to use a conditional block,
which can result in improperly scoped code if one is
careless.