How to Get Organized with RStudio Projects and Git
Use RStudio Projects and Git to organize your data science projects
Would you like to better organize your data science projects following a simple and consistant workflow?
In my new YouTube video, I show you how to use RStudio Projects and Git version control to follow a consistant file structure and track all the changes in your code.
You will learn how to:
use RStudio projects and connect them to Git and GitHub (the easy way);
use RStudio Git panel view to commit, push and pull files with 1 click;
see an example of a data science project file structure.
Watch the video now:
Data science projects can quickly become chaotic without proper organization and version control. In this tutorial, you’ll learn how to use RStudio Projects and Git to maintain clean, organized, and portable data science workflows.
The Problem: Disorganized Project Folders
Many data scientists end up with folders that look like this:
analysis.Rnew-analysis.Rnew-analysis-version2.Rideas-from-document.RMixed files from different projects in the same directory
This disorganized approach leads to confusion, lost work, and difficulty collaborating with others. Let’s fix this using RStudio Projects and Git version control.
Creating Your First RStudio Project
Step 1: Start a New Project
Click on the New Project button in the top-right corner of RStudio
You’ll see three options:
New Directory: Creates a new folder
Existing Directory: Uses an existing folder
Version Control: Connects to Git (we’ll cover this later)
Step 2: Choose Project Type
Select New Directory, then choose from various project templates: - Empty new project (recommended for beginners) - R package - Shiny application - Quarto projects - And other specialized templates
Step 3: Configure Your Project
Directory name: This becomes your project folder name (e.g., “organized”)
Subdirectory: Choose where to save your project
Pro tip: Create a dedicated folder for all your data science projects
Consider syncing with cloud storage (OneDrive, Google Drive, etc.)
Optional settings:
Git repository (we’ll set this up separately)
renvfor package management (recommended for advanced users)
Click Create Project to finish setup.
Understanding Project Benefits
Automatic Working Directory
RStudio Projects automatically set your working directory to the project root. This means:
# Instead of complex file paths like this:
read_csv("/Users/username/long/path/to/project/data/data.csv")
# You can use simple relative paths:
read_csv("data/data.csv")This makes your projects portable – you can move the entire folder anywhere and the code still works.
Project File Structure
The .Rproj file in your project folder tells RStudio this is an RStudio Project. Benefits include:
Easy project switching via the top-right dropdown menu
Automatic working directory management
Project-specific settings and history
Better organization across multiple projects
Recommended Folder Structure
For consistency across projects, follow R package conventions:
your-project/
├── data/ # Raw and processed data
├── R/ # R scripts and functions
├── docs/ # Documentation
├── outputs/ # Results, plots, reports
├── README.md # Project description
└── your-project.RprojConnecting Git to RStudio Projects
Why Use Git?
Git helps you:
Track changes to your code over time
Collaborate with others
Back up your work online
Revert to previous versions when needed
Method 1: Add Git to Existing Project
If you already have an RStudio Project, use the usethis package:
library(usethis)
use_git()
use_github(private = TRUE) # Set private = TRUE for private reposImportant: The private argument defaults to FALSE, making your repository public!
Method 2: Create Project with Git (Recommended)
This approach avoids common Git setup issues:
Create GitHub repository first:
Go to GitHub.com and create a new repository
Add a description
Choose public or private
Copy the HTTPS URL
Clone in RStudio:
Choose New Project > Version Control > Git
Paste the repository URL
RStudio will automatically name the project after your repository
Choose your subdirectory location
Working with Git in RStudio
The Git Panel
Once connected, you’ll see a Git tab in RStudio showing: - Modified files - New files - Files ready to commit
Using .gitignore
The .gitignore file tells Git which files to ignore. Common entries:
# Credentials and sensitive data
credentials.R
*.env
# Large data files
data/large-dataset.csv
# Temporary files
.Rhistory
.RDataThe Git Workflow
Stage files: Check the boxes next to files you want to commit
Commit: Click “Commit” and write a descriptive message
Push: Upload your changes to GitHub
Viewing Changes
RStudio’s Git integration shows you: - Which files have been modified - Exactly what changed in each file (diff view) - When you right-click a file and select “Diff selected files”
This helps you: - Spot unintended changes or typos - Review your work before committing - Understand what’s changed since your last commit
Best Practices Summary
Always use RStudio Projects for better organization and portability
Connect Git early in your project lifecycle
Use consistent folder structures across projects
Write meaningful commit messages to track your progress
Regularly commit and push your work
Review diffs before committing to catch errors
See you in another tutorial!

