Comments are essential for readable, maintainable code. While R doesn’t have a native block comment syntax (like /* */ in other languages), there are multiple ways to create multi-line comments. Good comments explain why your code does something, not what it does.
When to Use Comments
You’ll use comments when:
- Explaining complex logic
- Documenting assumptions or data structures
- Noting performance considerations
- Temporarily disabling code
- Adding references or citations
- Noting TODO items
Methods for Multi-Line Comments
Method 1: Repeat # Symbol (recommended)
# This is a multi-line comment
# You can explain your code here
# Each line needs its own # symbol
Method 2: IDE Keyboard Shortcut
- RStudio: Ctrl + Shift + C (Windows/Linux) or Cmd + Shift + C (Mac)
- Automatically adds # to multiple selected lines
Method 3: Block comments with repeated # (formatting)
################################
# This is a block comment
# It stands out visually
################################
Examples with Explanations
Create Multi-Line Comments Using # Symbol
Let’s see how we can add multiline comment in R.
# print("This shows before creating multi line command")
# print("After creating multi line command")
# print("Hello R Language")
print("Hello R Language")
Output:
[1] "Hello R Language"
Here the output shows the string we printed using print(). The other statements are commented out with # symbols, so they don’t execute.
Common Mistakes to Avoid
Mistake 1: Forgetting # on each line
# ❌ WRONG - Second line isn't commented!
# This is a comment
This is NOT a comment - will cause error!
# ✅ CORRECT - Each line needs #
# This is a comment
# This is also a comment
Mistake 2: Using /* */ block comments (from other languages)
# ❌ WRONG - R doesn't support block comments
/* This is NOT a comment in R
It will cause an error */
# ✅ CORRECT - Use # for each line
# This is a comment in R
# Multiple lines like this
Mistake 3: Comments that don’t add value
# ❌ WRONG - States the obvious
x <- 5 # Assign 5 to x
y <- x + 1 # Add 1 to x and assign to y
# ✅ BETTER - Explain the "why"
x <- 5 # Initialization value for algorithm
y <- x + 1 # Increment by 1 per iteration
Mistake 4: Unbalanced commented code
# ❌ CONFUSING - Hard to find where comment ends
# result <- function(x) {
# if (x > 0) return(x * 2)
# }
# Some code here
# ✅ CLEARER - Use block markers
# ===== COMMENTED OUT =====
# result <- function(x) {
# if (x > 0) return(x * 2)
# }
# ========================
Pro Tips
- Use comments for future you: Explain why you chose an approach, not what code does
- Document assumptions: “Assumes data is sorted”, “Requires positive values”
- Mark TODOs:
# TODO: Optimize this loop for large datasets - Use in RStudio: Select lines, press Ctrl+Shift+C to toggle comments
- Document regex patterns:
# Pattern: matches email addresses ([email protected]) email_pattern <- "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$"
Comment Best Practices
# BAD: Too many obvious comments
x <- 1 # Set x to 1
if (x > 0) { # If x is greater than 0
print(x) # Print x
}
# GOOD: Comments explain intent
# Initialize counter for main loop
x <- 1
if (x > 0) { # Positive values only (per requirement)
print(x)
}