1 Using R in real life

1.1 Organizing work

Usually, work is organized into a directory with:

R can also save the state of the current session (prompt when choosing to quit() R), and to view and save the history() of the the current session; I do not find these to be helpful in my own work flows.

1.2 R Packages

All the functionality we have been using comes from packages that are automatically loaded when R starts. Loaded packages are on the search() path.

##  [1] ".GlobalEnv"        "package:BiocStyle" "package:stats"     "package:graphics" 
##  [5] "package:grDevices" "package:utils"     "package:datasets"  "package:methods"  
##  [9] "Autoloads"         "package:base"

Additional packages may be installed in R’s libraries. Use `installed.packages() or the RStudio interface to see installed packages. To use these packages, it is necessary to attach them to the search path, e.g., for survival analysis


There are many thousands of R packages, and not all of them are installed in a single installation. Important repostories are

Packages can be discovered in various ways, including CRAN Task Views and the Bioconductor web and Bioconductor support sites.

To install a package, use install.packages() or, for Bioconductor packages, instructions on the package landing page, e.g., for GenomicRanges. Here we install the ggplot2 package.

install.packages("ggplot2", repos="")

A package needs to be installed once, and then can be used in any R session.

2 Graphics and Visualization

Load the BRFSS-subset.csv data

path <- "extdata/BRFSS-subset.csv"   # or file.choose()
brfss <- read.csv(path)

Clean it by coercing Year to factor

brfss$Year <- factor(brfss$Year)

2.1 Base R Graphics

Useful for quick exploration during a normal work flow.

2.2 What makes for a good graphical display?

2.3 Grammar of Graphics: ggplot2


‘Grammar of graphics’