A comprehensive cookiecutter template for creating reproducible R projects with Docker, renv, and modern development tools.
- Docker Environment: Uses
rocker/verse
for comprehensive R development - Package Management: Integrated
renv
for reproducible package environments - Development Tools:
- VS Code dev containers support
- RStudio Server integration
- Comprehensive Makefile for common tasks
- Code Quality: Built-in linting, formatting, and testing
- Reproducibility: Docker containers with persistent volumes
- Flexibility: Customizable project structure and dependencies
- Cookiecutter (
pip install cookiecutter
) - Docker and Docker Compose
- Make (optional, for convenient commands)
# Using cookiecutter directly
cookiecutter https://github.com/IsaiasGutierrezCruz/r-flow
The template will prompt you for the following variables:
Variable | Description | Default | Example |
---|---|---|---|
project_name |
Human-readable project name | my-r-project |
Advanced Data Analysis |
project_slug |
Project directory name | Auto-generated | advanced-data-analysis |
project_description |
Brief project description | A reproducible R project... |
Statistical analysis of customer data |
author_name |
Your name | Your Name |
John Doe |
author_email |
Your email | your.email@example.com |
john.doe@company.com |
r_version |
R version for Docker | 4.3.2 |
4.3.2 |
use_git |
Initialize git repo | y |
y or n |
use_github |
Set up GitHub remote | y |
y or n |
github_username |
GitHub username | yourusername |
johndoe |
license |
License type | MIT |
MIT , GPL-3 , Apache-2.0 , BSD-3-Clause , None |
include_shiny |
Include Shiny packages | y |
y or n |
include_rmarkdown |
Include R Markdown packages | y |
y or n |
include_plotly |
Include Plotly packages | y |
y or n |
include_tidyverse |
Include Tidyverse packages | y |
y or n |
include_devtools |
Include development tools | y |
y or n |
include_testthat |
Include testing framework | y |
y or n |
include_lintr |
Include code quality tools | y |
y or n |
rstudio_server_password |
RStudio Server password | password123 |
mysecretpassword |
timezone |
Container timezone | UTC |
America/New_York |
your-project/
βββ .devcontainer/ # VS Code dev container configuration
β βββ devcontainer.json
βββ data/ # Data files (raw, processed, etc.)
β βββ raw/
β βββ processed/
β βββ interim/
β βββ external/
βββ docs/ # Documentation and reports
β βββ example_report.Rmd
βββ output/ # Generated outputs
β βββ figures/
β βββ tables/
β βββ models/
β βββ reports/
βββ scripts/ # R scripts
β βββ example_analysis.R
βββ tests/ # Unit tests
β βββ test_example.R
βββ docker-compose.yml # Docker services configuration
βββ Dockerfile # Container image definition
βββ Makefile # Development commands
βββ .Rprofile # R session configuration
βββ .Renviron # R environment variables
βββ .gitignore # Git ignore patterns
βββ renv_init.R # renv initialization script
βββ renv_restore.R # renv restoring dependencies script
βββ README.md # Project documentation
βββ LICENSE # License file (if specified)
# Navigate to your project
cd your-project
# Build the Docker environment
make build # then Open your browser to http://localhost:8787
# Start the development environment
make up
# Access R console
make r-console
# Open shell in container
make shell
# Initialize renv (first time only)
make renv-init
# Snapshot current packages
make renv-snapshot
# Restore packages from lockfile
make renv-restore
# Check renv status
make renv-status
# Run R script
make r-script SCRIPT=scripts/analysis.R
# Render R Markdown
make render-rmd FILE=docs/report.Rmd
# Run Shiny app
make shiny-app APP=app.R
# Check code quality
make lint
# Run tests
make test
The template uses Docker for reproducible environments with:
rocker/verse:R_VERSION
- Comprehensive R environment with RStudio Server
renv_cache
- R package cache for faster installationsrstudio_config
- RStudio Server configurationr_history
- R command history
8787
- RStudio Server3838
- Shiny applications
- Configurable through
.Renviron
file - Docker-specific settings for optimal performance
The template includes complete VS Code dev container configuration:
- R language support with syntax highlighting
- Integrated debugging and testing
- Git integration
- Docker support
- Automatic port forwarding
- Comprehensive extension pack
- Install "Remote - Containers" extension
- Open project in VS Code
- Click "Reopen in Container"
- Wait for container to build and start
The template integrates renv
for reproducible package management:
- Isolated package libraries per project
- Lockfile for exact package versions
- Shared cache for faster installations
- Automatic restoration in Docker
- Container starts with base packages
renv_init.R
initializes renv if no lockfile exists- Installs packages based on template selections
- Creates initial lockfile
The template includes comprehensive testing setup:
- Unit tests in
tests/
directory - Example tests for common functions
# Run all tests
make test
# Generate coverage report
make test-coverage
# Lint code
make lint
# Format code
make style
The template includes a comprehensive Makefile with 40+ commands:
make build
- Build Docker containermake up
- Start development environmentmake down
- Stop containersmake logs
- View container logs
make r-console
- Open R consolemake rstudio
- Open RStudio Servermake shell
- Open container shell
make renv-init
- Initialize renvmake renv-snapshot
- Update lockfilemake renv-restore
- Restore packages
make lint
- Run code lintingmake style
- Format codemake test
- Run unit tests
make r-script SCRIPT=file.R
- Run R scriptmake render-rmd FILE=report.Rmd
- Render R Markdownmake shiny-app APP=app.R
- Run Shiny appmake run-rmd-shiny-file FILE=report.Rmd
- Run shiny app based on a rmd filemake run-rmd-shiny-file-pre-rendered FILE=report.Rmd
- Run shiny app with a pre-rendered rmd file.
- Modify
renv_init.R
to include additional packages - Update Dockerfile if system dependencies are needed
- Add to cookiecutter.json if it should be optional
- Edit
Dockerfile
for system-level changes - Update
docker-compose.yml
for service configuration - Modify
.devcontainer/devcontainer.json
for VS Code settings
- Add new targets to
Makefile
- Follow existing patterns for consistency
- Include help text with
##
comments
- Keep raw data immutable in
data/raw/
- Use
here::here()
for file paths - Document data sources and transformations
- Use meaningful file and variable names
- Run
make lint
before committing - Write tests for critical functions
- Use consistent coding style
- Document complex algorithms
- Always commit
renv.lock
file - Use Docker for consistent environments
- Document system dependencies
- Include session information in reports
- Use meaningful commit messages
- Keep commits focused and atomic
- Use branches for feature development
- Tag releases with version numbers
- Check if Docker daemon is running
- Verify ports 8787 and 3838 are available
- Run
make logs
to check for errors
- Check system dependencies in Dockerfile
- Clear renv cache with
make clean-renv
- Rebuild container with
make build
- Verify container is running:
docker ps
- Check port forwarding:
make logs
- Try different browser or incognito mode
- Use
make disk-usage
to check storage - Consider using faster storage for Docker
- Check project README.md
- Run
make help
for available commands - Check container logs with
make logs
- Review Docker and renv documentation
To contribute to this template:
- Fork the repository
- Create a feature branch
- Make your changes
- Test with different configurations
- Submit a pull request
- Test template generation with various options
- Ensure all generated files are valid
- Update documentation for new features
- Add examples for complex features
This template is licensed under the MIT License. Projects generated from this template can use any license specified during generation.
This template is built on the shoulders of giants:
- Cookiecutter - Template engine
- Rocker Project - Docker images for R
- renv - Package management
- RStudio - IDE and tools
- tidyverse - Data science packages
Happy coding! π