R function with variable args depending on presence/absence of other args -
i've stumbled upon varargs issue in r 2 or 3 times, seems problem have little bit trickier expected. here is
i have function, variables, introduce variable, kind of flag, selects way function working , parameters needed function itself: namely number , type of inputs depends on (flag) input.
ok, example better:
example = function(x,flag=1,y){ if (flag) return(x) else return(y) }
and working fine. point in example need specify both x , y every time. instead function taking only x if flag=1 , only y if flag=0. (in stupid example 2 distinct functions, in actual case have other (common) arguments on calculations both 'parts' of functions need).
i know 1 may specify whatever value unused argument , result wouldn't change, want function readable user, , cumbersome need specify argument won't used function
thank help
what following.
example = function(x,flag=1,y){ if (flag && !missing(x)) return(x) else if(!flag && !missing(y)) return(y) }
this check if flag 0 or non-zero plus check if argument missing. may want handle case when neither of these true cause function return null in case.
Comments
Post a Comment