Skip to content

Instantly share code, notes, and snippets.

@audy
Last active February 6, 2021 17:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save audy/faa80238827fdc0c9890 to your computer and use it in GitHub Desktop.
Save audy/faa80238827fdc0c9890 to your computer and use it in GitHub Desktop.
Get Rscript to print line numbers when errors occur

Get Rscript to print line numbers when errors occur

Put this at the top of your executable Rscript file:

options(error = quote({
  dump.frames(to.file=T, dumpto='last.dump')
  load('last.dump.rda')
  print(last.dump)
  q()
}))

Example:

./somescript

Error: I fail!
$`stop("I fail!")`
<environment: 0x100ca5790>

attr(,"error.message")
[1] "Error: I fail!\n"
attr(,"class")
[1] "dump.frames"

Just kidding! You still can't get line numbers for errors in Rscripts but this is slightly more helpful.

Solution modified from StackOverflow question: R script line numbers at error?.

#!/usr/bin/env Rscript
options(error = quote({
dump.frames(to.file=T, dumpto='last.dump')
load('last.dump.rda')
print(last.dump)
q()
}))
stop('I fail!')
@moritzschaefer
Copy link

R is just so painful..

@inodb
Copy link

inodb commented Feb 6, 2021

This is helpful! But yes second @moritzschaefer. Can't believe it has to be this hard to get a stack trace

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment