🏠 R Session Management: Your Digital Workspace
Imagine walking into your personal art studio every day. You have your brushes, your paints, your half-finished paintings, and notes about what you were working on. R’s session management is exactly like that—your personal workspace that remembers everything!
🎯 What You’ll Learn
Think of R as your personal assistant that keeps track of:
- Who you are (Session Information)
- What computer you’re using (System Information)
- Where your stuff is stored (Workspace Management)
- How to run your saved instructions (source Function)
📋 Session Information
What is Session Information?
Imagine you’re writing a diary entry. You’d write the date, maybe the weather, and what you did. R does the same thing—it keeps a “diary” about your current work session!
The Magic Command: sessionInfo()
sessionInfo()
This one command tells you EVERYTHING about your current R session:
# What you'll see:
# R version 4.3.2 (2023-10-31)
# Platform: x86_64-pc-linux-gnu
# Running under: Ubuntu 22.04.3 LTS
#
# Matrix products: default
# BLAS: /usr/lib/...
#
# locale: en_US.UTF-8
#
# attached packages:
# [1] ggplot2_3.4.4
Why Does This Matter?
🎨 Story Time:
Sarah wrote an amazing R script at work. She sent it to her friend Tom, but it didn’t work on his computer! Why? Because Tom had a different R version. If Sarah had shared her sessionInfo(), Tom would have known exactly what he needed!
Quick Session Checks
# What R version am I using?
R.version.string
# "R version 4.3.2 (2023-10-31)"
# What packages are loaded?
search()
# ".GlobalEnv" "package:stats" ...
# When did this session start?
Sys.time()
💻 System Information
Your Computer’s ID Card
Just like you have an ID card with your name and photo, your computer has one too! R can read it.
Key System Commands
# What's my computer's name?
Sys.info()["nodename"]
# What operating system?
Sys.info()["sysname"] # "Linux", "Windows", "Darwin"
# Who's logged in?
Sys.info()["user"]
The Full Picture
# Get EVERYTHING about your system
Sys.info()
This returns:
sysname→ Operating System (Windows, Linux, Darwin)release→ OS versionnodename→ Computer namemachine→ Processor typelogin→ Your usernameuser→ Current user
🧭 Practical Example
# Make your code work on ANY computer!
if (Sys.info()["sysname"] == "Windows") {
path <- "C:/Users/data/"
} else {
path <- "/home/user/data/"
}
📁 Workspace Management
Your Digital Desk
Think of your workspace as your desk. On it, you have:
- Papers (data frames)
- Calculators (functions)
- Notes (variables)
R keeps all of this in its “Global Environment.”
Where Am I?
# What folder is R looking at right now?
getwd()
# "/home/student/my_project"
# Change to a different folder
setwd("/home/student/another_project")
What’s On My Desk?
# See everything in your workspace
ls()
# "my_data" "my_function" "result"
# See with more details
ls.str()
Cleaning Up
# Remove one thing
rm(old_variable)
# Remove multiple things
rm(var1, var2, var3)
# CLEAR EVERYTHING (be careful!)
rm(list = ls())
💾 Saving Your Work
# Save EVERYTHING to a file
save.image("my_work.RData")
# Save specific objects
save(my_data, my_model, file = "important.RData")
# Load it back later
load("my_work.RData")
Workspace Files Explained
graph TD A["Your R Session"] --> B[".RData File"] A --> C[".Rhistory File"] B --> D["All Variables"] B --> E["All Functions"] B --> F["All Data"] C --> G["Commands You Typed"]
| File | What It Stores |
|---|---|
.RData |
All your objects (data, variables) |
.Rhistory |
Every command you typed |
📜 The source() Function
Running Saved Instructions
Imagine you write the same recipe every time you bake cookies. Wouldn’t it be easier to just say “follow the cookie recipe”? That’s what source() does!
How It Works
Step 1: Save your code in a file (like analysis.R)
# analysis.R
data <- read.csv("sales.csv")
summary(data)
plot(data$month, data$sales)
Step 2: Run it all with one command!
source("analysis.R")
✨ Magic! All the code runs automatically.
source() Options
# Basic usage
source("my_script.R")
# Show each line as it runs
source("my_script.R", echo = TRUE)
# Keep running even if there's an error
source("my_script.R", keep.source = TRUE)
# Run from the internet!
source("https://example.com/script.R")
🎭 Real-World Story
Monday Morning at DataCorp:
Alex arrives at work and opens R. Instead of typing 50 lines of code to load data and create reports, Alex just types:
source("monday_report.R")
☕ Coffee time while R does all the work!
Building Good Habits
graph TD A["Write Code in RStudio"] --> B["Save as .R File"] B --> C["Test It Works"] C --> D["Use source Next Time"] D --> E["Save Hours of Work!"]
🔧 Putting It All Together
A Day in the Life of an R User
# 1. Check your session
sessionInfo()
# 2. See where you are
getwd()
# 3. Load yesterday's work
load("project.RData")
# 4. See what you have
ls()
# 5. Run your analysis script
source("daily_analysis.R")
# 6. Save when done
save.image("project.RData")
🌟 Quick Reference
| Task | Command |
|---|---|
| R version | R.version.string |
| Full session info | sessionInfo() |
| Computer info | Sys.info() |
| Current folder | getwd() |
| Change folder | setwd("path") |
| List objects | ls() |
| Remove object | rm(name) |
| Clear all | rm(list = ls()) |
| Save workspace | save.image("file.RData") |
| Load workspace | load("file.RData") |
| Run script | source("script.R") |
💡 Pro Tips
-
Always check
sessionInfo()before sharing code — it helps others reproduce your work! -
Use
setwd()at the start of scripts — so R knows where to find your files. -
Save your workspace regularly — computers crash, but
.RDatafiles don’t forget! -
Put reusable code in
.Rfiles — thensource()them whenever you need. -
Name your objects clearly — when you run
ls(), you’ll thank yourself!
🎉 You Did It!
Now you know how to:
- ✅ Check your session details
- ✅ Understand your system
- ✅ Manage your workspace like a pro
- ✅ Run saved scripts effortlessly
Your R workspace is now your organized, efficient, digital studio. Happy coding! 🚀
