This cheatsheet covers how to control and troubleshoot the working directory in R, RStudio Desktop, and RStudio Cloud. A correct working directory makes data import, script sourcing, and project management much smoother.
Instead of just:
rstudio .
Use:
rstudio --cwd /path/to/your/directory
Example:
rstudio --cwd /c/workspace/My_Projects/alarm-projects
This ensures RStudio starts in the specified directory.
- Menu:
Session
→Set Working Directory
→Choose Directory...
- Shortcut: Ctrl + Shift + H
- R Console Command:
setwd("C:/workspace/My_Projects/alarm-projects")
- Go to
Tools
→Global Options
→General
- Under Default working directory, set your path (e.g.,
C:/workspace/My_Projects/alarm-projects
) - Click Apply and restart RStudio
RStudio Projects automatically set the working directory to the project folder.
File
→New Project
→Existing Directory
- Select your folder (e.g.,
C:/workspace/My_Projects/alarm-projects
) - RStudio creates a
.Rproj
file—always open this file to launch the project with the right directory!
- RStudio Cloud always starts in the project’s root directory.
- For reproducibility, always use RStudio Projects in the cloud too.
- To check your current directory:
getwd()
- To change it:
setwd("/cloud/project/subfolder")
- Upload files to
/cloud/project
for easy access.
- Check current directory:
getwd()
- Set working directory:
setwd("/path/to/your/directory")
- Paths on Windows: use either
/
or double backslashes\\
(never single\
). - Always check your current directory with
getwd()
if file loading fails. - Use Projects whenever possible—they save a ton of headaches!
Pro Tip:
Always use RStudio Projects for each analysis or codebase. They save window layouts, history, and—most importantly—set your working directory automatically!
Last updated: 2025-06-26
Setting Unrestricted Working Directory for RStudio Desktop
Yes, Windows protects your user folder (
C:\Users\...
) with special permissions, which can cause issues for R, RStudio,renv
, and sometimesgit
.Cloning your repos to a simpler location like
C:\workspace
is a great idea.Here’s how to set that up as your default location for cloning via RStudio (and some tips for git in general):
1. Set Default Working Directory in RStudio
RStudio does not have a global “default repo location” for cloning, but you can specify the folder every time you clone:
da-with-r
).C:\workspace
as the location.2. For Command-Line git
If you use git bash or command prompt for cloning, just:
3. Optional: Make a Shortcut for Quick Navigation
C:\workspace
on your Desktop for easy access.4. (Advanced) Change Default Folder for New RStudio Projects
You can also set the default working directory for new projects in RStudio:
C:/workspace
5. Move Existing Project
If you want to move your current project:
da-with-r
folder fromC:\Users\AbdulHafeez
toC:\workspace
.da-with-r.Rproj
inC:\workspace\da-with-r
to reopen the project.renv::activate()
and everything should still work.Summary:
Whenever you clone a repo in RStudio, just pick
C:\workspace
as the location. No need to set a global default—just always choose this path in the dialog.