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)
}

Prerequisites

Software

R packages

install.packages(c('devtools', 'testthat', 'covr'))

Accounts

Create a project

Create new GitHub repository

Log into GitHub and create a new repository (click the plus sign in the top-right corner and choose New repository...)

Create and configure RStudio project

Create RStudio project from the GitHub repository

Configure your RStudio project

Initialize an R package

Set up testing and continues integration

Commit your changes

Create a simple function

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 a file with function source code

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:

Provide documentation

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:

Check if function works

Create a simple test

User-defined tests

We will use the testthat library to prepare tests.

R package tests

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).

See how it works

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.



ViennaR/startupPackage documentation built on May 9, 2019, 9:56 p.m.