1Objectives¶
By the end of this lesson you will be able to:
Explain what an R package is and why courses use them.
Install a package (and explain why you only do this once).
Load a package with
library()(and explain why you do this every session).Confirm the two packages this course uses,
mosaicandBSDA, are working.
2What’s a package?¶
Base R (what you installed in Lesson 2) already knows how to do
arithmetic, build vectors, and call functions like mean() and round()
(Lesson 4). A package is an add-on bundle of extra functions,
written by someone else and shared for free, that you can turn on when you need
them. Think of base R as a phone’s default apps, and a package as something you
download from an app store — except every package used in this course is entirely
free, open, and vetted by the R community.
MATH 1209 uses exactly two packages, all semester, and no others:
| Package | What it adds | Why this course uses it |
|---|---|---|
mosaic | A consistent, beginner-friendly way to summarize data and make plots, all using one grammar (y ~ x, read “y broken down by x” — you’ll meet this properly in Lesson 6) | One syntax pattern covers summaries, tables, and graphs, so you learn it once |
BSDA | Statistical tests written directly from summary statistics (mean, SD, sample size) instead of requiring the raw data | Textbook and calculator problems usually give you summary statistics — BSDA matches that exactly |
You will not need any other package this semester.
3Installing a package (once per computer)¶
Installing downloads a package’s code onto your computer, one time. You do not
need to do this on CSUB JupyterHub — the coordinator’s environment already has
mosaic and BSDA installed for you. If you’re on your own laptop
(Lesson 2, Option A), run this once, in the Console:
install.packages(c("mosaic", "BSDA"))You will see a scroll of text while it downloads and installs — that’s normal.
Once it finishes and returns you to a blank prompt, installation is done, and you
never need to run install.packages() for these two again on that computer (until
you get a new computer, or reinstall R from scratch).
4Loading a package (every session)¶
Installing is a one-time download; loading with library() is what actually
turns a package’s functions on for your current R session — and you do this
every time you start a new R session (open RStudio, or open a fresh notebook).
It’s like the difference between installing an app once and opening it each time
you want to use it.
library(mosaic)Registered S3 method overwritten by 'mosaic':
method from
fortify.SpatialPolygonsDataFrame ggplot2
The 'mosaic' package masks several functions from core packages in order to add
additional features. The original behavior of these functions should not be affected by this.
Attaching package: 'mosaic'
The following objects are masked from 'package:dplyr':
count, do, tally
The following object is masked from 'package:Matrix':
mean
The following object is masked from 'package:ggplot2':
stat
The following objects are masked from 'package:stats':
binom.test, cor, cor.test, cov, fivenum, IQR, median, prop.test,
quantile, sd, t.test, var
The following objects are masked from 'package:base':
max, mean, min, prod, range, sample, sumThat is a lot of text, and it can look alarming the first time — it is not an
error. Every line starting with “The following...” is R telling you, honestly,
that mosaic is replacing (R’s word: “masking”) a handful of base
functions — like mean() and sum() — with its own versions that add small
teaching improvements. You almost never need to think about this again; it is
simply what a normal, successful library(mosaic) looks like.
library(BSDA)
Attaching package: 'BSDA'
The following object is masked from 'package:mosaic':
CIsim
The following object is masked from 'package:mosaicData':
Alcohol
The following object is masked from 'package:datasets':
OrangeSame idea, shorter list. Both packages loaded successfully.
5Confirming your toolkit is on¶
A quick way to check both packages are really loaded, without scrolling back
through the startup messages, is search(), which lists everything currently
turned on — or, more simply, ask a yes/no question about it:
"package:mosaic" %in% search()
"package:BSDA" %in% search()[1] TRUE
[1] TRUEYou can also check which version you have — useful if your instructor ever asks, or if a function behaves differently than this book describes:
packageVersion("mosaic")
packageVersion("BSDA")[1] '1.9.2'
[1] '1.2.2'(Your version numbers may be slightly newer, depending on when your computer or CSUB’s JupyterHub last updated — that’s fine.)
Finally, a real smoke test: mosaic ships with some small practice datasets built
in, including one called KidsFeet (foot measurements from a group of kids — you’ll
use it again in Lesson 6). If your toolkit is working, this should run
with no errors and print a table of summary statistics:
favstats(~ length, data = KidsFeet) min Q1 median Q3 max mean sd n missing
21.6 24 24.5 25.6 27.5 24.72308 1.317586 39 0You’ll learn exactly what favstats() and this ~ symbol mean in
Lessons 6 and 7 — for now, the fact that a real table of
numbers appeared means mosaic is fully working.
6Every session, from here on: your two-line start¶
From this point in the course forward, the very first thing you type in any new R session is:
library(mosaic)
library(BSDA)Make this a habit — most “function not found” errors after Lesson 5 trace back to forgetting this step in a fresh session.
7Summary¶
A package adds extra functions on top of base R; this course uses exactly two,
mosaicandBSDA.install.packages()downloads a package — once per computer. Not needed on CSUB JupyterHub.library()turns a package on for your current session — every time you start a new one.“Masked” messages after
library(mosaic)are normal, not errors.packageVersion()and a quick smoke-test function call confirm your toolkit is really working.
8Check your understanding¶
What’s the difference between
install.packages()andlibrary(), and how often do you run each?You restart RStudio after lunch and get
Error in favstats(~length, data = KidsFeet) : could not find function "favstats". What almost certainly happened, and what’s the one-line fix?Why doesn’t this course ask you to install any package besides
mosaicandBSDA?