[R] default arguments and '...' in a function

Bert Gunter gunter.berton at gene.com
Thu Dec 2 05:12:40 CET 2010


Look at the code for plot.default to see how it handles this issue
usling locally defined versions of functions with (possibly) locally
set parameter values.


Cheers,
Bert

On Wed, Dec 1, 2010 at 6:39 PM, Gabor Grothendieck
<ggrothendieck at gmail.com> wrote:
> On Wed, Dec 1, 2010 at 2:34 PM,  <Antonio.Gasparrini at lshtm.ac.uk> wrote:
>> Dear R-users,
>>
>> I'm trying to work out a way to set default values for arguments in a function which includes the optional argument '...'.
>> In practice, I have a 'plot' method for a function which specifies different types of plots. Every different plot should have different default arguments (for example for 'col', 'ylim' etc), plus the argument '...' to leave further optional choices to the user.
>> My problem is that I need to specify a large number of arguments in the usage (losing clarity), or alternatively leaving to the user the specification of an high number of argument (adding complexity).
>>
>> An example.
>> Suppose I want to create a function f with arguments 'a', and '...'.
>> Two more arguments 'b' and 'c' should be both set to 1 by default if the user don't include them in '...'.
>>
>> f <- function(a,...) {
>>  # set b <- 1 if b is not included in '...'
>>  # set c <- 1 if c is not included in '...'
>>  somefunction(a,b,c,...)
>> }
>>

Bert Gunter
Genentech



More information about the R-help mailing list