15 Using R in real life
15.1 Organizing work
Usually, work is organized into a directory with:
- A folder containing R scripts (
scripts/BRFSS-visualize.R) - ‘External’ data like the csv files that we’ve been working with, usually in a separate folder (
extdata/BRFSS-subset.csv) - (sometimes) R objects written to disk using
saveRDS()(.rdsfiles) that represent final results or intermediate ‘checkpoints’ (extdata/ALL-cleaned.rds). Read the data into an R session usingreadRDS(). - Use
setwd()to navigate to folder containing scripts/, extdata/ folder - Source an entire script with
source("scripts/BRFSS-visualization.R").
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.
15.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.
search()## [1] ".GlobalEnv" "package:RColorBrewer" "package:stats"
## [4] "package:graphics" "package:grDevices" "package:utils"
## [7] "package:datasets" "package:methods" "Autoloads"
## [10] "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
library("survival")There are many thousands of R packages, and not all of them are installed in a single installation. Important repositories are
- CRAN: https://cran.r-project.org/
- Bioconductor: https://bioconductor.org/packages
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="https://cran.r-project.org")A package needs to be installed once, and then can be used in any R session.