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
Workflow with RStudio Desktop and RStudio Cloud
Here’s a step-by-step workflow for using renv and RStudio Projects, with guidance for both new and returning sessions:
1. Should I Create a New RStudio Project?
Yes, you should!
For every analytical project (especially when using renv), it’s best to create an RStudio Project (
.Rprojfile) in your repo folder.How to create:
.Rprojfile in your directory.2. Using renv in Your Project
Installation
To install the latest version of renv, run these in R console (not terminal):
install.packages("renv")Alternatively, you can install the development version from r-universe:
Workflow
Initialize renv in a new or existing project using
renv::init()via R console and not the terminal. This will set up a project library and create arenv.lockfile to record the packages and their versions.As you work on the project, install and upgrade packages using either
install.packages()andupdate.packages()or the renv-specific functionsrenv::install()andrenv::update().After confirming your code works as expected, use
renv::snapshot()to record the current package versions in therenv.lockfile.When sharing your code or running it on a new machine, your collaborator (or you) can use
renv::restore()to reinstall the specific package versions recorded in therenv.lockfile, ensuring a reproducible environment..Rproj), runrenv::init()if you haven't already done so.renv::snapshot()to updaterenv.lock.3. Daily Workflow
a) Starting a New Session (Next Day or Later)
In RStudio Desktop:
.Rprojfile (File > Open Project or double-click the.Rprojfile).git pull), if collaborating.renv::init()or manually activate anything.renv.lockfile, runrenv::restore()to ensure your packages match.In RStudio Cloud:
renv::restore()if therenv.lockfile was updated.b) Ending Your Session
git add,git commit,git push) if using Git.4. Do You Need to Create a New .Rprofile File?
.Rprofilefile for basic use. renv and RStudio Projects handle environment setup for you..Rprofile, but it’s not required for renv/project workflow.5. Summary Workflow
First time:
renv::init()(if not already done).renv::snapshot().Rproj,renv.lock, scripts) to GitHub.Every other time:
.Rprojfile.git pull(get updates).renv::restore()(if needed).Example Terminal Session
R Project Visual Workflow with RStudio, renv, and GitHub
Step-by-Step Summary
.Rprojif missing).renv::init()to start package management.renv::snapshot()to saverenv.lock.git pullto get latest code.renv.lockchanged, runrenv::restore()to sync your environment..Rprojfile.renv.lockchanges.