Friday, September 27, 2013

Change function defaults in R

For many functions in R, I use the same non-default arguments over and over again. I wanted to change the default behavior of the functions at first, using the setDefaults::defaults package. However, as pointed out by Josh O'Brien in this topic, this "will immediately decrease the portability of any scripts or code snippets in which you use the redefined functions."  Using the Curry::functional function to create similarly named function seems like a better option:


x <- c(1,NA,3,NA)
table(x)  #  NA's excluded by default...
paste("text",x)  #  space added by default...

library(functional)
table0 <- Curry(table, useNA="ifany")
paste0 <- Curry(paste, sep="")

table0(x)  #  NA's included by default!
paste0("text",x)  #  no space added by default!


# getting right sided z-values by default:
qnorm0 <- Curry(qnorm, lower.tail=F)



See also: All things random: Favorite custom functions in R

No comments:

Post a Comment