knitr::opts_chunk$set(
  echo = TRUE,
  eval = FALSE,
  fig.dim = c(10, 7) #width, height
  )
library(devtools)
setwd(dirname(getwd()))

Set up a package compendium

What do you need ?

Resources

Architecture

Prepare your template directory

mkdir template 
cd template
git init
git config user.name "FirstName LastName"
git config user.email "MY_NAME@example.com"

General set up

library(devtools)

options(devtools.name = "Alain Danet", 
    devtools.desc.author = "person('Alain', 'Danet',
    email='alain.danet@mnhn.fr', role = c('aut', 'cre'))",
devtools.desc.license = "file LICENSE")

setup() #See also create()

What are the files created ?

  • DESCRIPTION:
  • NAMESPACE:
  • R/:

Description

```{bash, eval = TRUE} head ../DESCRIPTION

- [Package deSolve](https://cran.r-project.org/web/packages/deSolve/index.html)

## NAMESPACE

```{bash, eval = TRUE}
head ../NAMESPACE

Not important for us

Licence

use_gpl3_license()

A README

use_readme_rmd()

OR

use_readme_md()

R

It is the place where your functions live! But not your analysis worflow

# Good
fit_models.R
utility_functions.R

# Bad
foo.r
stuff.r

Object names

  • Why is this naming scheme bad ?
first_day_of_the_month
DayOne
dayone
djm1

Test and document your functions

use_package_doc()
use_testthat()

Report your analysis

use_vignette("intro")

So far

You have a versionned repository containing:

  • DESCRIPTION and README to describe your work
  • R/ to host your functions
  • man/ to document your functions
  • tests/ to test your functions
  • vignettes/ to describe your results
  • A LICENCE for your work

Let's commit your changes

git status
git add --all 
git commit -m "setup of a research template"

Let's play a bit

Change of branch

git branch testing 
git checkout testing 

First function

library(ggplot2)
make_planet <- function() {

  ggplot(data.frame(x = 0, y = 0), aes(x, y)) + 
    geom_point(size = 100, color = "red")
}

In your terminal:

load_all()

make_planet()

Second function

make_planet_great_again <- function() {

  ggplot(data.frame(x = 0, y = 0), aes(x, y)) + 
    geom_point(size = 100, color = "green")
}

In your terminal:

load_all()

make_planet_great_again()

DRY principle

Do not Repeat Yourself

make_color_planet <- function(color = "red") {

  ggplot2::ggplot(data.frame(x = 0, y = 0), aes(x, y)) + 
    ggplot2::geom_point(size = 100, color = color)
}

Data

data(iris)
head(iris)

Where do you place data ?

myiris <- iris 
use_data(myiris)
load_all()
data(myiris)

Raw data

use_data_raw()

Test

Write a test

Whenever you are tempted to type something into a print statement or a debugger expression, write it as a test instead. — Martin Fowler

test_that("squaring works", {
  expect_equal(square_it(2), 4)
})

Write the function

square_it <- function(x) {
    square <- x * x
    return(square)
}
  • Now test with: test()

Document your functions

#' Square a number.
#'
#' @param x A number.
#' @return \code{x} squared. 
#' @examples
#' square_it(2)
#' square_it(4)
square_it <- function(x) {
    square <- x * x
    return(square)
}

Run document()

Vignettes

Goal

  • Describe your analysis
  • Generate your figures
  • Generate your tables

It is a very convenient way to report your results and write draft

Exemple

Go to vignette example: vignettes/intro.Rmd

Docker

Build

You can build the image from the Dockerfile

docker build -t my-r-image .


alaindanet/reproducibleR_workshop documentation built on May 24, 2019, 7:33 a.m.