R Intermediate Course 2025

R is a Lego Construction Set

Bioinformatics Core Facility CECAD

2025-03-17

Slides & Code

  • [f] Full screen
  • [o] Slide Overview
  • [c] Notes
  • [h] help

git repo

R-Basic


Clone repo

git clone https://github.com/CECADBioinformaticsCoreFacility/Intermediate_R_Course_2025.git


Slides Directly

https://cecadbioinformaticscorefacility.github.io/Intermediate_R_Course_2025/

Session 1 :: R is a Lego Construction Set

R is a Lego Construction Set

R is a Lego Construction Set

R is a Lego Construction Set

R is a Lego Construction Set

R is a Lego Construction Set

R is a Lego Construction Set

R is a Lego Construction Set

R is a Lego Construction Set

R is a Lego Construction Set

R is a Lego Construction Set – One-Time Code

 
Let’s say we are tinkering interactively in the console:

hour <- 3

if(hour %in% 6:10) {
  print("Good morning!")
} else if (hour %in% c(20:24,1:5)) {
  print("Good night!")
} else {
  print("Have a nice day!")
}
[1] "Good night!"

 
But what if we want to try a different value for hour? Do we need to write it all again?

R is a Lego Construction Set – Functions

The first level of abstraction R is the function:

greet_me <- function(h) {
  if(h %in% 6:10) {
    print("Good morning!")
  } else if (h %in% c(20:24,1:5)) {
    print("Good night!")
  } else {
    print("Have a nice day!")
  }
}

 

greet_me(h = hour)
[1] "Good night!"

 

greet_me(12)
[1] "Have a nice day!"

 

greet_me(55)
[1] "Have a nice day!"

R is a Lego Construction Set – Functions

Catching illegal input: Throw an error

greet_me <- function(h) {
  if(!(h %in% 1:24)) {
    stop("Parameter h is ", h,
         ", which is not in 1:24!")
  }
  if(h %in% 6:10) {
    print("Good morning!")
  } else if (h %in% c(20:24,1:5)) {
    print("Good night!")
  } else {
    print("Have a nice day!")
  }
}

 

greet_me(55)
Error in greet_me(55): Parameter h is 55, which is not in 1:24!

… or return internally with a message

greet_me <- function(h) {
  if(!(h %in% 1:24)) {
    cat("Parameter h is ", h,
         ", which is not in 1:24!\n",
         "I will return an empty greeting :(")
    
    return("")
  }
  if(h %in% 6:10) {
    print("Good morning!")
  } else if (h %in% c(20:24,1:5)) {
    print("Good night!")
  } else {
    print("Have a nice day!")
  }
}

 

