Purpose
To deep practice R- Topic chosen in Debug

Example 1: Complex processing, don’t want to modify code. traceback(),
error=recover().

> options(stringsAsFactors = FALSE)
> library(plyr)
> setwd("C:/Cauldron/garage/R/soulcraft/Volatility/Learn/DeepPractice/Debug")
> all.dat <- read.csv("piratical_dat.csv")
> dat.test <- head(all.dat)
> process.complexiously <- function(df, arg1 = 0.12, arg2 = function(x,
+     q = arg1) ifelse(x < q, x, q + 2), stuff = 0) {
+     do <- sum(stuff)
+     other <- rep(arg1, 44)
+     do <- sum(other, stuff)
+     df2 <- ddply(df, .(rx), transform, wacky.sum = dzyhapi +
+         qwexinox + 7)
+     more.wackiness(df2)
+ }
> more.wackiness <- function(df) {
+     for (i in 1:(nrow(df) * 0.95)) {
+         df[i, "wacky.sum"] <- df[i, "wacky.sum"] + sum(df[, df[i,
+             "rx"]])
+     }
+     df
+ }
> options(width = 80)
> process.complexiously(dat.test)
          rx dzyhapi qqrvibe.AB aarvbyol prlylyfe ytrwow zorncat qwexinox
1   aarvbyol    0.92       0.78     0.98     0.36   0.95    0.53     0.53
2    dzyhapi    0.66       0.58     0.53     0.09   0.99    0.45     0.49
3   prlylyfe    0.35       0.82     0.14     0.37   0.57    0.50     0.92
4 qqrvibe.AB    0.14       0.32     0.21     0.16   0.12    0.18     0.63
5     ytrwow    0.60       0.16     0.51     0.25   0.49    0.50     0.41
6    zorncat    0.13       0.09     0.44     0.35   0.20    0.89     0.72
  xyxyslim axyzoz.XL poyqwr wacky.sum
1     0.37      0.69   0.77     11.26
2     0.06      0.92   0.15     10.95
3     0.10      0.59   0.62      9.85
4     0.59      0.50   0.84     10.52
5     0.91      0.04   0.22     11.33
6     0.31      0.52   0.73      7.85
> tryCatch(process.complexiously(all.dat), error = function(e) {
+     print("Moron, there is an error")
+ })
[1] "Moron, there is an error"

Use Debug on the function

> cats.test <- data.frame(cat = c("fluffy", "spot", "tiger", "baby",
+     "garth", "felix"), height = c(15, 9, 22, 11, 12, 9), weight = c(6,
+     5, 14, 9, 4, 5), rx = c("dzyhapi", "qqrvibe AB", "xyxyslim",
+     "ytrwow", "prlylyfe", "dzyhapi"))
> rxs.test <- data.frame(rx = c("dzyhapi", "qqrvibe AB", "aarvbyol",
+     "prlylyfe", "ytrwow", "zorncat", "qwexinox", "ytrwow", "xyxyslim",
+     "axyzoz XL", "poyqwr"), loss = c(0.04, 0.13, 0.02, 0.04,
+     0.06, 0.1, 0.09, 0.04, 0.01, 0.05, 0.05))
> do.tricky.thing <- function(pets, meds, sd = 0.03) {
+     merged.test <- merge(pets, meds, by = "rx")
+     sim.result <- with(merged.test, loss + rnorm(length(loss),
+         mean = 0, sd = sd))
+     ret <- pets
+     ret$exp.profit <- sim.result
+     ret
+ }

Example 3: Debugging when there’s no error. Conditional breakpoints.

> rxs.test <- data.frame(rx = c("dzyhapi", "qqrvibe AB", "aarvbyol",
+     "prlylyfe", "ytrwow", "zorncat", "qwexinox", "xyxyslim",
+     "axyzoz XL", "poyqwr"), loss = c(0.04, 0.13, 0.02, 0.04,
+     0.06, 0.1, 0.09, 0.01, 0.05, 0.05))
> exps.test <- data.frame(rx = c("dzyhapi", "dzyhapi", "dzyhapi",
+     "prlylyfe", "qqrvibe AB", "qqrvibe AB", "qqrvibe AB", "xyxyslim",
+     "xyxyslim", "xyxyslim", "xyxyslim", "ytrwow", "ytrwow"),
+     Ns = c(15, 30, 36, 16, 26, 36, 42, 44, 20, 24, 19, 32, 29))
> do.trickier.thing <- function(meds, exps, resample.n = 1000) {
+     merged <- merge(exps, meds, by = "rx")
+     meaning.less <- ddply(merged, .(rx), function(df) {
+         data.frame(asdf = mean(sample(df$Ns, resample.n, replace = TRUE)) +
+             max(df$loss))
+     })
+     meaning.less
+ }

You can control the place the trace is invoked

You can control the time the trace is browse is invoked using quote argument