traceback provides a trail of functions to track where and why an error occurred. For instance, if the error was deeply nested, then traceback is sort of like a road map showing where the error occurred.

Example. Below the getReturns function from the stockPortfolio package is used to retrieve stock performance data for both Research in Motion (RIMM), Apple (AAPL), and a third stock that does not exist. The traceback function is used here to query the source of the error, and it is nested within several layers of functions. Seeing that there was a problem with a call to a URL (read.delim(URL, …)) is a hint that there may be trouble with a stock that I chose.
As a developer, the traceback function can be powerfully paired with the debug function. First identify the function call that is causing trouble, then use debug to go through the function, one line at a time, until arriving at that line of code, then investigate the arguments (e.g. URL) to identify what went wrong.
> # install.packages("stockPortfolio")
> library(stockPortfolio)
> gr2 <- getReturns(c("RIMM", "AAPL", "oweifjwoej"))
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") : cannot open: HTTP status was '404 Not Found'
> traceback()
4: file(file, "rt")
3: read.table(file = file, header = header, sep = sep, quote = quote, 
       dec = dec, fill = fill, comment.char = comment.char, ...)
2: read.delim(URL, TRUE, sep = ",")
1: getReturns(c("RIMM", "AAPL", "oweifjwoej"))

Leave a Reply