[R] run Rscript and ignore errors?

Nick Matzke matzke at nimbios.org
Fri Apr 24 18:47:58 CEST 2015


Hi,

Thanks so much for the hints, I think I've cracked it!  The key is to
create a dummy function, "continue_on_error" which gets run instead of
"stop" when an error occurs, then reference it
with options(error=continue_on_error).  Here's an example:


==========================
continue_on_error <- function()
{
print("NOTE: THERE WAS AN ERROR HERE. We are continuing because we have set
'options(error=continue_on_error())'")
}

testfunc <- function(a,b)
{
print(a+b)
}

# This is the key option
options(error=continue_on_error)

print(1)
print(2)

# This should work
testfunc(a=10, b=10)

print(3)

# This should produce an error and stop the Rscript run normally
testfunc(a=1)

print(4)
print(5)
==========================


Running this via Rscript completes:

Rscript continue_on_error.R
================
[1] 1
[1] 2
[1] 20
[1] 3
Error in print(a + b) : argument "b" is missing, with no default
Calls: testfunc -> print
[1] "NOTE: THERE WAS AN ERROR HERE. We are continuing because we have set
'options(error=continue_on_error())'"
[1] 4
[1] 5
================

If you comment out the options() line you get:

Rscript continue_on_error.R
================
[1] 1
[1] 2
[1] 20
[1] 3
Error in print(a + b) : argument "b" is missing, with no default
Calls: testfunc -> print
Execution halted
================

...which was what was annoying me before.

So, life hack FTW.  (Of course, yes, it would be better to write code good
n stuff, but for quick and dirty things this is handy.)

Cheers!
Nick



On Fri, Apr 24, 2015 at 10:06 AM, Jue Lin-Ye <jl.iccp at gmail.com> wrote:

> Jeff Newmiller <jdnewmil <at> dcn.davis.ca.us> writes:
>
> >
> > This seems like a recipe for garbage results to me, but there may be I
> something you can set the error option
> > to. See ?options.
> >
> ---------------------------------------------------------------------------
> > Jeff Newmiller                        The     .....       .....  Go
> Live...
> > DCN:<jdnewmil <at> dcn.davis.ca.us>        Basics: ##.#.       ##.#.
> Live Go...
> >                                       Live:   OO#.. Dead: OO#..  Playing
> > Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
> > /Software/Embedded Controllers)               .OO#.       .OO#.
> rocks...1k
> >
> ---------------------------------------------------------------------------
> > Sent from my phone. Please excuse my brevity.
> >
> > On April 23, 2015 9:51:31 AM PDT, Nick Matzke <nickmatzke.ncse <at>
> gmail.com> wrote:
> > >Hi R-help,
> > >
> > >I've looked at google, the Rscript documentation and the Rscript --help
> > >output and haven't found much on this.  So, here's my question:
> > >
> > >I have a rather long script that runs on various input datasets.  It is
> > >quite convenient to run the script from the Terminal command line with
> > >"Rscript scriptname.R"
> > >
> > >However, some datasets will cause errors. These are non-essential
> > >errors --
> > >just some datasets don't have certain columns so certain parts of the
> > >overall analysis don't produce figures etc.  Yes, I could go through
> > >the
> > >whole script and insert try() statements, etc.  But I'm lazy.
> > >
> > >So, is there a way to run Rscript or something similar, and just have
> > >it
> > >ignore all errors (i.e., keep running through the script)?  I.e., just
> > >like
> > >what happens if you just copy-paste the whole script into the R window
> > >--
> > >errors happen and are noted but the rest of the script keeps running.
> > >
> > >Thanks very much for any help!!
> > >
> > >Cheers!
> > >Nick
> > >
>
> This is a really, really simple "life-hack" of mine after failed past
> attemps to solve this
> ​ issue​
> :
> Option
> ​1​
> : Write an if(foreseen error){empty} so if _that_ happens, the code just
> ignores it.
> ​​Option 2: Try to run same code for the different initial values in
> different windows, so if one stops, another one keeps running.​
> I am interested in finding a real solution
> ​ (like the one proposed above by Mr. Newmiller)​
> , so I am looking forward to some more expert's say.
>
> Best regards.
>
> Jue
>
> > >    [[alternative HTML version deleted]]
> > >
> > >______________________________________________
> > >R-help <at> r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > >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.
> >
> >
>
>
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list