library(dplyr) myFunction = function(values, groups){ result = data.frame(value = values, group = groups) %>% group_by_('group') %>% summarize_(avg = ~mean(value, na.rm = TRUE)) return(result) }
install.packages(c('devtools', 'testthat', 'covr'))
Log into GitHub and create a new repository (click the plus sign in the top-right corner and choose New repository...)
r
^.*\.Rproj$
^\.Rproj\.user$
^.travis.yml$
r
devtools::setup(rstudio = FALSE)
r
devtools::use_testthat()
Set up a continues integration with code coverage checks
Create file .travis.yml in the root directory of your project
Fill it with:
(adjust the notification email)
```r
# http://docs.travis-ci.com/user/languages/r/
language: r
sudo: required
after_success: - Rscript -e 'library(covr);coveralls()'
warnings_are_errors: false
notifications:
email:
- your@email
- if your package depends on packages available on GitHub but not on CRAN, add a section:
r
r_github_packages:
- user1/repoName1
- user2/repoName2
- Log in to Travis at https://travis-ci.org/
- Go to https://travis-ci.org/profile/
- Turn on Travis for your new repository
- If you can not see your repository on the list, you may refresh the list with the _Sync_ button
![](img/travisEnable.png)
- Add Travis and Coveralls badges to your package _README.md_ file:
(adjust your login and repository name in URLs)
(you can also copy badges code from Travis/Coveralls web sites)
r
# sampleProject
Now lets create a simple function which will take two arguments and return average value of the first one grouped according to values of the second one
Create file myFunction.R in the R directory with a following content:
myFunction = function(values, groups){ result = data.frame(value = values, group = groups) %>% group_by_('group') %>% summarize_(avg = ~mean(value, na.rm = TRUE)) return(result) }
Remarks:
We will use Roxygen syntax.
The easiest way to introduce it will be an example, so lets look at the myFunction.R after adding documentation in a Roxygen syntax:
#' Computes means by groups #' @description #' You can put extended description here #' @details #' If your function is complicated, you may consider adding a details section #' @param values numeric vector of values #' @param groups vector of groups #' @return data.frame with groups names in first column and average values per group in the second one #' @export #' @import dplyr myFunction = function(values, groups){ result = data.frame(value = values, group = groups) %>% group_by(group) %>% summarize(avg = mean(value, na.rm = TRUE)) return(result) }
Remarks:
r
myFunction(1:10, rep(1:2, each = 5))
r
?myFunction
We will use the testthat library to prepare tests.
r
test_that('my first test', {
expect_equal(
myFunction(1:10, rep(1:2, each = 5)),
data.frame(group = c(1, 2), avg = c(3, 8))
)
})
Remarks:You should get something like: ```r Loading sampleProject Loading required package: testthat Loading required package: dplyr
Attaching package: ‘dplyr’
Następujące obiekty zostały zakryte z ‘package:stats’:
filter, lag
Następujące obiekty zostały zakryte z ‘package:base’:
intersect, setdiff, setequal, union
Loading required package: ggplot2
Testing sampleProject
1
1. Failure (at test-myFunction.R#2): my first test -----------------------------
myFunction(1:10, rep(1:2, each = 5)) not equal to data.frame(group = c(1, 2), avg = c(3, 8))
Incompatible type for column group: x integer, y numeric
- Lets fix our test according to the error message (by providing the _group_ variable as an _integer_ data type):
r
test_that('my first test', {
expect_equal(
myFunction(1:10, rep(1:2, each = 5)),
data.frame(group = c(1L, 2L), avg = c(3, 8))
)
})
```
Besides tests provided by you R can perform his own tests on your package code.
Among them there are as useful ones as checking for undefined variables and functions (a curse of R developers).
As these are more advanced topics and your package will still work even R package tests rise some NOTEs or even WARNINGs on it you can skip this section if you find it to difficult (but if you want to put your package in CRAN at some point you will have master them anyway).
r
#' Computes means by groups
#' @description
#' You can put extended description here
#' @details
#' If your function is complicated, you may consider adding a details section
#' @param values numeric vector of values
#' @param groups vector of groups
#' @return data.frame with groups names in first column and average values per group in the second one
#' @export
#' @import dplyr
myFunction = function(values, groups){
result = data.frame(value = values, group = groups) %>%
group_by_('group') %>%
summarize_(avg = ~mean(value, na.rm = TRUE))
return(result)
}
At the moment we should have an R project configured to work with Travis and Coveralls with a sample function and tests.
No lets try to see a continues integration at work.
r
devtools::install_github('yourLogin/yourRepository')
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.