[R] fatal error prevents multi-line functions or loop to run

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Tue Apr 3 05:06:38 CEST 2012


Since you want to progress to further statements within test(), you need to wrap the summary function call in the try, not the whole test() function:

try( summary(w), silent =TRUE )
---------------------------------------------------------------------------
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.

Ebrahim Jahanshiri <e.jahanshiri at gmail.com> wrote:

>Thanks jeff,
>
>You are right about my expectation so if you have time please take a
>look
>at this:
>
>library(spdep)
>
>test <- function () {
>#read neighborhood list
>  neigh.gal <- read.gal("
>http://sites.google.com/site/geosciej/Home/ter1RIorigM_T1.GAL?revision=1")
>
>#Making a weight matrix (listW)
>  w <- nb2listw(neigh.gal,style="B", zero.policy=TRUE)
>
>  sink("w.txt")
>  summary(w)
>  sink()
>#Did the execution reached this point?
>  print("the function reached this point in the code")
>
>}
>
>
>and the result would be:
>
>> test()
>Error in print(data.frame(rbind(unlist(spweights.constants(object,
>zero.policy = zero.policy))[c(1,  :
>error in evaluating the argument 'x' in selecting a method for function
>'print': Error in spweights.constants(object, zero.policy =
>zero.policy) :
>  regions with no neighbours found
>
>obviously the function does not go beyod sink().
>
>the problem is at the print stage. the function does the job and this
>error
>is expected but still it halts the execution of function running (and
>the
>problem is not the sink() function, I tried).
>
>I tried
>
>try(test())
>options(showErrorCalls=F)
>options(show.error.messages=F)
>
>for tryCatch() I could put the whole other part of the function in the
>"finally" argument but it is not practical with many lines of code
>ahead.
>
>I expect the code to continue executing regardless of any error or
>warning.
>
>appreciate it
>
>Ebrahim
>
>
>
>
>On Tue, Apr 3, 2012 at 5:45 AM, Jeff Newmiller
><jdnewmil at dcn.davis.ca.us>wrote:
>
>> Sounds like you tried everything I know. However, I, and anyone else
>on
>> this list whose mind-reading powers are undeveloped, really can't be
>sure
>> because you didn't show us what you tried. Perhaps you should
>(re-)read the
>> posting guidelines and try showing us reproducible sample code that
>doesn't
>> do what you expect it to (along with some indication of what you
>expected
>> it to do).
>>
>---------------------------------------------------------------------------
>> 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.
>>
>> Ebrahim Jahanshiri <e.jahanshiri at gmail.com> wrote:
>>
>> >Dear all,
>> >
>> >I have written some function that can go up to many lines. Two of
>the
>> >lines
>> >give warning errors like:
>> >
>> >Error in print(....)[c(1,  :
>> >  error in evaluating the argument 'x' in selecting a method for
>> >function 'print': Error in ...
>> >  regions with no neighbours found
>> >
>> >
>> >now obviously this kind of error will halt the execution of the
>> >function. I
>> >tried *try()* or *tryCatch()* and many other selections in the
>> >*options()*but still this error by all means stops the execution of
>> >the function or
>> >any other multi-line code.
>> >
>> >I would appreciate if you could give me a clue.
>> >
>> >Ebrahim
>> >
>> >       [[alternative HTML version deleted]]
>> >
>> >______________________________________________
>> >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.
>>
>>



More information about the R-help mailing list