Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

1Objectives

By the end of this lesson you will be able to:

  1. Name the three common ways to run R code and when you’d use each.

  2. Run a single line of R in the Console and read the result.

  3. Write and run a short multi-line script.

  4. Write a comment, and explain why comments matter.

2Three ways to run the same R

Lesson 1 said the R engine is identical no matter what window you use. Now let’s look at the three ways of typing to it you’ll actually encounter this semester.

WayWhat it feels likeBest for
The ConsoleA one-line-at-a-time chat with R: you type, press Enter, get an answer immediatelyQuick questions, checking one calculation, exploring
A script (a .R file)A text document of R commands you write ahead of time, then run all at once or line by lineHomework you want to save, redo, or hand in
A notebook (a .ipynb file, used on JupyterHub)A document that mixes your own written notes with small chunks (“cells”) of runnable R, each showing its output right underneathGuided labs and anything that mixes explanation with code — this book’s format

You’ll meet all three. None of them is “more advanced” than the others — they’re just different shapes for the same underlying R.

3The Console: type, Enter, answer

If you’re in RStudio, the Console pane is always there, usually bottom-left. Click into it, type a line, press Enter:

5 + 7
[1] 12

The Console remembers things you’ve created, for as long as your R session stays open. Try this as two separate lines (press Enter after each):

x <- 5 + 7
x
[1] 12

The first line used <- (typed as a less-than sign and a dash) to store the result of 5 + 7 in something named x — this is called an assignment, and you’ll use it constantly starting in Lesson 4. Notice the first line by itself printed nothing: assignment is quiet. The second line just asked “what is x?” and R answered.

You can also ask explicitly with print():

print(x)
[1] 12

In the Console, typing a bare name and using print() on it do the same thing — print() becomes useful later when it’s not the last line of a script (more on that below).

4A script: several lines, saved to a file

A script is just a plain text file ending in .R that holds a sequence of R commands. In RStudio: File ▸ New File ▸ R Script, then type into that new pane (usually top-left, above the Console). To run a line, click on it and press Ctrl+Enter (Windows) or Cmd+Enter (Mac); this sends that line down to the Console and runs it there — the script and the Console are always talking to each other.

Here’s a short script — three lines that build on each other, converting a body temperature from Fahrenheit to Celsius:

temp_f <- 98.6
temp_c <- (temp_f - 32) * 5 / 9
temp_c
[1] 37

Run each line in order (top to bottom) and you’ll see only the last line print anything — the first two are assignments (quiet), and the third just asks “what is temp_c?” This is exactly the pattern you’ll use over and over: build up a value across a few lines, then look at it.

Save the file (Ctrl+S / Cmd+S) and give it a sensible name, like lesson3.R. Now it’s on your computer permanently — close RStudio, reopen it later, and your script is still there (though anything it computed has to be re-run).

5Comments: notes to your future self

Anything on a line after # is a comment — R ignores it completely. Comments don’t produce output; they exist purely so a human (often you, a week later) can understand what a script is doing.

# this line is a comment; R ignores it
naps <- 3  # you can also comment at the end of a line
naps
[1] 3

Only the assignment and the final naps did anything; both # lines contributed nothing to the answer. Get in the habit of leaving a short comment above any line that isn’t obvious at a glance — Lesson 11 makes this a formal habit.

6A notebook: notes and code cells together

On CSUB JupyterHub (or in RStudio’s own notebook format, if your instructor uses it), you work in cells. A cell is either:

The advantage of a notebook is that explanation and code live in the same document, in order — which is why every guided lab in this course, and every lesson in this book, is built that way.

7Choosing among the three, in practice

You will not be forced to master all three at once — this course typically hands you a ready-made notebook for any graded R activity, and the Console is there whenever you just want to check a number.

8Summary

9Check your understanding

  1. You want to save a homework calculation so you can reopen it next week. Which of the three ways should you use, and why?

  2. What does the line score <- 88 print when you run it by itself? Why?

  3. Rewrite this script with one helpful comment added above each line:

    gallons <- 12
    miles <- 350
    mpg <- miles / gallons