3  Setting Up Your Environment: R and RStudio

3.1 Why R and RStudio?

  • R: A powerful, free programming language specifically designed for statistical computing and graphics. Widely used in academia and industry for data analysis, including genomics and breeding.
  • RStudio: An excellent, free Integrated Development Environment (IDE) for R. It makes using R much easier with features like code highlighting, plot viewing, package management, and project organization.

3.2 Installation Steps

  1. Install R: Go to CRAN (the Comprehensive R Archive Network) and download the latest version for your operating system (Windows, macOS, Linux). Follow the installation instructions.
  2. Install RStudio: Go to the Posit website and download the free RStudio Desktop version for your operating system. Install it after installing R.
  3. Install RTools: RTools can be installed from the CRAN website. You need to install the version corresponding to the R version you have installed through its installer.
  4. Install Quarto: Go to Quarto’s website and download and install Quarto for your system. RStudio often bundles Quarto, but installing the latest version is good practice.
  5. (For PDF Output) Install LaTeX: Open RStudio, go to the Console panel, and type the following commands one by one, pressing Enter after each:
# Run these lines in the R Console 
install.packages("tinytex") 
# Run only once if you don't have it 
tinytex::install_tinytex() 
# Run only once to install LaTeX distribution
# This might take a few minutes. If it fails, consult TinyTeX documentation or ask instructors.
# Installing R Packages for the Course
# We will use several add-on packages in R. 
# You only need to install packages *once*.
# --- Run this code chunk in the R Console --- 
# List of packages we will likely need:

# Packages in Cran
cran_packages <- c(
  "tidyverse", "readxl", "writexl", "readr", "qqman", "vcfR", "QBMS", "adegenet",
  "ade4", "ggiraph", "ggpubr", "plotly", "poppr", "reactable",
  "rnaturalearth", "scatterpie", "viridis", "tibble",
  "ggplot2", "reshape2", "forcats", "dplyr", "sp", "scales", "htmltools", 
  "ASRgenomics", "statgenGWAS", "gplots", "spdep", "adespatial", "DT", "rrBLUP",
  "geodata", "terra"
)

# Bioconductor Packages
bioc_packages <- c("rrBLUP", "LEA", "impute")

# Github Packages
git_packages <- c("snpReady")

# Installing Cran Packages
installed <- rownames(installed.packages())
missing <- setdiff(cran_packages, installed)
if (length(missing)) {
  message("Installing missing CRAN packages: ", paste(missing, collapse = ", "))
  install.packages(missing)
}

# Installing Bioconductor Packages
if (!requireNamespace("BiocManager", quietly = TRUE)) {
  install.packages("BiocManager")
}
installed <- rownames(installed.packages())
missing <- setdiff(bioc_packages, installed)
if (length(missing)) {
  message("Installing missing Bioconductor packages: ", paste(missing, collapse = ", "))
  BiocManager::install(missing)
}

# Installing Github Packages
if (!requireNamespace("devtools", quietly = TRUE)) {
  install.packages("devtools")
}
installed <- rownames(installed.packages())
missing <- setdiff(git_packages, installed)
if (length(missing)) {
  message("Installing missing Github packages: ", paste(missing, collapse = ", "))
  devtools::install_github("italo-granato/snpReady")
}

3.3 Loading Libraries and Functions

# You can use library() to load any single package
# We will load all libraries using lapply()
invisible(
  suppressPackageStartupMessages(
    lapply(c(cran_packages, bioc_packages, git_packages), library, 
           character.only = TRUE)
  )
)

3.4 Quick RStudio Tour

(We will cover this live, but key windows include: Console, Script Editor/Notebook, Environment/History, Files/Plots/Packages/Help/Viewer/Projects). Familiarize yourself with these panes. More in depth information on any of the functions we use can be found by searching for the function in the Help panel.