# create renv and lockfile in root folder
renv::init()
# install packages
install.packages(c("tidyverse", "here", "renv", "usethis", "devtools"))
# snapshot to save in lockfile for reproducibility and collaboration
renv::snapshot()How I Set Up Projects
Project Management
Workflow
Organisation
Notes on keeping everything organised with a clear work flow and file organisation.
Structure
- create project root folder
- open in software
project/
├── R/
├── data/
│ ├── raw/ # source data (never edited)
│ ├── versions/ # timestamped snapshots
│ ├── processed/ # cleaned / merged
│ └── final/ # datasets used for analysis / sharing
├── outputs/ # figures, tables, model objects
├── docs/ # protocols, codebooks, notes
├── renv.lock
notes: - root folder always treated as root - no setwd() - all paths relative - never change raw data
Environment
initialise on first open:
notes: - renv.lockfile tracks package versions - collaborators use renv::restore() to get same environment
Paths
- anchor scripts using
herepackage - avoid using absolute paths
# shows where file is saved relative to root
here::i_am("R/01_data_processing.R")
# import data using relative paths
readr::read_csv(here::here("data/raw/survey_data_raw.csv"))Secrets
- use
usethisto create.Renvironfile in root folder and add secrets in there - for example: API keys, database credentials etc.
- github will ignore this file so its not shared with collaborators
usethis::edit_r_environ()- after running, edit
.Renvironfile - format specifically:
API_KEY="your_api_key_here"
DB_USERNAME="your_db_username_here"
DB_PASSWORD="your_db_password_here"
after saving the .Renviron file, access the secrets in your R code:
api_key <- Sys.getenv("API_KEY")
db_username <- Sys.getenv("DB_USERNAME")
db_password <- Sys.getenv("DB_PASSWORD")