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/directoryExample:
rstudio --cwd /c/workspace/My_Projects/alarm-projectsThis ensures RStudio starts in the specified directory.
Update: better to use Rproj since we uses relative dir instead of specific path.
- 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
.Rprojfile—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/projectfor 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
VS Code Settings for R
To implement the specified R terminal options in VS Code settings, follow these steps:
Ctrl + Shift + P(Windows/Linux) orCmd + Shift + P(Mac) to open the Command Palette. Type "Open User Settings (JSON)" and select the option.settings.jsonfile, add the following code:{ "r.rterm.option": [ "--r-binary=/c/Program Files/R/R-4.5.1/bin/R", // Replace with your R path as set PATH "--no-save", // Optional: Prevent saving the workspace at the end of the session "--no-restore", // Optional: Prevent restoring workspaces ], "r.rterm.windows": "C:\\Program Files\\R\\R-4.5.1\\bin\\R.exe", // Replace with your R path "r.rterm.linux": "/usr/bin/R", // Replace with your R path on Linux "r.rterm.mac": "/usr/local/bin/R" // Replace with your R path on macOS }settings.jsonfile to apply the changes.Note: Make sure to replace
"/c/Program Files/R/R-4.5.1/bin/R"with the actual path to your R executable as set in your system's PATH environment variable.By adding these options, you can control the behavior of the R terminal in VS Code, such as preventing workspace saving and restoring.
Quick R Test Run in VS Code