[R] matching last argument in function

Charilaos Skiadas cskiadas at gmail.com
Tue Feb 12 23:09:24 CET 2008


On Feb 12, 2008, at 3:31 PM, Thomas Lumley wrote:

> On Tue, 12 Feb 2008, Alistair Gee wrote:
>
>> I often want to temporarily modify the options() options, e.g.
>>
>> a <- seq(10000001, 10000001 + 10) # some wide object
>>
>> with.options <- function(..., expr) {
>>  options0 <- options(...)
>>  tryCatch(expr, finally=options(options0))
>> }
>>
>> Then I can use:
>>
>> with.options(width=160, expr = print(a))
>>
>> But I'd like to avoid explicitly naming the expr argument, as in:
>>
>> with.options(width=160, print(a))
>>
>> How can I do this with R's argument matching?
>
> You can't.  You could provide a list, though:

I'd second this idea: Always have two arguments, the second one being  
the expression and the first one being the list of options. This  
would make it look a lot more like "with" also.

Haris Skiadas
Department of Mathematics and Computer Science
Hanover College



> with.options <- function(optionlist,expr){
>    option0<-options(optionlist)
>    on.exit(options(options0))
>    eval.parent(expr)
> }
>
> then
> with.options(width=160, print(a))
> with.options(list(width=160, warn=1), print(a))
>
>
>         -thomas
>
> Thomas Lumley			Assoc. Professor, Biostatistics
> tlumley at u.washington.edu	University of Washington, Seattle



More information about the R-help mailing list