R Comments

R Tutorial

R Comments

Comments in R are used to explain code and make programs easier to understand.

They are ignored by R during execution.


 1. Single-Line Comment (#)

R supports single-line comments using the # symbol.

Anything after # on the same line is treated as a comment.


 2. Multiple-Line Comments

R does not have a special multi-line comment syntax.
Instead, use # at the beginning of each line.


 3. Commenting Out Code (Disable Code)

Comments are often used to temporarily stop code execution.

  •  Useful for debugging

 4. Comments Inside Functions


 5. Best Practices for Comments

  •  Write comments to explain why, not just what
  •  Keep comments short and clear
  •  Avoid unnecessary comments
  •  Update comments when code changes

 6. Bad vs Good Comments

 Bad:

 Good:


 7. Keyboard Shortcut (RStudio)

  • Windows / Linux: Ctrl + Shift + C

  • Mac: Cmd + Shift + C

 Quickly comment or uncomment selected lines


Summary

  • # is used for comments

  • R supports single-line comment only

  • Comments improve code readability

  • Not executed by R

You may also like...