Chapter 6: R Comments

1. What are Comments in R? (The Simple Truth)

Comments are notes you write inside your code that the computer completely ignores. R never reads or executes them — they’re only for humans (you, your teammates, your future self, or your professor checking your project).

Main purposes:

  • Explain why you wrote something (the intention / business logic)
  • Make code easier to read & understand later
  • Temporarily disable lines during testing/debugging
  • Add section headers / TODO notes / references

2. The Only Syntax Rule in R for Comments (Very Simple!)

In R, everything after a # (hash/pound sign) on the same line is a comment → ignored by R.

R

Key facts (memorize these!):

  • # must be at the start of a comment (or after code on the same line)
  • No multi-line comment like /* */ in C/Java or “”” in Python → only single-line with #
  • To comment multiple lines → put # at the beginning of each line
  • In RStudio: Ctrl+Shift+C (Windows/Linux) or Cmd+Shift+C (Mac) → toggles comment on selected lines (super handy!)

3. Real Examples – Let’s See Them in Action

Example 1: Basic explanatory comments

R

Example 2: Inline comments (explaining tricky parts)

R

Example 3: Section headers (very common in long scripts)

R

Example 4: Debugging / Testing (temporary comments)

R

Example 5: TODO / FIXME notes (professional habit)

R

4. Modern Best Practices for Comments in R (2026 Style)

From tidyverse style guide, R community, books like “The Art of Readable Code”, and recent advice:

Rule / Tip Why it matters Good Example Bad Example (avoid)
Explain WHY, not WHAT Good code already shows WHAT – comments should explain intention / trade-offs # Use log-transform because data is right-skewed (confirmed by skewness test) # Take log of x (obvious from code)
Keep comments up-to-date Outdated comments are worse than no comments – misleading Update when you change logic # Add 5 to each (but code now adds 10)
Use them sparingly Too many comments = visual noise – aim for self-documenting code Name variables clearly → fewer comments needed Commenting every single line
Use complete sentences Easier to read, looks professional # Calculate monthly growth rate as percentage change from previous month. # growth rate
Put file-level / script header At top: purpose, author, date, dependencies See header example below No header → hard to know what script does
Use roxygen2 for functions/packages Automatic help files + documentation For packages/functions (we’ll cover later) Plain # comments inside package functions
Separate sections clearly Use # ==== or # —- for visual blocks Shown in example 3 above No separation in long scripts

Recommended script header (copy this style!)

R

5. Quick Practice – Improve This Messy Code with Comments

Bad version (no comments):

R

Better version (good comments):

R

See how much clearer it becomes?

Final Thoughts from Your Teacher

  • Good comments = kindness to your future self (you’ll thank yourself in 6 months!)
  • Bad/over comments = noise (train yourself to write clear variable names & functions first)
  • In RStudio: comments appear in gray/different color → easy to spot
  • Shortcut love: Ctrl+Shift+C to comment/uncomment blocks quickly

Ready for next step?

  • Want to practice writing a commented mini-script together?
  • Move to R vectors or data frames in detail?
  • Or learn roxygen2 comments for functions?

Just tell me — whiteboard is ready! ☕📝🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *