Chapter 15: R Escape Characters
1. What is an Escape Character in R?
An escape character is a special combination that starts with a backslash \ inside a string.
The backslash tells R: “The next character is not normal text — it has a special meaning”
Without escaping, some characters would break the string syntax or do unexpected things.
2. The Most Important Escape Sequences in R
Here are the ones you will actually use (in order of frequency):
| What you want to include | How to write it in string | Example code | What you see when printed (print() or cat()) |
|---|---|---|---|
| Double quote “ | \” | “He said \”Hi\”” | He said “Hi” |
| Single quote ‘ | \’ | ‘It\’s hot today’ | It’s hot today |
| Backslash \ | \\ | “C:\\Users\\Webliance” | C:\Users\Webliance |
| New line (line break) | \n | “Line 1\nLine 2” | Line 1 Line 2 |
| Tab (horizontal spacing) | \t | “Name\tAge\tCity” | Name Age City |
| Carriage return | \r | (rarely used alone) | — |
| Unicode character | \uXXXX or \UXXXXXXXX | “\u20B9” (Indian Rupee) | ₹ |
| Bell (beep sound) | \a | cat(“\a”) | makes a beep (if terminal supports) |
3. Live Examples – Copy-Paste These Right Now
Example 1: Quotes inside strings (most common problem)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# Wrong – syntax error wrong <- "He said "Hello, Hyderabad!"" # Correct ways good1 <- "He said \"Hello, Hyderabad!\"" good2 <- 'He said "Hello, Hyderabad!"' # ← easiest – use single quotes outside cat(good1, "\n") cat(good2, "\n") |
Example 2: Windows file paths (very common pain in India)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# Wrong – R thinks \U is unicode, \W is unknown escape wrong_path <- "D:\Users\Webliance\Documents\data.csv" # Correct – double every backslash correct_path <- "D:\\Users\\Webliance\\Documents\\data.csv" # Even better – use forward slashes (R accepts them on Windows!) best_path <- "D:/Users/Webliance/Documents/data.csv" # Or use raw strings (R 4.0.0+) raw_path <- r"(D:\Users\Webliance\Documents\data.csv)" print(correct_path) print(best_path) print(raw_path) |
Example 3: Multi-line messages / reports
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
report <- "Hyderabad Weather Report - Feb 17, 2026 ---------------------------------------- City: Hyderabad Temp: 29.5°C Humidity: 62% Wind: 12 km/h Note: \"Feels like summer already!\"\n" cat(report) |
→ You get nice formatted output with line breaks and quotes.
Example 4: Indian Rupee symbol & other Unicode
|
0 1 2 3 4 5 6 7 8 9 10 |
rupee <- "\u20B9" # or "\U0001F4B9" for emoji version amount <- 1500 message <- paste0("Price: ", rupee, amount) cat(message, "\n") # Price: ₹1500 |
4. Two Modern Super Helpful Tricks (2026 Style)
a) Raw strings – r”(…)” (available since R 4.0 – you have it!)
No escaping needed inside!
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# Perfect for Windows paths, regex, JSON, SQL path <- r"(C:\Program Files\R\R-4.4.1\bin\x64\R.exe)" regex_pattern <- r"(\d{2}-\d{2}-\d{4})" # date like 17-02-2026 json_like <- r"( { "city": "Hyderabad", "temp": 29.5, "note": "It's hot!" } )" cat(path, "\n") |
→ No \\, no \” — super clean.
b) cat() vs print() – escape behavior difference
|
0 1 2 3 4 5 6 7 8 9 |
s <- "Line1\nLine2\tTabbed\nHe said \"Hi\"" print(s) # shows escapes literally: "Line1\nLine2\tTabbed\nHe said \"Hi\"" cat(s) # interprets escapes: actual new lines and tabs |
→ Use cat() when you want to see the formatted output.
5. Common Beginner Mistakes & Fixes (Hyderabad Edition)
Mistake 1 — Writing Windows paths with single \
→ Fix: double \\ or use / or r”()”
Mistake 2 — Nested quotes without escaping
→ Fix: alternate quotes or escape inner ones
Mistake 3 — Forgetting \n when wanting line breaks
→ Fix: add \n and use cat() or writeLines()
Mistake 4 — Thinking \t always aligns perfectly
→ Reality: tab size depends on console → better to use format() or knitr::kable() for tables
6. Quick Reference Table (Save This!)
| Goal | Escape sequence | Alternative (often better) |
|---|---|---|
| Include ” inside ” string | \” | Use ‘ ‘ for outer string |
| Include \ in path | \\ | / or r”(…)” |
| New line | \n | cat() + multiple strings |
| Tab | \t | format(…, width=…) |
| Rupee ₹ | \u20B9 | paste0(“₹”, amount) |
| Raw string (no escaping) | — | r”(your\text\here)” |
Your Mini Practice (Copy → Run & Fix!)
Try to fix these broken lines:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# Broken 1 msg <- "It's "very hot" in Hyderabad today" # Broken 2 file <- "D:\R\projects\data.csv" # Broken 3 note <- "Price: ₹500 Discount: 10%" cat(note) |
How would you rewrite them cleanly? (Try raw strings for the file path!)
Ready for next?
- Want to practice stringr / glue with escapes?
- Go deeper into regular expressions (where escapes are everywhere)?
- Or next topic (factors, vectors, data frames)?
Just tell me — I’m right here with the whiteboard! ☕📝🚀
