Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Introduction and Setup  ›  Lesson

Your First R Script

Your First R Script 12 minAn R script is a plain text file of commands and comments that R runs from top to bottom, showing only what you print or what is automatically printed at the top level.

Your First R Script

Typing into the console is fine for a quick sum, but next week you will want to run the same harvest summary again with new numbers. A script saves your steps in a file, so you can rerun them, fix them and share them.

The rule

A script is a saved conversation with the console, run top to bottom.

A script is a plain text file whose name ends in .R. R reads it line by line, from the first line to the last, much as if you had typed each line at the > prompt.

# harvest.R — summarise this week's garden harvest
kg <- c(12.5, 8, 15, 3.5, 6)   # kilograms from plots A to E

total <- sum(kg)
cat("Total harvest:", total, "kg\n")
print(mean(kg))

Comments start with #. R ignores everything from the # to the end of that line, so a comment can sit on its own line or after code. R has no block-comment syntax: for several lines, start each one with # (in RStudio, select the lines and press Ctrl+Shift+C, or Cmd+Shift+C on a Mac). Use comments to say why, not to repeat what the code already says.

Output comes from two everyday functions:

  • print(x) shows a value the way the console does, with the [1] label: [1] 9.
  • cat(...) writes plain text: no quotes, no [1], pieces separated by spaces, and no line break unless you add "\n".

Three ways to run it:

  1. In an editor, put the cursor on a line and press Ctrl+Enter (Cmd+Enter on a Mac) to send that line to the console.
  2. At the console, source("harvest.R") runs the whole file.
  3. In a terminal, Rscript harvest.R runs it without opening an editor.

One difference catches everyone. At the console, and with Rscript, a bare value such as total on its own line is printed automatically. With source(), and inside loops and functions, nothing is printed unless you call print() or cat().

The shortcut

Read a script the way R does: one line at a time, top to bottom, skipping everything after a #. Assignments are silent; only printing puts something on the screen.

Running harvest.R with RscriptStep 1 of 5
  1. The first line

    A comment. R skips it entirely.

When a script "does nothing", check whether you are relying on automatic printing. Wrapping the value in print() works everywhere.

Drills

Answer fast — trust the rule.

quick drill

What does R do with the line kg <- c(1, 2) # two plots?

quick drill

How do you write a comment that spans three lines?

quick drill

Which command runs harvest.R from a terminal?

quick drill

What does cat("Total:", 45, "kg\n") print?

quick drill

A script run with source("harvest.R") ends with the bare line total. What appears?

Remember

  • A script is a saved conversation with the console, run top to bottom.
  • Shortcut: read it like R does. Skip comments, assignments are silent, only printing shows.
  • Trap: source() (by default), loops and functions do not auto-print, so use print() or cat().

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainWhy did nothing print?

Your script ends with the line mean(kg) on its own. Running it with Rscript shows the mean, but running it with source() shows nothing. Explain why, and how to make the output appear either way.