Andrew J. Bass
06-10-2014
There are two ways to create a R package:
package.skeleton(name = "sample")
In RStudio:
You will notice a few things:
Let's create a simple program that determines all the prime numbers
getPrimed <- function(x) {
allIntegers <- 2:x
i <- 1
while (allIntegers[i] <= ceiling(sqrt(x))) {
bool <- allIntegers %% allIntegers[i] != 0
bool[i] <- TRUE
allIntegers <- allIntegers[bool]
i <- i + 1
}
return(allIntegers)
}
package.skeleton(list=c("getPrimed"), name = "getPrimed")
?
help()
install.packages("roxygen2")
#' @title
#' @details
#' @author
#' @examples
tmp <- function(x) {
print(x)
}
#' @description The function getPrimed determines all the prime numbers up to a specific value
#'
#' @details x must be an integer > 1
#'
#' @title getPrimed: The Prime Function
#' @param x numeric number
#' @return list of prime numbers up to x
#' @examples
#' a <- 250
#' primes <- getPrimed(a)
#' @export
getPrimed <- function(x){
...
}
#' @description prime numbers in both x and y
#'
#' @param y second parameter
#' @inheritParams getPrimed
#' @export
getPrimed2 <- function(x, y) {
gpx <- getPrimed(x)
gpy <- getPrimed(y)
gpx[getPrimed(x) %in% getPrimed(y)]
}
#' @rdname getPrimed
getPrimed <- function(x) {
...
}
#' @rdname getPrimed
getPrimed2 <- function(x, y) {
...
}
library(getPrtimed)
getPrimed(-10)
Error in while (allIntegers[i] <= ceiling(sqrt(x))) { :
missing value where TRUE/FALSE needed
Testing functions in a package is essential and can be time consuming! The testthat package allows for automated testing.
Hadley Wickam: “I started automating my tests because I discovered I was spending too much time recreating bugs that I had previously fixed.”
Testing in the package has a hierarchical structure:
install.packages("testthat")
Expectations test whether a value is what you expect!
library(testthat)
b <- getPrimed(5)
expect_that(c(2, 3, 5), equals(b))
expect_that(getPrimed(13,12), throws_error())
expect_that(getPrimed(0), throws_error())
Error: getPrimed(0) code did not generate an error
expect_that(getPrimed(-10), throws_error())
Warning message:
In sqrt(x) : NaNs produced
expect_that(getPrimed(10.2), gives_warning())
Error: getPrimed(10.2) no warnings given
Each test should be testing a single functionality
test_that("getPrimed handles positive numerics > 1", {
b <- getPrimed(5)
expect_that(c(2, 3, 5), equals(b))
expect_that(getPrimed(10.2), gives_warning())
expect_that(getPrimed(1), gives_error())
### Can include plenty more tests! ###
})
Context: Group tests into blocks that have related functionality
context("getPrimed: testing input variable x")
test_that("getPrimed handles positive integers > 1", {
b <- getPrimed(5)
expect_that(c(2, 3, 5), equals(b))
expect_that(getPrimed(10.2), gives_warning())
})
test_that("getPrimed handles integers <= 1 ", {
expect_that(getPrimed(0), throws_error())
expect_that(getPrimed(-10), throws_error())
})
library(testthat)
test_check("getPrimed")
edgeSet
edgeFit
lrt
odp
mNull <- ~sex
mFull <- ~sex + ns(age, df=3, intercept = FALSE)
edgeObj <- edgeSet(expSet, full.model = mFull, null.model = mNull)
out.odp <- odp(edgeObj)
out.lrt <- lrt(edgeObj)