Can you make R print more detailed error messages?

16,016

Solution 1

Try some of the suggestions in this post:

General suggestions for debugging in R

Specifically, findLineNum() and traceback()/setBreakpoint().

Solution 2

@Nathan Well add this line sink(stdout(), type="message") at the beginning of the script and you should get in console message both script content and output along with error message so you can see it as in interactive mode in the console. (you can then also redirect to a log file if you prefer keeping the console "clean")

Share:
16,016
nsheff
Author by

nsheff

Computational biologist interested in large datasets, machine learning, automation, and high performance computing. I work in R, Python, and C++.

Updated on June 19, 2022

Comments

  • nsheff
    nsheff almost 2 years

    I've often been frustrated by R's cryptic error messages. I'm not talking about during an interactive session, I mean when you're running a script. Error messages don't print out line numbers, and it's often hard to trace the offending line, and the reason for the error (even if you can find the location).

    Most recently my R script failed with the the incredibly insightful message: "Execution halted." The way I usually trace such errors is by putting a lot of print statements throughout the script -- but this is a pain. I sometimes have to go through the script line by line in an interactive session to find the error.

    Does anyone have a better solution for how to make R error output more informative?

    EDIT: Many R-debugging things work for interactive sessions. I'm looking for help on command-line scripts run through Rscript. I'm not in the middle of an R session when the error happens, I'm at the bash shell. I can't run "traceback()"

  • nsheff
    nsheff over 12 years
    Most of these suggestions pertain to loading interactive R and running scripts with source(). What about using Rscript on the command-line? I went through all these suggestions but can't figure out how to print out line numbers. It only works when using source()
  • nsheff
    nsheff over 12 years
    This actually doesn't change anything for me
  • Ari B. Friedman
    Ari B. Friedman about 9 years
    @sheffien For interactive use, either options(error=recover) or debugonce can be helpful.
  • EngrStudent
    EngrStudent almost 7 years
    does this change something that should be changed back? Is it like "par"?