# R Programming/Debugging

R Programming
* R Basics
** Introduction
** Sample Session
** Manage your workspace
** Settings and Installation
** Documentation
** Control Structures
** Working with functions
** Debugging
** Using C or Fortran
** Utilities
** Estimation utilities
** Packages
* Data Management
** Data types
** Working with data frames
** Importing and exporting data
** Text Processing
** Times and Dates
* Graphics
** Grammar of graphics
* Publication quality output
* Descriptive Statistics
* Mathematics
** Optimization
** Probability Distributions
** Random Number Generation
* Statistical Core Methods
** Maximum Likelihood
** Method of Moments
** Bayesian Methods
** Bootstrap
** Multiple Imputation
** Nonparametric Methods
* Regression Models
** Linear Models
** Quantile Regression
** Binomial Models
** Multinomial Models
** Tobit And Selection Models
** Count Data Models
** Duration Analysis
* Time Series
* Factor Analysis
* Classification
** Ordination
** Clustering
* Network Analysis
* High Performance Computing
** Profiling R code
** Parallel computing with R
* Appendix
** Sources
** Index

## Some basic tips

* Use print statements in your functions to print variable values. Although this technique is considered low-tech or even old fashioned by some, it can still be a quick and easy way to trace an error.
* Place a browser statement in the function just before the crashing line. When the function is called, it will be executed up to the browser line. The command-line interface then switches to the function environment, so that all variables in the function can be inspected or changed. See below for commands available in browser mode.

## Tracing errors with traceback

A standard error message in R will tell you which function threw the error. Consider as an example the following function whose sole purpose is to throw an error.
A call to myFun gives
After an error is raised, the traceback function allows you to show the call stack leading to the error. For example, the function below calls myFun.
Calling myFun2 and traceback gives
The traceback function can be executed automatically each time an error is raised with the option
It may be switched off again with

## Executing code line by line

A function can be executed by setting it to debugging mode with
Then, when the function is called, and a browser in that function's environment is opened so that it can be executed line by line. In the debugging browser, apart from all standard R functionality, the following commands are available.
| Command | Meaning |
| n | Advance to next step. An empty line also works. |
| c, cont | Continue to the end of the current context. E.g. to the end the loop within a loop or to the end of the function. |
| where | Print the stack of function calls (where are you?) |
| Q | Exit the browser and return to the top-level R prompt. |
Debugging can be switched off with
There are a few related functions as well:
* debugonce Switch off debugging after the first call.
* isdebugged Check if a function is in degugging mode.

## Browsing the call stack

This is the most advanced debugging option in R base. By setting options(error=recover) you get the opportunity to browse any environment in the call stack. For example,
By typing '1' or '2' behind Selection: the browser will jump to the selected environment. Once in the browser, all standard R functionality is at your disposal, as well as the commands in the table below.
| Command | Meaning |
| c, cont | Exit the browser and continue at the next statement. An empty line will do the same. |
| n | Enter the step-through debugger (this changes the meaning of c) |
| where | Print a stack trace of active function calls (where are you in the stack?). |
| Q | Exit the browser, do not continue at the next statement but go back to the top-level R browser. |
Recovery mode can be switched off by