template
template copied to clipboard
A template for data analysis projects structured as R packages (or not)
template package
Generic template for data analysis projects structured as R packages
The template package automates creation of new projects with all the
necessary scaffolding: different folders for data, scripts, and
functions, plus (optionally) additional files required for an R package
structure. It can simultaneously create and synchronise a new repository
on GitHub so one can start working immediately.
template can create both projects with or without R package structure.
Structuring data analysis projects as R packages (a.k.a. “research
compendia”) can bring some advantages (e.g. see this
blogpost, this
repo,
these and these
slides or read
Marwick et al.). But
there are also good
reasons why an R package
structure may not always be needed or convenient.
Installation
# install.packages("remotes")
remotes::install_github("Pakillo/template")
Usage
First, load the package:
library("template")
Now run the function new_project to create a directory with all the
scaffolding (slightly modified from R package structure). For example,
to start a new project about tree growth, just use:
new_project("treegrowth")
This will create a new Rstudio project with this structure:
You can create a GitHub repository for the project at the same time:
new_project("treegrowth", github = TRUE, private.repo = FALSE)
You could choose either public or private repository. Note that to create a GitHub repo you will need to have configured your system as explained in https://usethis.r-lib.org/articles/articles/usethis-setup.html.
There are other options you could choose, like setting up testthat or
continuous integration (Travis-CI, GitHub Actions…). Or skip R package
structure altogether. See ?new_project for all options.
Developing the project
-
Now edit
README.Rmdand theDESCRIPTIONfile with some basic information about your project: title, brief description, licence, package dependencies, etc. -
Place original (raw) data in
data-rawfolder. Save all R scripts (or Rmarkdown documents) used for data preparation in the same folder. -
Save final (clean, tidy) datasets in the
datafolder. You may write documentation for these data. -
R scripts or Rmarkdown documents used for data analyses may be placed at the
analysesfolder. The final manuscript/report may be placed at themanuscriptfolder. You could use one of the many Rmarkdown templates available out there (e.g. rticles, rrtools or rmdTemplates). -
If you write custom functions, place them in the
Rfolder. Document all your functions withRoxygen. Write tests for your functions and place them in thetestsfolder. -
If your analysis uses functions from other CRAN packages, include these as dependencies (
Imports) in theDESCRIPTIONfile (e.g. usingusethis::use_package()orrrtools::add_dependencies_to_description(). Also, useRoxygen@importor@importFromin your function definitions, or alternativelypackage::function(), to import these dependencies in the namespace. -
I recommend using an advanced tool like
targetsto manage your project workflow. A simpler alternative might be writing amakefileor master script to organise and execute all parts of the analysis. A template makefile is included with this package (usemakefile = TRUEwhen callingnew_project). -
Render Rmarkdown reports using
rmarkdown::render, and use RstudioBuildmenu to create/update documentation, run tests, build package, etc. -
Record the exact dependencies of your project. One option is simply running
sessionInfo()but many more sophisticated alternatives exist. For example,automagic::make_deps_file()orrenv::snapshot()will create a file recording the exact versions of all packages used, which can be used to recreate such environment in the future or in another computer. If you want to use Docker, you could use e.g.containerit::dockerfile()orrrtools::use_dockerfile(). -
Archive your repository (e.g. in Zenodo), get a DOI, and include citation information in your README.
Thanks to:
- Carl Boettiger and his template package
- Jeff Hollister and his manuscriptPackage
- Robert Flight: http://rmflight.github.io/posts/2014/07/analyses_as_packages.html
- Hadley Wickham: http://r-pkgs.had.co.nz/
- Yihui Xie: http://yihui.name/knitr/
- Rstudio