greet_me(55)
Parameter h is  55 , which is not in 1:24!
 I will return an empty greeting :(
[1] ""

R is a Lego Construction Set – Source Files

Keep a file for hand-crafted functions:

## Functions for drawing heatmaps:
## pheatmaps code, from https://www.biostars.org/p/223532/
scale_rows  <-  function(x){
  m  <-  apply(x, 1, mean, na.rm = TRUE)
  s  <-  apply(x, 1, sd, na.rm = TRUE)
  return((x - m) / s)
}

scale_mat  <-  function(mat, scale){
  if(!(scale %in% c("none", "row"))){
    stop("scale argument shoud take values: 'none' or 'row'")
  }
  mat  <-  switch(scale, none = mat, 
                  row = scale_rows(mat), 
                  column = t(scale_rows(t(mat))))
  return(mat)
}

Load the content into your workspace::  

source("functions.R")

R is a Lego Construction Set – Packages

  • The structure of an R package is best seen in its raw form, as source package: it is simply a folder with strictly defined content in plain text
  • This includes functions and possibly data, but notably also the NAMESPACE file, which describes how the package depends on other packages  
  • the post-installation structure is operation system dependent and no longer plain text

R is a Lego Construction Set – Package Dependencies

The base R function package_dependencies queries information on the CRAN repository. Therefore we need to set a valid CRAN mirror, if we have not already done so:

options(repos = findCRANmirror("web"))
getOption("repos")
[1] "https://CRAN.R-project.org"

 

List the strong dependencies of the plotting package ggplot2, i.e. those packages which need to be installed for ggplot2 to work:

tools::package_dependencies("ggplot2", which="strong")
$ggplot2
 [1] "cli"       "grDevices" "grid"      "gtable"    "isoband"   "lifecycle"
 [7] "rlang"     "S7"        "scales"    "stats"     "vctrs"     "withr"    

R is a Lego Construction Set – the CRAN repository

R is a Lego Construction Set – the CRAN repository

R is a Lego Construction Set – Libraries

 

While repositories are package sources, a destination folder for packages on our local system is called a library.
 

There may be more than a single library folder. 
Function .libPaths() can be used to list them :

.libPaths() 
[1] "/home/ugoebel/R-4.4.3/lib/R/library"

R is a Lego Construction Set – Install a Package (via R)

 

Bioi is a small and barebones package related to bioimaging . Let’s install it from CRAN:

install.packages("Bioi",
                 lib = .libPaths()[1],
                 repos = getOption("repos")
                 )

 

Note that parameters "lib" and "repos" are superfluous in this case, because

  • packages are by default installed to .libPaths()[1] (the user library)
  • we have already set the CRAN mirror URL above.

R is a Lego Construction Set – Install a Package (in rstudio)

 

First remove it again …

remove.packages("Bioi")

 

Restart the R session for rstudio's Packages pane to realize the change!

R is a Lego Construction Set – Install a Package (in rstudio)

Then do it rstudio-compliant:

R is a Lego Construction Set – Install a Package (in rstudio)

 

Have an eye on what happens in the console:

It uses a different CRAN mirror from the one we had set above!  
This happens because R had been restarted in between.

If you want a permanent value for an option,_  

set it in your .Rprofile file!

R is a Lego Construction Set – Load a Package (in rstudio)

 

Checking an entry in the Packages list invokes the library() function:

R is a Lego Construction Set – Package Dependencies

library(PKG) without further arguments scans .libPath().  

The first entry found with name “PKG” - is attached to the search path - its namespace is loaded (code can refer to PKG’s functions by name, 
without specifying the package)

Functions which would be exposed after a loading a package via library() may also be accessed via the package name: PKG::my_func(). PKG would then be in the state loaded via namespace (and not attached).

R is a Lego Construction Set – Bioconductor

R is a Lego Construction Set – Bioconductor

 

  • a typical "ecosystem" of interconnected packages 
    (most of them in R),  
    with several layers of topic specific "sub-ecosystems"
  • Bioconductor has its own repository and installer,
    • because the R and Bioconductor release schedules do not match
    • and the package interdependencies require version compatibility
## if not installed, install via install.packages()
if (!require("BiocManager", quietly = TRUE))
    install.packages("BiocManager")

## once installed, use it to install an entire Bioconductor version (here)  or individual packages
BiocManager::install(version=3.21)

R is a Lego Construction Set – Bioconductor

 

Bioconductor’s guiding idea:

  • ensure that the packages can “talk to each other”
  • in order to allow chaining of function input and output into flexible analysis workflows, even across packages
  • the ultimate “Lego construction set”!

R is a Lego Construction Set – Bioconductor

 

Here are two Bioconductor URLs in plain text, for easier copying:

  • https://bioconductor.org
  • https://carpentries-incubator.github.io/bioc-project/

While I find the official site a bit hard to navigate, the Carpentries Incubator site is a really nice introduction.

R is a Lego Construction Set – Smaller Ecosystems

A nice set of interdependent statistics-related packages is easystats (https://easystats.github.io/easystats/), with a mission “to provide a unifying and consistent framework to tame, discipline, and harness the scary R statistics and their pesky models”.

R is a Lego Construction Set – The Tidyverse

And then there is the Tidyverse, a large and growing ecosystem of cross-compatible packages, with focus on workflows on 2D tables and graphical objects: