🚀 R Get Started – Your First Steps with R Programming and Scripts
🧲 Introduction – How to Start with R?
Once you’ve installed R and RStudio, you’re ready to start coding. This guide will walk you through how to write your first R commands, understand the R console, and create your first R script.
We’ll also cover how to run code line-by-line, save your work, and explore the working directory—all crucial skills for effective R programming.
🎯 In this guide, you’ll learn:
- How to run commands in the R console
- How to create and save R scripts
- The basics of working directories
- Writing your first R program
💬 R Console vs Script Editor – What’s the Difference?
Feature | R Console | R Script Editor |
---|---|---|
Purpose | Run code interactively | Write multi-line programs |
Output | Immediate | Run line-by-line or entire script |
Save-able | ❌ No | ✅ Yes |
Use Case | Quick tests, debugging | Full programs, reports, projects |
When you open RStudio, you’ll see:
- Console (bottom left)
- Script editor (top left)
- Environment/history (top right)
- Plots/files/packages/help (bottom right)
✍️ Your First R Commands
Open RStudio, and in the console, try:
2 + 3
🧾 Output:
[1] 5
You just performed your first R operation! Let’s try a few more:
print("Hello, R!")
🧾 Output:
[1] "Hello, R!"
x <- 10
y <- 5
z <- x * y
z
🧾 Output:
[1] 50
📄 Create Your First R Script
- Click File > New File > R Script
- Type your R code:
# My First R Script
name <- "Alice"
score <- 98
message(paste("Student:", name, "- Score:", score))
- Save the script: File > Save As > my_first_script.R
- Click “Run” or press Ctrl + Enter to execute line-by-line
📁 Understanding Working Directory
The working directory is where R reads/writes files by default.
🔍 To check your working directory:
getwd()
📍 To set a new working directory:
setwd("C:/Users/YourName/Documents/R")
Make sure the folder path exists on your system. You can also use:
- Session > Set Working Directory > Choose Directory (in RStudio)
📦 Install and Load Your First Package
Try installing a useful package like ggplot2
:
install.packages("ggplot2")
library(ggplot2)
If no errors show up, you’ve successfully installed and loaded a package!
📌 Summary – Recap & Next Steps
Getting started with R involves mastering the console, scripts, and your working directory. These basics lay the foundation for deeper data analysis, plotting, and modeling.
🔍 Key Takeaways:
- Use the console for quick code tests
- Use scripts to write reusable R code
- Learn to save files and set the working directory
- Start experimenting with built-in functions and installing packages
⚙️ Real-World Relevance:
Every R project begins with scripts and data files. Knowing how to navigate RStudio and execute R code smoothly will help you work faster in data science projects, statistical reporting, and academic research.
❓ FAQs – Getting Started with R
❓ What is the difference between R and RStudio?
✅ R is the language; RStudio is an IDE that makes using R easier with menus, panes, and tools.
❓ How do I run code in RStudio?
✅ Type code in the script editor and press Ctrl + Enter to run selected lines or use the Run button.
❓ Where do my scripts get saved?
✅ Scripts are saved as .R
files in the folder you select using setwd()
or via the RStudio interface.
❓ How do I stop a running R process?
✅ Click the red stop icon in RStudio or press Esc to interrupt execution.
❓ What is the file extension for R programs?
✅ R scripts use the .R
extension. For R Markdown reports, use .Rmd
.
Share Now :