knitr::opts_chunk$set( echo = TRUE, eval = FALSE, fig.dim = c(10, 7) #width, height ) library(devtools) setwd(dirname(getwd()))
mkdir template cd template git init git config user.name "FirstName LastName" git config user.email "MY_NAME@example.com"
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/
:
```{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
use_gpl3_license()
use_readme_rmd()
knit
!OR
use_readme_md()
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
- Why is this naming scheme bad ?
first_day_of_the_month DayOne dayone djm1
- Better
day_one
orday_one
- Be consistent
- Ex: name for object and variables and verbs for functions
- See more in the R packages book
use_package_doc() use_testthat()
use_vignette("intro")
You have a versionned repository containing:
DESCRIPTION
andREADME
to describe your workR/
to host your functionsman/
to document your functionstests/
to test your functionsvignettes/
to describe your results- A
LICENCE
for your work
git status git add --all git commit -m "setup of a research template"
git branch testing git checkout testing
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()
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()
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(iris) head(iris)
Where do you place data ?
myiris <- iris use_data(myiris) load_all() data(myiris)
use_data_raw()
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) })
R/
folder:square_it <- function(x) { square <- x * x return(square) }
- Now test with:
test()
#' 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()
- Describe your analysis
- Generate your figures
- Generate your tables
It is a very convenient way to report your results and write draft
Go to vignette example: vignettes/intro.Rmd
You can build the image from the Dockerfile
docker build -t my-r-image .
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.