R wrapper function

Can someone help me understand what is a wrapper function in r? I would really appreciate it if you could explain this with examples of how to create one custom wrapper function and when to use it.

Thanks in advance.

+3


source to share


1 answer


Let's say I want to use mean()

, but I want to set some default arguments, and my usecase does not allow additional arguments to be added when I actually call mean()

.

I could create a wrapper function:

mean_noNA <- function(x) {
    return(mean(x, na.rm = T))
}

      



mean_noNA

is a wrapper for mean()

where we set it na.rm

to TRUE.

Now we can use mean_noNA(x)

the same as mean(x, na.rm = T)

.

+6


source







All Articles