r - Assigning an argument to null verses nothing in the function -


to understanding, if have functions 1 below:

hello1<- function(pillow){        if (missing(pillow)){       stop("you need enter pillow")     } pillow } 

hello1() stops , returns message want:

hello2<- function(pillow){        if (is.null(pillow)){       stop("you need enter pillow")     } pillow } 

hello2() stops , returns following message: error in hello2() : argument "pillow" missing, no default

hello3<- function(pillow=null){        if (is.null(pillow)){       stop("you need enter pillow")     } pillow } 

hello3() stops , returns message want:

hello4<- function(pillow=null){            if (missing(pillow)){           stop("you need enter pillow")         }     pillow     } 

hello4() stops , returns message want:

to summarize, can message want using missing() , is.null() when argument set null , when argument set nothing, can message want missing() not is.null(). being said, want know pros , cons associated assigning null in argument verses not assigning null.

the big advantage of using missing explicit in testing (is argument given or not?). default value null less explicit since null can correct value functions.

a particular case argument computed function , in corner cases computes value null.


Comments

Popular posts from this blog

user interface - how to replace an ongoing process of image capture from another process call over the same ImageLabel in python's GUI TKinter -

javascript - Restarting Supervisor and effect on FlaskSocketIO -

php - Mongodb connectivity error -