This report is automatically generated with the R package knitr (version 1.5) .

# Chapter 10 Debugging Your Code NOTE : Much code is commented out, as they generate errors
# on purpose. Uncomment the code and run the line to see the error and try the debugging
# out Knowing What to Look For Reading Errors and Warnings Reading error messages 'a' + 1
# Error in 'a' + 1 : non-numeric argument to binary operator data.frame(1:10,10:1,) Error
# in data.frame(1:10, 10:1, ) : argument is missing, with no default Caring about warnings
# (or not)
x <- 1:10
y <- if (x < 5) 0 else 1
## Warning: the condition has length > 1 and only the first element will be used
x <- 4
sqrt(x - 5)
## Warning: NaNs produced
## [1] NaN
plot(1:10, 10:1, color = "green")
## Warning: "color" is not a graphical parameter
## Warning: "color" is not a graphical parameter
## Warning: "color" is not a graphical parameter
## Warning: "color" is not a graphical parameter
## Warning: "color" is not a graphical parameter
## Warning: "color" is not a graphical parameter
plot of chunk unnamed-chunk-1
# Going Bug Hunting Calculating the logit checks input and does logit calculation
logit <- function(x) {
    x <- ifelse(x < 0 | x > 1, "NA", x)
    log(x/(1 - x))
}
# transforms percentage to number and calls logit
logitpercent <- function(x) {
    x <- gsub("%", "", x)
    logit(as.numeric(x))
}
## Knowing where an error comes from logitpercent('50%') Error in 1 - x : non-numeric
## argument to binary operator traceback() Looking inside a function Telling R which
## function to debug debug(logit) logitpercent('50%') Stepping through the function Start
## browsing from within the function
logit <- function(x) {
    x <- ifelse(x < 0 | x > 1, "NA", x)
    browser()
    log(x/(1 - x))
}
# logit(50) Generating Your Own Messages Creating errors
logit <- function(x) {
    if (any(x < 0 | x > 1))
        stop("x not between 0 and 1")
    log(x/(1 - x))
}
# logitpercent(c('50%','150%')) Error in logit(as.numeric(x)/100) : x not between 0 and 1
# Creating warnings Function wrapped around for illustrative purposes In book only body is
# given
logit <- function(x) {
    x <- ifelse(x < 0 | x > 1, NA, x)
    if (any(is.na(x)))
        warning("x not between 0 and 1")
    log(x/(1 - x))
}
logitpercent(c("50%", "150%"))
## Warning: x not between 0 and 1
## [1] NA NA
# Recognizing the Mistakes You're Sure to Make Starting with the wrong data Having your
# data in the wrong format Dropping dimensions when you don’t expect it
rowsum.df <- function(x) {
    id <- sapply(x, is.numeric)
    rowSums(x[, id])
}
# rowsum.df(sleep) Messing up with lists
strsplit("this is a sentence", " ")[2]
## [[1]]
## NULL
strsplit("this is a sentence", " ")
## [[1]]
## [1] "this"     "is"       "a"        "sentence"
strsplit("this is a sentence", " ")[[1]][2]
## [1] "is"
customer <- c("Johan Delong", "Marie Petit")
namesplit <- strsplit(customer, " ")
paste(namesplit[2], collapse = ".")
## [1] "c(\"Marie\", \"Petit\")"
paste(namesplit[[2]], collapse = ".")
## [1] "Marie.Petit"
### Mixing up factors and numeric vectors
cyl.factor <- as.factor(mtcars$cyl)
median(as.numeric(cyl.factor))
## [1] 2
as.numeric(levels(cyl.factor))[cyl.factor]
##  [1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4

The R session information (including the OS info, R version and all packages used):

sessionInfo()
## R version 3.0.2 (2013-09-25)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## 
## locale:
## [1] LC_COLLATE=English_United Kingdom.1252  LC_CTYPE=English_United Kingdom.1252   
## [3] LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C                           
## [5] LC_TIME=English_United Kingdom.1252    
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
##  [1] BiocInstaller_1.12.1 ggplot2_0.9.3.1      reshape2_1.2.2       sos_1.3-8           
##  [5] brew_1.0-6           stringr_0.6.2        knitr_1.5            plyr_1.8            
##  [9] Revobase_7.1.0       RevoMods_7.1.0       RevoScaleR_7.1.0     lattice_0.20-27     
## [13] rpart_4.1-2         
## 
## loaded via a namespace (and not attached):
##  [1] codetools_0.2-8    colorspace_1.2-4   dichromat_2.0-0    digest_0.6.4      
##  [5] evaluate_0.5.1     foreach_1.4.1      formatR_0.10       fortunes_1.5-2    
##  [9] grid_3.0.2         gtable_0.1.2       highr_0.3          iterators_1.0.6   
## [13] labeling_0.2       MASS_7.3-29        munsell_0.4.2      proto_0.3-10      
## [17] RColorBrewer_1.0-5 scales_0.2.3       tools_3.0.2
Sys.time()
## [1] "2014-05-13 15:05:46 BST"