1Objectives¶
By the end of this lesson you will be able to:
Name the three common ways to run R code and when you’d use each.
Run a single line of R in the Console and read the result.
Write and run a short multi-line script.
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.
| Way | What it feels like | Best for |
|---|---|---|
| The Console | A one-line-at-a-time chat with R: you type, press Enter, get an answer immediately | Quick 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 line | Homework 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 underneath | Guided 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] 12The 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] 12The 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] 12In 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] 37Run 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] 3Only 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:
a text cell, formatted like a mini word-processor document (what you’re reading right now, if this book were a notebook), or
a code cell, which looks like a small script — you type R into it and run just that cell with Shift+Enter. The output appears directly underneath, and stays there even after you close and reopen the notebook (unlike the plain Console).
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¶
Checking one quick thing? Console.
Doing homework you’ll save or turn in? A script, or the notebook your instructor gives you.
Following a guided walkthrough with explanation mixed in? A notebook.
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¶
The Console runs one line at a time and forgets everything when closed.
A script (
.Rfile) saves a sequence of commands you can rerun anytime.A notebook (
.ipynb, used on JupyterHub) mixes explanation with runnable code cells and keeps their output.<-stores a value in a name (quiet); typing the bare name, orprint(), displays it.#starts a comment — text for humans that R ignores.
9Check your understanding¶
You want to save a homework calculation so you can reopen it next week. Which of the three ways should you use, and why?
What does the line
score <- 88print when you run it by itself? Why?Rewrite this script with one helpful comment added above each line:
gallons <- 12 miles <- 350 mpg <- miles / gallons