Skip to content
Prev 350377 / 398506 Next

run Rscript and ignore errors?

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: