Chapter 2 Prerequisite

2.1 Installation

The installation instruction is adapted based on CRAN’s R installation guide and a DataCamp tutorial on ‘How to install R on Windows, Mac OS X, and Ubuntu’.

2.1.1 Windows

To Install R

  • Open an internet browser and go to https://cran.r-project.org/.
  • Click on the “Download R for Windows” link at the top of the page.
  • Choose the “base” and then Click on the “Download R 4.1.0 for Windows” link at the top of the page.
  • Once the download is finished, you will obtain a file named “R-4.1.0-win.exe” or similar depending on the version of R that you download.
  • Most of the time, you will likely want to go with the defaults, so click the button ‘Next’ until the process is complete.
  • Now that R is installed, you need to download and install RStudio.

To Install RStudio

  • Go to www.rstudio.com and click on the “Download RStudio” button.
  • Click on “Download RStudio Desktop.”
  • The installation process is very straightforward as the gif below.

2.1.2 Mac OSX

To Install R

  • Open an internet browser and go to https://cran.r-project.org/.
  • Click on the “Download R for (Mac) OS X” link at the top of the page.
  • Click on the file containing the latest version of R under “Files.”
  • Save the .pkg file, double-click it to open, and follow the installation instructions.
  • Now that R is installed, you need to download and install RStudio.

To Install RStudio

Very similar with Windows,

  • Go to www.rstudio.com and click on the “Download RStudio” button.
  • Click on “Download RStudio Desktop.”
  • Click on the version recommended for your system, or the latest Mac version, save the .dmg file on your computer, double-click it to open, and then drag and drop it to your applications folder.

2.1.3 Ubuntu

To Install R

As it is common, prior to installing R, let us update the system package index and upgrade all our installed packages using the following two commands:

sudo apt update
sudo apt -y upgrade

After that, all that you have to do is run the following in the command line to install base R.

sudo apt -y install r-base

To Install RStudio

Once base R is installed, you can go ahead and install RStudio. For that we are going to head over again to the RStudio downloads page and download the .deb file for our Ubuntu version as shown in the image below:

2.2 Use R inside RStudio

2.2.1 R studio

RStudio is very powerful for providing a a four pane work-spaces.

Top-left panel: Your scripts of the R codes, script is good to keep a record of your work and also convenient for command execution.

You can create a new script by:

File –> New –> R Script

Bottom-left panel: R console for R commands, where you actually run the R codes.

Top-right panel: Workspace tab: All the data(more specifically, R objects) you have created in the Workspace and all previous commands you previously ran in the History.

Bottom-right panel:

Files in your working directory(you probably should also set your working directory) in Files, and the plots you have created in Plots.

2.2.2 Set working directory

  • Create a folder named “R-workshop” in your preferred directory
  • Create a “Data” folder in the “R-workshop”
  • Download the data from the GitHub: holab-hku.github.io/R-workshop/data/ for this workshop and store the data in the “data” folder
  • From RStudio, use the menu to change your working directory under Session > Set Working Directory > Choose Directory
  • Choose the directory to “R-workshop”

Or you can type in the console:

setwd("/yourdirectory/R-workshop")

For Windows, the command might look like :

setwd("c:/yourdirectory/R-workshop")

2.2.3 Some general knowledge

  • R is case-sensitive
  • Type enter to run R code in console pane
  • Ctrl-enter or Cmd-return if the code is in the scripts pane.
  • Comments comes after # will not be treated as codes
  • R has some pre-loaded data sets, to see the list of pre-loaded data, type data()
  • In R, a function is an object, a basic syntax of an R function looks like something below:
function_name <- function(arg_1, arg_2, ...) {
   actual function codes
}

For example:

st.err <- function(x){
  sd(x)/sqrt(length(x))
}

R contains a lot of build-in functions, you can use ? to see the documentation of a function, there are also a lot if external libraries with specific functions. To use a library, we do:

install.packages('package_name')
library(package_name)

2.2.4 Install packages

There are several packages used in this workshop, in the R console, type:

install.packages('ggplot2')
install.packages('gplots')
install.packages('NMF')
install.packages('stats')
install.packages('ggfortify')