Re: [R] matching last argument in function

2008-02-13 Thread Gabor Grothendieck
Add envir = parent.frame() to the do.call: with.options <- function(...) { L <- as.list(match.call())[-1] len <- length(L) old.options <- do.call(options, L[-len], envir = parent.frame()) on.exit(options(old.options)) invisible(eval.parent(L[[len]])) } # test with.options(wid

Re: [R] matching last argument in function

2008-02-13 Thread Alistair Gee
Hi Gabor, That almost works ... but it fails when I nest with.options() within another function: with.options <- function(...) { L <- as.list(match.call())[-1] len <- length(L) old.options <- do.call(options, L[-len]) on.exit(options(old.options)) invisible(eval.parent(L[[len]])) }

Re: [R] matching last argument in function

2008-02-12 Thread Charilaos Skiadas
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(1001, 1001 + 10) # some wide object >> >> with.options <- function(..., expr) { >> options0 <- options(...) >> t

Re: [R] matching last argument in function

2008-02-12 Thread Gabor Grothendieck
Try this: with.options <- function(...) { L <- as.list(match.call())[-1] len <- length(L) old.options <- do.call(options, L[-len]) on.exit(options(old.options)) invisible(eval.parent(L[[len]])) } > with.options(width = 40, print(1:25)) [1] 1 2 3 4 5 6 7 8 9 10 11 12

Re: [R] matching last argument in function

2008-02-12 Thread Thomas Lumley
On Tue, 12 Feb 2008, Alistair Gee wrote: > I often want to temporarily modify the options() options, e.g. > > a <- seq(1001, 1001 + 10) # some wide object > > with.options <- function(..., expr) { > options0 <- options(...) > tryCatch(expr, finally=options(options0)) > } > > Then I can u

Re: [R] matching last argument in function

2008-02-12 Thread Alistair Gee
I couldn't get that to work, b/c I need the expr block to be evaluated after the call to options(). I suspect that list(...) evaluates its arguments. Here's what I did to your example: test2 <- function(...) { dots <- list(...)# <=== I think expr is evaluated here. if(sum(dots.missin

Re: [R] matching last argument in function

2008-02-12 Thread Erik Iverson
Yes that will work, that's exactly what I was getting at in my second paragraph. I wrote a function that uses this idea, except the (single) unnamed argument can occur anywhere in the function (not necessarily last). It will stop if there is more than one unnamed argument. test2 <- function(..

Re: [R] matching last argument in function

2008-02-12 Thread Gabor Csardi
It should be possible i think. You just supply all the arguments via '...' and then cut off the last one. I don't see why this wouldn't work, but maybe i'm missing something. Gabor On Tue, Feb 12, 2008 at 12:58:25PM -0600, Erik Iverson wrote: > Alistair - > > I don't believe this is possible. T

Re: [R] matching last argument in function

2008-02-12 Thread Erik Iverson
Alistair - I don't believe this is possible. The only way formal arguments (like expr) can be matched after a '...' is with *exact* name matching. Why do you want to avoid explicitly naming the expr argument? If you always want the expr argument last, you might be able to just use ... as the

[R] matching last argument in function

2008-02-12 Thread Alistair Gee
I often want to temporarily modify the options() options, e.g. a <- seq(1001, 1001 + 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 li