iuke-tier@ey m@iii@g oii uiow@@edu
2025-Jan-19 16:46 UTC
[R] [External] Re: Parser For Line Number Tracing
On Sun, 19 Jan 2025, Ivo Welch wrote:> Hi Duncan ? Wonderful. Thank you. Bug or no bug, I think it would be > a huge improvement for user-friendliness if R printed the last line by > default *every time* a script dies. Most computer languages do so. > > Should I file it as a request for improvement to the R code > development team? Maybe R can be improved at a very low cost to the > development team and a very high benefit to newbies.No. There are already many ways to influence the way the default error handler prints information about errors, mstly via options(). In particular you may want to look at entries in ?options for show.error.locations showErrorCalls showWarningCalls and adjust your options settings accordingly. Best, luke> > Regards, > > /ivo > > On Sun, Jan 19, 2025 at 2:39?AM Duncan Murdoch <murdoch.duncan at gmail.com> wrote: >> >> On 2025-01-18 8:27 p.m., Ivo Welch wrote: >>> I am afraid my errors are worse! (so are my postings. I should have >>> given an example.) >>> >>> ``` >>> x <- 1 >>> y <- 2 >>> nofunction("something stupid I am doing!") >>> z <- 4 >>> ``` >>> >>> and >>> >>> ``` >>>> source("where-is-my-water.R") >>> Error in nofunction("something stupid I am doing!") : >>> could not find function "nofunction" >>> ``` >>> >>> and no traceback is available. >> >> Okay, I see. In that case traceback() doesn't report the line, but it >> still is known internally. You can see it using the following function: >> >> showKnownLocations <- function() { >> calls <- sys.calls() >> srcrefs <- sapply(calls, function(v) if (!is.null(srcref <- attr(v, >> >> "srcref"))) { >> srcfile <- attr(srcref, "srcfile") >> paste0(basename(srcfile$filename), "#", srcref[1L]) >> } else ".") >> cat("Current call stack locations:\n") >> cat(srcrefs, sep = " ") >> cat("\n") >> } >> >> I haven't done much testing on this, but I think it can be called >> explicitly from any location if you want to know how you got there, or >> you can set it as the error handler using >> >> options(error = showKnownLocations) >> >> For example, try this script: >> >> options(error = showKnownLocations) >> f <- function() showKnownLocations() >> x <- 1 >> f() >> y <- 2 >> nofunction("something stupid I am doing!") >> z <- 4 >> >> I see this output from source("test.R"): >> >> > source("test.R") >> Current call stack locations: >> . . . . test.R#4 test.R#2 >> Error in nofunction("something stupid I am doing!") : >> could not find function "nofunction" >> Current call stack locations: >> . . . . test.R#6 >> >> The first report is from the explicit call in f() on line 2 that was >> invoked on line 4, and the second report happens during error handling. >> >> I supppose the fact that traceback() isn't showing you the line 6 >> location could be considered a bug. >> >> Duncan Murdoch >> >> > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide 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: luke-tierney at uiowa.edu Iowa City, IA 52242 WWW: stat.uiowa.edu
understood. but, please, consider not people like me but unwary beginners and students of R. I have used R now for decades, and even I am baffled by it. Couldn't you make R code easier to debug not only for people like me (who can indeed tweak their environments) but also for novice users? On Sun, Jan 19, 2025 at 8:46?AM <luke-tierney at uiowa.edu> wrote:> > On Sun, 19 Jan 2025, Ivo Welch wrote: > > > Hi Duncan ? Wonderful. Thank you. Bug or no bug, I think it would be > > a huge improvement for user-friendliness if R printed the last line by > > default *every time* a script dies. Most computer languages do so. > > > > Should I file it as a request for improvement to the R code > > development team? Maybe R can be improved at a very low cost to the > > development team and a very high benefit to newbies. > > No. There are already many ways to influence the way the default error > handler prints information about errors, mstly via options(). In > particular you may want to look at entries in ?options for > > show.error.locations > showErrorCalls > showWarningCalls > > and adjust your options settings accordingly. > > Best, > > luke > > > > > Regards, > > > > /ivo > > > > On Sun, Jan 19, 2025 at 2:39?AM Duncan Murdoch <murdoch.duncan at gmail.com> wrote: > >> > >> On 2025-01-18 8:27 p.m., Ivo Welch wrote: > >>> I am afraid my errors are worse! (so are my postings. I should have > >>> given an example.) > >>> > >>> ``` > >>> x <- 1 > >>> y <- 2 > >>> nofunction("something stupid I am doing!") > >>> z <- 4 > >>> ``` > >>> > >>> and > >>> > >>> ``` > >>>> source("where-is-my-water.R") > >>> Error in nofunction("something stupid I am doing!") : > >>> could not find function "nofunction" > >>> ``` > >>> > >>> and no traceback is available. > >> > >> Okay, I see. In that case traceback() doesn't report the line, but it > >> still is known internally. You can see it using the following function: > >> > >> showKnownLocations <- function() { > >> calls <- sys.calls() > >> srcrefs <- sapply(calls, function(v) if (!is.null(srcref <- attr(v, > >> > >> "srcref"))) { > >> srcfile <- attr(srcref, "srcfile") > >> paste0(basename(srcfile$filename), "#", srcref[1L]) > >> } else ".") > >> cat("Current call stack locations:\n") > >> cat(srcrefs, sep = " ") > >> cat("\n") > >> } > >> > >> I haven't done much testing on this, but I think it can be called > >> explicitly from any location if you want to know how you got there, or > >> you can set it as the error handler using > >> > >> options(error = showKnownLocations) > >> > >> For example, try this script: > >> > >> options(error = showKnownLocations) > >> f <- function() showKnownLocations() > >> x <- 1 > >> f() > >> y <- 2 > >> nofunction("something stupid I am doing!") > >> z <- 4 > >> > >> I see this output from source("test.R"): > >> > >> > source("test.R") > >> Current call stack locations: > >> . . . . test.R#4 test.R#2 > >> Error in nofunction("something stupid I am doing!") : > >> could not find function "nofunction" > >> Current call stack locations: > >> . . . . test.R#6 > >> > >> The first report is from the explicit call in f() on line 2 that was > >> invoked on line 4, and the second report happens during error handling. > >> > >> I supppose the fact that traceback() isn't showing you the line 6 > >> location could be considered a bug. > >> > >> Duncan Murdoch > >> > >> > > > > ______________________________________________ > > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > > stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide 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: luke-tierney at uiowa.edu > Iowa City, IA 52242 WWW: stat.uiowa.edu
Luke, The fact that there are alternative ways to do things does not mean that the a new, better way, should not be considered. You note three options that I have never heard for, nor read about despite using R for many years. Ibest most uses don't know about these options. Ivo, was correct when he said, " I think it would be a huge improvement for user-friendliness if R printed the last line by default *every time* a script dies. Most computer languages do so." John John David Sorkin M.D., Ph.D. Professor of Medicine, University of Maryland School of Medicine; Associate Director for Biostatistics and Informatics, Baltimore VA Medical Center Geriatrics Research, Education, and Clinical Center; PI Biostatistics and Informatics Core, University of Maryland School of Medicine Claude D. Pepper Older Americans Independence Center; Senior Statistician University of Maryland Center for Vascular Research; Division of Gerontology and Paliative Care, 10 North Greene Street GRECC (BT/18/GR) Baltimore, MD 21201-1524 Cell phone 443-418-5382 ________________________________________ From: R-help <r-help-bounces at r-project.org> on behalf of luke-tierney--- via R-help <r-help at r-project.org> Sent: Sunday, January 19, 2025 11:46 AM To: Ivo Welch Cc: r-help at r-project.org Subject: Re: [R] [External] Re: Parser For Line Number Tracing On Sun, 19 Jan 2025, Ivo Welch wrote:> Hi Duncan ? Wonderful. Thank you. Bug or no bug, I think it would be > a huge improvement for user-friendliness if R printed the last line by > default *every time* a script dies. Most computer languages do so. > > Should I file it as a request for improvement to the R code > development team? Maybe R can be improved at a very low cost to the > development team and a very high benefit to newbies.No. There are already many ways to influence the way the default error handler prints information about errors, mstly via options(). In particular you may want to look at entries in ?options for show.error.locations showErrorCalls showWarningCalls and adjust your options settings accordingly. Best, luke> > Regards, > > /ivo > > On Sun, Jan 19, 2025 at 2:39?AM Duncan Murdoch <murdoch.duncan at gmail.com> wrote: >> >> On 2025-01-18 8:27 p.m., Ivo Welch wrote: >>> I am afraid my errors are worse! (so are my postings. I should have >>> given an example.) >>> >>> ``` >>> x <- 1 >>> y <- 2 >>> nofunction("something stupid I am doing!") >>> z <- 4 >>> ``` >>> >>> and >>> >>> ``` >>>> source("where-is-my-water.R") >>> Error in nofunction("something stupid I am doing!") : >>> could not find function "nofunction" >>> ``` >>> >>> and no traceback is available. >> >> Okay, I see. In that case traceback() doesn't report the line, but it >> still is known internally. You can see it using the following function: >> >> showKnownLocations <- function() { >> calls <- sys.calls() >> srcrefs <- sapply(calls, function(v) if (!is.null(srcref <- attr(v, >> >> "srcref"))) { >> srcfile <- attr(srcref, "srcfile") >> paste0(basename(srcfile$filename), "#", srcref[1L]) >> } else ".") >> cat("Current call stack locations:\n") >> cat(srcrefs, sep = " ") >> cat("\n") >> } >> >> I haven't done much testing on this, but I think it can be called >> explicitly from any location if you want to know how you got there, or >> you can set it as the error handler using >> >> options(error = showKnownLocations) >> >> For example, try this script: >> >> options(error = showKnownLocations) >> f <- function() showKnownLocations() >> x <- 1 >> f() >> y <- 2 >> nofunction("something stupid I am doing!") >> z <- 4 >> >> I see this output from source("test.R"): >> >> > source("test.R") >> Current call stack locations: >> . . . . test.R#4 test.R#2 >> Error in nofunction("something stupid I am doing!") : >> could not find function "nofunction" >> Current call stack locations: >> . . . . test.R#6 >> >> The first report is from the explicit call in f() on line 2 that was >> invoked on line 4, and the second report happens during error handling. >> >> I supppose the fact that traceback() isn't showing you the line 6 >> location could be considered a bug. >> >> Duncan Murdoch >> >> > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide 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: luke-tierney at uiowa.edu Iowa City, IA 52242 WWW: stat.uiowa.edu ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide r-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Thanks for pointing out the options. Using options(show.error.locations = TRUE) works on Ivo's example, but it doesn't show a location if the error happens in a function that doesn't have srcrefs, because the known location isn't on the top of the stack. Perhaps TRUE (and maybe "top"?) should look back through the stack until it finds a known location, and report that, or another option (e.g. "highest"?) could be added to do that. And still I think traceback() should show the top call in Ivo's example. Duncan On 2025-01-19 11:46 a.m., luke-tierney at uiowa.edu wrote:> On Sun, 19 Jan 2025, Ivo Welch wrote: > >> Hi Duncan ? Wonderful. Thank you. Bug or no bug, I think it would be >> a huge improvement for user-friendliness if R printed the last line by >> default *every time* a script dies. Most computer languages do so. >> >> Should I file it as a request for improvement to the R code >> development team? Maybe R can be improved at a very low cost to the >> development team and a very high benefit to newbies. > > No. There are already many ways to influence the way the default error > handler prints information about errors, mstly via options(). In > particular you may want to look at entries in ?options for > > show.error.locations > showErrorCalls > showWarningCalls > > and adjust your options settings accordingly. > > Best, > > luke > >> >> Regards, >> >> /ivo >> >> On Sun, Jan 19, 2025 at 2:39?AM Duncan Murdoch <murdoch.duncan at gmail.com> wrote: >>> >>> On 2025-01-18 8:27 p.m., Ivo Welch wrote: >>>> I am afraid my errors are worse! (so are my postings. I should have >>>> given an example.) >>>> >>>> ``` >>>> x <- 1 >>>> y <- 2 >>>> nofunction("something stupid I am doing!") >>>> z <- 4 >>>> ``` >>>> >>>> and >>>> >>>> ``` >>>>> source("where-is-my-water.R") >>>> Error in nofunction("something stupid I am doing!") : >>>> could not find function "nofunction" >>>> ``` >>>> >>>> and no traceback is available. >>> >>> Okay, I see. In that case traceback() doesn't report the line, but it >>> still is known internally. You can see it using the following function: >>> >>> showKnownLocations <- function() { >>> calls <- sys.calls() >>> srcrefs <- sapply(calls, function(v) if (!is.null(srcref <- attr(v, >>> >>> "srcref"))) { >>> srcfile <- attr(srcref, "srcfile") >>> paste0(basename(srcfile$filename), "#", srcref[1L]) >>> } else ".") >>> cat("Current call stack locations:\n") >>> cat(srcrefs, sep = " ") >>> cat("\n") >>> } >>> >>> I haven't done much testing on this, but I think it can be called >>> explicitly from any location if you want to know how you got there, or >>> you can set it as the error handler using >>> >>> options(error = showKnownLocations) >>> >>> For example, try this script: >>> >>> options(error = showKnownLocations) >>> f <- function() showKnownLocations() >>> x <- 1 >>> f() >>> y <- 2 >>> nofunction("something stupid I am doing!") >>> z <- 4 >>> >>> I see this output from source("test.R"): >>> >>> > source("test.R") >>> Current call stack locations: >>> . . . . test.R#4 test.R#2 >>> Error in nofunction("something stupid I am doing!") : >>> could not find function "nofunction" >>> Current call stack locations: >>> . . . . test.R#6 >>> >>> The first report is from the explicit call in f() on line 2 that was >>> invoked on line 4, and the second report happens during error handling. >>> >>> I supppose the fact that traceback() isn't showing you the line 6 >>> location could be considered a bug. >>> >>> Duncan Murdoch >>> >>> >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide r-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. >> >