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
📚 R & RStudio Cheatsheet for Data Analysts & Data Engineers
A compact reference for R, RStudio Desktop, and RStudio Cloud—including working directory, data wrangling, visualization, and key workflows for analytics and engineering.
1️⃣ RStudio & R: Working Directory
Menu:
Session
→Set Working Directory
→Choose Directory...
Shortcut: Ctrl+Shift+H
File
→New Project
→Existing Directory
2️⃣ Data Import & Export
3️⃣ Data Wrangling (dplyr & tidyr)
df %>% select(col1, col2)
df %>% filter(col1 == "value")
df %>% mutate(new_col = col1 * 2)
df %>% summarise(mean_col = mean(col1, na.rm=TRUE))
df %>% group_by(col2) %>% summarise(count = n())
left_join(df1, df2, by = "id")
pivot_longer(df, cols = c(colA, colB), names_to = "key", values_to = "value")
4️⃣ Data Cleaning
df <- na.omit(df)
df$col[df$col == "old"] <- "new"
as.numeric()
,as.character()
,as.Date()
library(stringr)
str_detect(df$col, "pattern")
str_replace(df$col, "old", "new")
5️⃣ Data Visualization (ggplot2)
6️⃣ Programming (Base R)
apply(mat, 1, sum)
lapply(list, mean)
7️⃣ Data Table (High Performance)
8️⃣ RStudio Cloud Tips
/cloud/project
getwd()
,setwd()
install.packages("pkg")
9️⃣ Exporting Results
write.csv(df, "mydata.csv")
writexl::write_xlsx(df, "mydata.xlsx")
dbWriteTable(con, "tablename", df)
🔟 Reproducibility Best Practices
set.seed(123)
.Rmd
)sessionInfo()
Useful Links
Last updated: 2025-06-26