Re: [R] setting options only inside functions

From: Jonathan Daily <biomathjdaily_at_gmail.com>
Date: Fri, 29 Apr 2011 13:17:19 -0400

In python, opening a connection using with allows for a temporary assignment using "as". So:

with file("/path/to/file") as con:

    permanent_object = function(con)

would provide the return of function(con) globally, but close con. If function(con) causes an error, con is still closed.

I agree with your description of what the function would need to do. Would it make sense to make it generic and define default methods for different setups? e.g. Using the current with/within when it is a data.frame/environment, evaluating it when it is a function, etc.

On Fri, Apr 29, 2011 at 12:34 PM, <luke-tierney_at_uiowa.edu> wrote:
> 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.
>
>
> 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

-- 
===============================================
Jon Daily
Technician
===============================================
#!/usr/bin/env outside
# It's great, trust me.

______________________________________________
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.
Received on Fri 29 Apr 2011 - 17:24:55 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 29 Apr 2011 - 17:50:34 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive