Re: [R] setting options only inside functions

From: Jannis <bt_jannis_at_yahoo.de>
Date: Wed, 27 Apr 2011 19:16:08 +0100 (BST)


Thanks to all who supplied suggestions. All of them worked. The best solution, however, was to wrap the stuff inside dummy() after the options(...) into a try() command. That way it also worked with the following setup:

dummy=function()
{
  old.options=options(error=quote{dummy1()})

  try(....,silent=TRUE)

  options(old.options)
}
options(error=quote{dummy()})

The suggestion of Uwe did not work with these nested error handling functions. I, however, did not state my problem precisely enough for this.

Thanks again

Jannis

> Von: Jonathan Daily <biomathjdaily_at_gmail.com>
> Betreff: Re: [R] setting options only inside functions
> An: "Jannis" <bt_jannis@yahoo.de>
> CC: r-help_at_r-project.org
> Datum: Mittwoch, 27. April, 2011 13:35 Uhr
> There is probably a more elegant way
> to do this, but you could write
> it into dummy1():
>
> dummy1 <- function()
> {
> ...original function
> options(old.options)
> }
>
> Alternatively, you could use ?tryCatch with the finally
> argument as a
> call to options.
>
> HTH,
> Jon
>
> On Wed, Apr 27, 2011 at 9:16 AM, Jannis <bt_jannis_at_yahoo.de>
> wrote:
> > Dear list members,
> >
> >
> > is it possible to set some options only inside a
> function so that the original options are restored once the
> function is finished or aborted due to an error?  Until now
> I do something like:
> >
> >
> > dummy=function()
> > {
> >  old.options=options(error=dummy1())
> >
> >  ....
> >
> >  options(old.options)
> > }
> >
> >
> > This works for most cases but when the function
> terminates because of an error and its last command is not
> run, error=dummy1() still remains as an option. Is there any
> way around this?
> >
> >
> > Cheers
> > Jannis
> >
> >
> > ______________________________________________
> > 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.
> >
>
>
>
> --
> ===============================================
> 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 Wed 27 Apr 2011 - 18:26:17 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 Wed 27 Apr 2011 - 19:10:33 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