[R] setting options only inside functions

luke-tierney at uiowa.edu luke-tierney at uiowa.edu
Tue May 3 00:34:30 CEST 2011


On Fri, 29 Apr 2011, William Dunlap wrote:

>> -----Original Message-----
>> From: r-help-bounces at r-project.org
>> [mailto:r-help-bounces at r-project.org] On Behalf Of
>> luke-tierney at uiowa.edu
>> Sent: Friday, April 29, 2011 9:35 AM
>> To: Jonathan Daily
>> Cc: r-help at r-project.org; Hadley Wickham; Barry Rowlingson
>> Subject: Re: [R] setting options only inside functions
>>
>> The Python solution does not extend, at least not cleanly, to things
>> like dev on/ dev off or to Hadley's locale example.  In any case if I
>> am reading the Python source correctly on how they handle user
>> interrupts this solution has the same non-robusness to user interrupts
>> issue that Bill's initial solution had.
>>
>> As a basis I believe what we need is a mechanism that handles a
>> setup, an action, and a cleanup, with setup and cleanup occurring with
>> interrupts disablednand the action with interrupts enabled. Scheme's
>> dynamic wind is similar, though I don't believe the scheme standard
>> addresses interrupts and we don't need to worry about continuations,
>> but some of the issues are similar.  Probably we would want two
>> flavors, one in which the action has to be a function that takes as a
>> single argument the result produced by the setup code, and one in
>> which the action can be an argument expression that is then evaluated
>> at the appropriate place by laze evaluation.
>>
>> This can be done at the R level except for the controlling of
>> interrupts (and possibly other asynchronous stuff)-- that would need a
>> new pair of primitives (suspendInterrupts/enableInterupts or something
>> like that).  There is something in the Haskell literature on this that
>> I have looked at a while back -- probably time to have another look.
>
> Luke,
>
>  A similar problem is that if optionsList contains an illegal
> option then setting options(optionList) will commit changes
> to .Options as it works it way down the optionList until it
> hits the illegal option, when it throws an error.  Then the
> following on.exit is never called (it wouldn't have the output
> of options(optionList) to work on if it were called) and the
> initial settings in optionList stick around forever.  E.g.,
>
>  > withOptions <- function(optionList, expr) {
>  +     oldOpt <- options(optionList)
>  +     on.exit(options(oldOpt))
>  +     expr
>  + }
>  > getOption("height")
>  NULL
>  > getOption("width")
>  [1] 80
>  > withOptions(list(height=10, width=-2), 666)
>  Error in options(optionList) :
>    invalid 'width' parameter, allowed 10...10000
>  > getOption("height")
>  [1] 10
>  > getOption("width")
>  [1] 80
>
> I haven't checked to see if par() works in the same way - it
> does in S+.
>
> An ignoreInterrupts(expr) function would not help in that case.

It would be solving an orthogonal problem.

> Making options() (and par()) atomic operations would help, but that
> may be a lot of work.

But it would be the right thing to do for this purpose, either by
creating an atomic version just for use in this context or by having a
withOptions construct recursively work thougheach option.

>  options() might also warn but no change
> .Options if there were an attempt to set an illegal option.

Seems more or less the same as making options() atomic.

Best,

luke

>
> Bill Dunlap
> Spotfire, TIBCO Software
> wdunlap tibco.com
>
>>
>>
>> On Thu, 28 Apr 2011, Jonathan Daily wrote:
>>
>>> I would also love to see this implemented in R, as my
>> current solution
>>> to the issue of doing tons of open/close, dev/dev.off, etc.
>> is to use
>>> snippets in my IDE, and in the end I feel like it is a hack job. A
>>> pythonic "with" function would also solve most of the
>> situations where
>>> I have had to use awkward try or tryCatch calls. I would be
>> willing to
>>> help with this project, even if it is just testing.
>>>
>>> On Wed, Apr 27, 2011 at 5:43 PM, Barry Rowlingson
>>> <b.rowlingson at lancaster.ac.uk> wrote:
>>>>> but it's a little clumsy, because
>>>>>
>>>>> with_connection(file("myfile.txt"), {do stuff...})
>>>>>
>>>>> isn't very useful because you have no way to reference
>> the connection
>>>>> that you're using. Ruby's blocks have arguments which
>> would require
>>>>> big changes to R's syntax.  One option would to use pronouns:
>>>>
>>>>  Looking very much like python 'with' statements:
>>>>
>>>> http://effbot.org/zone/python-with-statement.htm
>>>>
>>>>  Implemented via the 'with' statement which can operate on anything
>>>> that has a __enter__ and an __exit__ method. Very neat.
>>>>
>>>> Barry
>>>>
>>>> ______________________________________________
>>>> R-help at r-project.org mailing list
>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>>>> and provide commented, minimal, self-contained, reproducible code.
>>>>
>>>
>>>
>>>
>>>
>>
>> --
>> Luke Tierney
>> Statistics and Actuarial Science
>> Ralph E. Wareham Professor of Mathematical Sciences
>> University of Iowa                  Phone:             319-335-3386
>> Department of Statistics and        Fax:               319-335-3017
>>     Actuarial Science
>> 241 Schaeffer Hall                  email:      luke at stat.uiowa.edu
>> Iowa City, IA 52242                 WWW:  http://www.stat.uiowa.edu
>>
>

-- 
Luke Tierney
Statistics and Actuarial Science
Ralph E. Wareham Professor of Mathematical Sciences
University of Iowa                  Phone:             319-335-3386
Department of Statistics and        Fax:               319-335-3017
    Actuarial Science
241 Schaeffer Hall                  email:      luke at stat.uiowa.edu
Iowa City, IA 52242                 WWW:  http://www.stat.uiowa.edu


More information about the R-help mailing list