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
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 (
.Rproj
file) in your repo folder.How to create:
.Rproj
file 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.lock
file 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.lock
file.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.lock
file, 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:
.Rproj
file (File > Open Project or double-click the.Rproj
file).git pull
), if collaborating.renv::init()
or manually activate anything.renv.lock
file, runrenv::restore()
to ensure your packages match.In RStudio Cloud:
renv::restore()
if therenv.lock
file 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?
.Rprofile
file 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:
.Rproj
file.git pull
(get updates).renv::restore()
(if needed).Example Terminal Session
R Project Visual Workflow with RStudio, renv, and GitHub
Step-by-Step Summary
.Rproj
if missing).renv::init()
to start package management.renv::snapshot()
to saverenv.lock
.git pull
to get latest code.renv.lock
changed, runrenv::restore()
to sync your environment..Rproj
file.renv.lock
changes.