Neal Richardson
neal@crunch.io
@enpiar
June 29, 2016
Revised version of 2016 R NYC Conference talk, with Mike Malecki
source("mycode.R")
df <- read.csv("data.csv")
doThings(df)
library(rmycode)
df <- read.csv("data.csv")
doThings(df)
Use a package skeleton, such as https://github.com/nealrichardson/skeletor
enpiar:c npr$ R -e 'skeletor::skeletor("elbr")'
enpiar:c npr$ cd elbr
enpiar:elbr npr$ atom .
# elbr/tests/testthat/test-read.R
context("read.elb")
test_that("read.elb returns a data.frame", {
expect_true(is.data.frame(read.elb("example.log")))
})
enpiar:elbr npr$ make test
...
Loading required package: elbr
read.elb: 1
Failed -------------------------------------------------------------------------
1. Error: read.elb returns a data.frame (@test-something.R#4) ------------------
could not find function "read.elb"
1: .handleSimpleError(function (e)
{
e$call <- sys.calls()[(frame + 11):(sys.nframe() - 2)]
register_expectation(e, frame + 11, sys.nframe() - 2)
signalCondition(e)
}, "could not find function \"read.elb\"", quote(eval(expr, envir, enclos))) at testthat/test-something.R:4
2: eval(expr, envir, enclos)
DONE ===========================================================================
Error: Test failures
# elbr/R/read-elb.R
read.elb <- function (file, stringsAsFactors=FALSE, ...) {
read.delim(file,
sep=" ",
stringsAsFactors=stringsAsFactors,
col.names=c("timestamp", "elb", "client_port", "backend_port",
"request_processing_time", "backend_processing_time",
"response_processing_time", "elb_status_code",
"backend_status_code", "received_bytes", "sent_bytes",
"request", "user_agent", "ssl_cipher", "ssl_protocol"),
...)
}
enpiar:elbr npr$ make test
...
Loading required package: elbr
read.elb: .
DONE ===========================================================================
test_that("read.elb returns a data.frame", {
df <- read.elb("example.log")
expect_true(is.data.frame(df))
expect_equal(dim(df), c(4, 15))
})
enpiar:elbr npr$ make test
...
Loading required package: elbr
read.elb: .1
Failed -------------------------------------------------------------------------
1. Failure: read.elb returns a data.frame (@test-something.R#6) ----------------
dim(df) not equal to c(4, 15).
1/2 mismatches
[1] 3 - 4 == -1
DONE ===========================================================================
Error: Test failures
read.elb <- function (file, stringsAsFactors=FALSE, ...) {
read.delim(file,
sep=" ",
header=FALSE, # <-- Oh, right.
stringsAsFactors=stringsAsFactors,
col.names=c("timestamp", "elb", "client_port", "backend_port",
"request_processing_time", "backend_processing_time",
"response_processing_time", "elb_status_code",
"backend_status_code", "received_bytes", "sent_bytes",
"request", "user_agent", "ssl_cipher", "ssl_protocol"),
...)
}
enpiar:elbr npr$ make test
...
Loading required package: elbr
read.elb: ..
DONE ===========================================================================
num [1:4, 1:4, 1:6, 1:51, 1:3] 0.0196 0.0414 0.038 0.0106 0.0167 ...
- attr(*, "dimnames")=List of 5
..$ edu : chr [1:4] "<HS" "HS" "Some" "Grad"
..$ age : chr [1:4] "18-29" "30-44" "45-64" "≥65"
..$ race.female: chr [1:6] "White M" "Black M" "Hispanic M" "White F" ...
..$ state : chr [1:51] "AK" "AL" "AR" "AZ" ...
..$ party : chr [1:3] "R" "I" "D"
Because that someone else could be your future self
Neal Richardson
neal@crunch.io
@enpiar