Problem with geterrmessage()

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Problem with geterrmessage()

Fisher Dennis
R 3.4.3
OS X

Colleagues

I have a 20K line script in which I encounter an unexpected problem.

If the script detects presence of a particular file USERCODE.txt, it executes:
        source(“USERCODE.txt”)
If that file is not present, the script executes without a problem.

There might be syntax errors in USERCODE.txt; therefore, the code above is embedded in a try command:
        try(source(“USERCODE.txt", local=T), silent=T)
followed by:
        ERRORMESSAGE <- geterrmessage()

For unclear reasons, an earlier command is yielding an error message:
        unused argument (\"\\n\")
Despite identifying the exact source of that error, I can’t fix it (and it is of no consequence).

Ideally, I would like to clear out the pre-existing error message immediately before the “try” command (or perhaps at that particular location where it is being created) — but I can’t figure out how to do so.

Any suggestions would be welcome.

Dennis

Dennis Fisher MD
P < (The "P Less Than" Company)
Phone / Fax: 1-866-PLessThan (1-866-753-7784)
www.PLessThan.com

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Problem with geterrmessage()

luke-tierney
Only the default error handler puts the error message in a buffer
where it can be retrieved with geterrmessage. try() replaces the
default error handler. Either look at the value returned by try() or
use tryCatch with conditionMessage.

Best,

luke

On Thu, 22 Feb 2018, Dennis Fisher wrote:

> R 3.4.3
> OS X
>
> Colleagues
>
> I have a 20K line script in which I encounter an unexpected problem.
>
> If the script detects presence of a particular file USERCODE.txt, it executes:
> source(“USERCODE.txt”)
> If that file is not present, the script executes without a problem.
>
> There might be syntax errors in USERCODE.txt; therefore, the code above is embedded in a try command:
> try(source(“USERCODE.txt", local=T), silent=T)
> followed by:
> ERRORMESSAGE <- geterrmessage()
>
> For unclear reasons, an earlier command is yielding an error message:
> unused argument (\"\\n\")
> Despite identifying the exact source of that error, I can’t fix it (and it is of no consequence).
>
> Ideally, I would like to clear out the pre-existing error message immediately before the “try” command (or perhaps at that particular location where it is being created) — but I can’t figure out how to do so.
>
> Any suggestions would be welcome.
>
> Dennis
>
> Dennis Fisher MD
> P < (The "P Less Than" Company)
> Phone / Fax: 1-866-PLessThan (1-866-753-7784)
> www.PLessThan.com
>
> ______________________________________________
> [hidden email] 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.
>

--
Luke Tierney
Ralph E. Wareham Professor of Mathematical Sciences
University of Iowa                  Phone:             319-335-3386
Department of Statistics and        Fax:               319-335-3017
    Actuarial Science
241 Schaeffer Hall                  email:   [hidden email]
Iowa City, IA 52242                 WWW:  http://www.stat.uiowa.edu
______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Problem with geterrmessage()

Fisher Dennis
Luke

Thanks — I revised the code to:
        ERRORMESSAGE <- try(source(USERSCRIPTFILE, local=T), silent=T)

print(ERRORMESSAGE) now returns:
$value
[1] 0

$visible
[1] FALSE

Not clear what to make of that.

Dennis


Dennis Fisher MD
P < (The "P Less Than" Company)
Phone / Fax: 1-866-PLessThan (1-866-753-7784)
www.PLessThan.com




> On Feb 22, 2018, at 12:45 PM, [hidden email] wrote:
>
> Only the default error handler puts the error message in a buffer
> where it can be retrieved with geterrmessage. try() replaces the
> default error handler. Either look at the value returned by try() or
> use tryCatch with conditionMessage.
>
> Best,
>
> luke
>
> On Thu, 22 Feb 2018, Dennis Fisher wrote:
>
>> R 3.4.3
>> OS X
>>
>> Colleagues
>>
>> I have a 20K line script in which I encounter an unexpected problem.
>>
>> If the script detects presence of a particular file USERCODE.txt, it executes:
>> source(“USERCODE.txt”)
>> If that file is not present, the script executes without a problem.
>>
>> There might be syntax errors in USERCODE.txt; therefore, the code above is embedded in a try command:
>> try(source(“USERCODE.txt", local=T), silent=T)
>> followed by:
>> ERRORMESSAGE <- geterrmessage()
>>
>> For unclear reasons, an earlier command is yielding an error message:
>> unused argument (\"\\n\")
>> Despite identifying the exact source of that error, I can’t fix it (and it is of no consequence).
>>
>> Ideally, I would like to clear out the pre-existing error message immediately before the “try” command (or perhaps at that particular location where it is being created) — but I can’t figure out how to do so.
>>
>> Any suggestions would be welcome.
>>
>> Dennis
>>
>> Dennis Fisher MD
>> P < (The "P Less Than" Company)
>> Phone / Fax: 1-866-PLessThan (1-866-753-7784)
>> www.PLessThan.com
>>
>> ______________________________________________
>> [hidden email] 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.
>>
>
> --
> Luke Tierney
> Ralph E. Wareham Professor of Mathematical Sciences
> University of Iowa                  Phone:             319-335-3386
> Department of Statistics and        Fax:               319-335-3017
>   Actuarial Science
> 241 Schaeffer Hall                  email:   [hidden email]
> Iowa City, IA 52242                 WWW:  http://www.stat.uiowa.edu

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Problem with geterrmessage()

Bert Gunter-2
Please read ?try (again) carefully.In paticular note (under Value):

"The value of the expression if expr is evaluated without error, but an
invisible object of class "try-error"containing the error message, and the
error condition as the "condition" attribute, if it fails."

so:

attr(ERRORMESSAGE, "conditon") will give you the error conditon.

Cheers,
Bert



Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )

On Thu, Feb 22, 2018 at 1:01 PM, Dennis Fisher <[hidden email]> wrote:

> Luke
>
> Thanks — I revised the code to:
>         ERRORMESSAGE <- try(source(USERSCRIPTFILE, local=T), silent=T)
>
> print(ERRORMESSAGE) now returns:
> $value
> [1] 0
>
> $visible
> [1] FALSE
>
> Not clear what to make of that.
>
> Dennis
>
>
> Dennis Fisher MD
> P < (The "P Less Than" Company)
> Phone / Fax: 1-866-PLessThan (1-866-753-7784)
> www.PLessThan.com
>
>
>
>
> > On Feb 22, 2018, at 12:45 PM, [hidden email] wrote:
> >
> > Only the default error handler puts the error message in a buffer
> > where it can be retrieved with geterrmessage. try() replaces the
> > default error handler. Either look at the value returned by try() or
> > use tryCatch with conditionMessage.
> >
> > Best,
> >
> > luke
> >
> > On Thu, 22 Feb 2018, Dennis Fisher wrote:
> >
> >> R 3.4.3
> >> OS X
> >>
> >> Colleagues
> >>
> >> I have a 20K line script in which I encounter an unexpected problem.
> >>
> >> If the script detects presence of a particular file USERCODE.txt, it
> executes:
> >>      source(“USERCODE.txt”)
> >> If that file is not present, the script executes without a problem.
> >>
> >> There might be syntax errors in USERCODE.txt; therefore, the code above
> is embedded in a try command:
> >>      try(source(“USERCODE.txt", local=T), silent=T)
> >> followed by:
> >>      ERRORMESSAGE <- geterrmessage()
> >>
> >> For unclear reasons, an earlier command is yielding an error message:
> >>      unused argument (\"\\n\")
> >> Despite identifying the exact source of that error, I can’t fix it (and
> it is of no consequence).
> >>
> >> Ideally, I would like to clear out the pre-existing error message
> immediately before the “try” command (or perhaps at that particular
> location where it is being created) — but I can’t figure out how to do so.
> >>
> >> Any suggestions would be welcome.
> >>
> >> Dennis
> >>
> >> Dennis Fisher MD
> >> P < (The "P Less Than" Company)
> >> Phone / Fax: 1-866-PLessThan (1-866-753-7784)
> >> www.PLessThan.com
> >>
> >> ______________________________________________
> >> [hidden email] 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.
> >>
> >
> > --
> > Luke Tierney
> > Ralph E. Wareham Professor of Mathematical Sciences
> > University of Iowa                  Phone:             319-335-3386
> > Department of Statistics and        Fax:               319-335-3017
> >   Actuarial Science
> > 241 Schaeffer Hall                  email:   [hidden email]
> > Iowa City, IA 52242                 WWW:  http://www.stat.uiowa.edu
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Problem with geterrmessage()

Martin Maechler
In reply to this post by Fisher Dennis
>>>>> Dennis Fisher <[hidden email]>
>>>>>     on Thu, 22 Feb 2018 13:01:37 -0800 writes:

    > Luke
    > Thanks — I revised the code to:
    > ERRORMESSAGE <- try(source(USERSCRIPTFILE, local=T), silent=T)

    > print(ERRORMESSAGE) now returns:
    > $value
    > [1] 0

    > $visible
    > [1] FALSE

    > Not clear what to make of that.
    > Dennis

The help page   ?try   has

contained for a long time

     ‘try’ is implemented using ‘tryCatch’; for programming, instead of
     ‘try(expr, silent = TRUE)’, something like ‘tryCatch(expr, error =
     function(e) e)’ (or other simple error handler functions) may be
     more efficient and flexible.

and you do use 'silent=T' (which is "unsafe" (*) !)

I'd strongly advocate you use the 2nd proposition by given by
Luke Tierney (cited below):
 Learn to use tryCatch() instead of try() and then such things
 can be done considerably less obscurely.

Best,
Martin Maechler, ETH Zurich

--
*) Using 'T' instead of 'TRUE'  (of 'F' instead of 'FALSE' *is* unsafe):
   a previous  'T <- 0'  will change what you really wanted.
   TRUE and FALSE are  "constants" in R, whereas  T and F are variables


    > Dennis Fisher MD
    > P < (The "P Less Than" Company)
    > Phone / Fax: 1-866-PLessThan (1-866-753-7784)
    > www.PLessThan.com




    >> On Feb 22, 2018, at 12:45 PM, [hidden email] wrote:
    >>
    >> Only the default error handler puts the error message in a buffer
    >> where it can be retrieved with geterrmessage. try() replaces the
    >> default error handler. Either look at the value returned by try() or
    >> use tryCatch with conditionMessage.
    >>
    >> Best,
    >>
    >> luke
    >>
    >> On Thu, 22 Feb 2018, Dennis Fisher wrote:
    >>
    >>> R 3.4.3
    >>> OS X
    >>>
    >>> Colleagues
    >>>
    >>> I have a 20K line script in which I encounter an unexpected problem.
    >>>
    >>> If the script detects presence of a particular file USERCODE.txt, it executes:
    >>> source(“USERCODE.txt”)
    >>> If that file is not present, the script executes without a problem.
    >>>
    >>> There might be syntax errors in USERCODE.txt; therefore, the code above is embedded in a try command:
    >>> try(source(“USERCODE.txt", local=T), silent=T)
    >>> followed by:
    >>> ERRORMESSAGE <- geterrmessage()
    >>>
    >>> For unclear reasons, an earlier command is yielding an error message:
    >>> unused argument (\"\\n\")
    >>> Despite identifying the exact source of that error, I can’t fix it (and it is of no consequence).
    >>>
    >>> Ideally, I would like to clear out the pre-existing error message immediately before the “try” command (or perhaps at that particular location where it is being created) — but I can’t figure out how to do so.
    >>>
    >>> Any suggestions would be welcome.
    >>>
    >>> Dennis
    >>>
    >>> Dennis Fisher MD
    >>> P < (The "P Less Than" Company)
    >>> Phone / Fax: 1-866-PLessThan (1-866-753-7784)
    >>> www.PLessThan.com
    >>>
    >>> ______________________________________________
    >>> [hidden email] 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.
    >>>
    >>
    >> --
    >> Luke Tierney
    >> Ralph E. Wareham Professor of Mathematical Sciences
    >> University of Iowa                  Phone:             319-335-3386
    >> Department of Statistics and        Fax:               319-335-3017
    >> Actuarial Science
    >> 241 Schaeffer Hall                  email:   [hidden email]
    >> Iowa City, IA 52242                 WWW:  http://www.stat.uiowa.edu

    > ______________________________________________
    > [hidden email] 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.

______________________________________________
[hidden email] 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.