Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Function Defaults R on a Project Specific Basis

Commonly, I use the same function settings. I'm wondering if there is a method, other than having a new object in the path that is essentially a wrapper for the function, to set default arguments. For example:

paste() has it's sep argument set to a space =" ", I'm tired of writing ,sep="" over and over. So is there a way to "temporarily" replace the function with my chosen defaults?

paste(...,sep="") 

Can I accomplish this through packaging? I've sometimes noticed that, some packages force other equally named functions to be masked in the global environment.

Ideally, I'd like something that can be set on a project by project basis in (load.r or some other such workflow startpoint)

like image 596
Brandon Bertelsen Avatar asked Jan 31 '12 03:01

Brandon Bertelsen


People also ask

How do you set a default argument in R?

Adding a Default Value in R You can specify default values for any disagreements in the argument list by adding the = sign and default value after the respective argument. You can specify a default value for argument mult to avoid specifying mult=100 every time.

When using a function the functions arguments can be specified by?

Because all function arguments have names, they can be specified using their name. Specifying an argument by its name is sometimes useful if a function has many arguments and it may not always be clear which argument is being specified. Here, our function only has one argument so there's no confusion.


2 Answers

I'd personally be very hesitant to change the default behavior of any commonly used functions --- especially base R functions. For one thing, it will immediately decrease the portability of any scripts or code snippets in which you use the redefined functions. Worse, other R users reading your scripts will likely be either: (a) unaware of your private meanings for well-known language elements or (b) frustrated at having to rewire their own expectations for the functions. For me, it would also feel like an added mental burden to attach different meanings to the same symbol in different settings.

I think a much better solution is to create similarly named functions implementing your preferred defaults. A slightly modified name will effectively flag that this isn't the familiar base function, without burdening you with much or any extra typing. A good example are the paste0() and cat0() functions that are included in the gsubfn package. (Clearly you and I aren't the only two to find ourselves (frequently) annoyed by the paste()'s default sep setting!):

library(gsubfn)

paste0
# function (..., sep = "") 
# paste(..., sep = sep)
# <environment: namespace:gsubfn>

cat0
# function (..., sep = "") 
# cat(..., sep = sep)
# <environment: namespace:gsubfn>

You can then either collect a number of these functions in a text file, sourcing them early in your script, or (better) package them up and load them via a call to library().

like image 50
Josh O'Brien Avatar answered Oct 16 '22 07:10

Josh O'Brien


The Defaults package used to do that; retired in 2014.

like image 44
Vincent Zoonekynd Avatar answered Oct 16 '22 06:10

Vincent Zoonekynd