Skip to content

Commit

Permalink
Merge commit '264376435b8aac47b808a287d13250523df65e9a'
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurentGatto committed Nov 5, 2014
2 parents cdbe500 + 2643764 commit 54ccd0e
Show file tree
Hide file tree
Showing 7 changed files with 279 additions and 646 deletions.
18 changes: 16 additions & 2 deletions _R-debugging/Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
vectorisation.pdf: debugging.Rnw
all:
make debugging.pdf
make unittesting.md
make testing.md

debugging.pdf: debugging.Rnw
R --vanilla -e "library(knitr); knit('debugging.Rnw')"
pdflatex debugging.tex

unittesting.md: unittesting.Rmd
R --vanilla -e "library(knitr); knit2html('unittesting.Rmd')"
R --vanilla -e "library(knitr); knit('unittesting.Rmd')"

unittesting.html: unittesting.md
R --vanilla -e "markdown::markdownToHTML('unittesting.md', output = 'unittesting.html')"

testing.md: testing.Rmd
R --vanilla -e "library(knitr); knit('testing.Rmd')"

testing.html: testing.md
R --vanilla -e "markdown::markdownToHTML('testing.md', output = 'testing.html')"

.PHONY: clean

Expand Down
5 changes: 3 additions & 2 deletions _R-debugging/debugging.Rnw
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,15 @@

\author[Stojnic and Gatto]
{
Robert~Stojnic <\url{[email protected]}> \\
Laurent~Gatto <\url{[email protected]}> \\
Robert~Stojnic \url{[email protected]} \\
Laurent~Gatto \url{[email protected]} \\
}

\institute[CSBC]
{
% \inst{1}%
University of Cambridge \\
\url{DataProgrammers.net}
}

\begin{document}
Expand Down
Binary file modified _R-debugging/debugging.pdf
Binary file not shown.
96 changes: 96 additions & 0 deletions _R-debugging/testing.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# Why test?

* Nobody writes buggy code... sure!

* How do you know your code is right unless you test?

* Can act as specification/examples of what the programme should do.

# Inverting some data can have serious consequences ...

http://www.sciencemag.org/content/314/5807/1856.full.pdf

2001 Science paper, and two more Science papers retracted after
inversion along x axis noted.

# Testing needs to be:

* reliable
* fast
* easy to run
* easy to summarise

# stop() vs warning()

* A warning is softer than an error; if a warning is generated
your program will still continue, whereas an error will stop the
program.

```{r eval=FALSE,tidy=FALSE}
log(c(2, 1, 0, -1, 2)); print('end') # warning
xor( c(TRUE, FALSE)); print ('end') # error
```

* If you try to isolate warnings, you can change warnings to
errors: `options(warn=2)`

* Add warnings and errors to your code using `warning()`,
`stop()`

```{r eval=TRUE}
x <- -1:1
y <- -5:-1
if ( any(x<0) ) warning("some elements of x are negative")
if ( all(y<0) ) stop("all elements of x are negative")
stopifnot(all.equal(pi, 3.1415927), 2 < 2, all(1:10 < 12), "a" < "b")
```

# Catching errors and warnings

```{r eval=FALSE}
safelog <- function(x) {
tryCatch(log(x),
error = function(e) "an error",
warning = function(e) "a warning")
}
safelog(3)
safelog(-5)
safelog("string")
```

# Floating point issues to be aware of

R FAQ 7.31? http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f


```{r}
a <- sqrt(2)
a * a == 2
a * a - 2
```

```{r,eval=FALSE}
1.0 + 2.0 == 3.0
0.1 + 0.2 == 0.3
```

# Floating point: how to compare

```{r}
all.equal( 0.1 + 0.2, 0.3)
all.equal( 0.1 + 0.2, 3.0)
isTRUE(all.equal(0.1 + 0.2, 3)) # when you just want TRUE/FALSE
```

# Compile this document.

```{r eval=FALSE, compiling}
library("knitr")
knit('testing.Rmd')
knit2html('testing.Rmd')
browseURL('testing.html')
```
148 changes: 148 additions & 0 deletions _R-debugging/testing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
# Why test?

* Nobody writes buggy code... sure!

* How do you know your code is right unless you test?

* Can act as specification/examples of what the programme should do.

# Inverting some data can have serious consequences ...

http://www.sciencemag.org/content/314/5807/1856.full.pdf

2001 Science paper, and two more Science papers retracted after
inversion along x axis noted.

# Testing needs to be:

* reliable
* fast
* easy to run
* easy to summarise

# stop() vs warning()

* A warning is softer than an error; if a warning is generated
your program will still continue, whereas an error will stop the
program.


```r
log(c(2, 1, 0, -1, 2)); print('end') # warning
xor( c(TRUE, FALSE)); print ('end') # error
```

* If you try to isolate warnings, you can change warnings to
errors: `options(warn=2)`

* Add warnings and errors to your code using `warning()`,
`stop()`


```r
x <- -1:1
y <- -5:-1

if ( any(x<0) ) warning("some elements of x are negative")
```

```
## Warning: some elements of x are negative
```

```r
if ( all(y<0) ) stop("all elements of x are negative")
```

```
## Error in eval(expr, envir, enclos): all elements of x are negative
```

```r
stopifnot(all.equal(pi, 3.1415927), 2 < 2, all(1:10 < 12), "a" < "b")
```

```
## Error: 2 < 2 is not TRUE
```

# Catching errors and warnings


```r
safelog <- function(x) {
tryCatch(log(x),
error = function(e) "an error",
warning = function(e) "a warning")
}

safelog(3)
safelog(-5)
safelog("string")
```

# Floating point issues to be aware of

R FAQ 7.31? http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f



```r
a <- sqrt(2)
a * a == 2
```

```
## [1] FALSE
```

```r
a * a - 2
```

```
## [1] 4.440892e-16
```


```r
1.0 + 2.0 == 3.0
0.1 + 0.2 == 0.3
```

# Floating point: how to compare


```r
all.equal( 0.1 + 0.2, 0.3)
```

```
## [1] TRUE
```

```r
all.equal( 0.1 + 0.2, 3.0)
```

```
## [1] "Mean relative difference: 9"
```

```r
isTRUE(all.equal(0.1 + 0.2, 3)) # when you just want TRUE/FALSE
```

```
## [1] FALSE
```

# Compile this document.


```r
library("knitr")
knit('testing.Rmd')
knit2html('testing.Rmd')
browseURL('testing.html')
```
Loading

0 comments on commit 54ccd0e

Please sign in to comment.