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
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]]))
}
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
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
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
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
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(..
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
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
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
10 matches
Mail list logo