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
See ?on.exit Jeremy On Wednesday, April 27, 2011 9:16:13 AM UTC-4, Jannis 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-h... 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. > >
On 27.04.2011 15:16, Jannis 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? >See ?on.exit: dummy <- function() { old.options <- options(error=dummy1()) on.exit(options(old.options)) .... } Uwe Ligges> > 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.
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.