Introduction to R Syntax

Ironically, the second thing we’re going to do is show you how to tell R to ignore a part of your program. We promise it’s very useful to know how to do this. Text written in a program but not run by the computer is called a comment. R interprets anything after a # as a comment.

Why would anyone want the computer to ignore a part of their file? Multiple reasons! Comments can:

  • Provide context for why something is written the way it is:

    # This code will be used to count the number of times anyone tweets the word persnickety persnickety_count <- 0
  • Help other people reading the code understand it faster:

    # This code will calculate the likelihood that it will rain tomorrow complicated_rain_calculation_for_tomorrow()
  • Ignore a line of code and see how a program will run without it:

    # useful_value <- old_sloppy_code() useful_value <- new_clean_code()

Annotating or documenting your code can help other people read your program later! It could also help your future-self understand the code when you go back and read an old file trying to remember how it works. Documenting your code will help others reproduce it, and it will help you become a better programmer too. Note: In R notebooks, you’re also allowed to add documentation using markdown text outside the code blocks.



Add a comment inside the code block that explains that the line of code inside notebook.Rmd is computing the volume to a cube.

Folder Icon

Take this course for free

Already have an account?