OOP in R: A Short Tutorial on S4 Class

This is a hands-on tutorial about OOP in R, giving a short tutorial about S4 class.

Note that S4 class is just one of at least four R’s object systems available to the R programmers: S3, S4, R5, and R6.

The famous Biocunductor folks makes heavy use of S4 classes, but Google, on the other hand, advise to “avoid S4 objects and methods when possible”. Read more OOP in R: A Short Tutorial on S4 Class

OOP in R: An Tutorial about S3 and S4 Classes

This tutorial gives a brief introduction about OOP in R, covering both S3, S4, reference (R5), and R6 classes.

1  S3 Classes

1.1  The basic idea

1.1.1  Class attribute

Everything in R is treated as an object, and one common attribute associated with an object is its class. A class attribute is a character vector giving the names of the classes from which the object inherits. If the object does not have a class attribute, it has an implicit class. For example, Matrices and arrays have class “matrix” or”array” followed by the class of the underlying vector.  Read more OOP in R: An Tutorial about S3 and S4 Classes

Install R Under Ubuntu

This totorial teaches you on how to install R under Ubuntu. 


You can easily install R within Ubuntu Software Center, but what you get may not be the latest version of R. 

Install R under UbuntuAt the time when this tutorial is written, the latest R is of version 3.1.2 released on 2014-10-31, and the latest version of Ubuntu is version 14.10 Utopic, released on 2014-10-23. Therefore, this tutorial covers installing of the latest stable version of R under Ubuntu 14.10 Utopic.

Read more Install R Under Ubuntu

Install GTK+ for R under Windows

This short tutorial teaches you how to install GTK+ for R under windows.

Installation procedure:

  • From the R command line (or in R-Studio), install the RGtk2 package by running

    This might fail with the warning that package ‘RGtk2’ is not available (for R version xxx). If so, just run

    to install the RGtk2 package directly from its source code (this might take a few minutes).

  • Load the package by running:

    This will notice the missing GTK and prompt you to install it.
    Choose “Install GTK+” when prompted, it might take a few minutes to install. Afterwards it will likely still complain (restart required).

Read more Install GTK+ for R under Windows

Quick Start with R Programming

This tutorial helps you quickly learn how to do R programming.


1  Must-know Prelimibaries for Learning R

1.1 Get helps in R

  • To get help on a function or a dataset:
    ?function_name <==> help(function_name)
    For example, ?mean is equivalent to help(mean) 
  • To find functions using a keyword
    ??keyword <==> help.search(keyword)
    Note that search term of multiwords shall be quoted.
    For example, ??plotting is equivalent to help.search(plotting)
    ??”nonlinear regression” is equivalent to help.search(“nonlinear regression”)

Read more Quick Start with R Programming

Filtering index of vectors in R

This short tutorial talks about filtering index of vectors in R.

Lets start with an example:

Read more Filtering index of vectors in R

Special and missing values in R: NA, NaN, NULL, Inf

This tutorial briefly discuss handling of missing values in R, including NA, NaN, and NULL.

1  NA vs NaN vs NULL vs Inf

  • In statistical data sets, we often encounter missing data, which are represented with NA in R. The motivation of NA, meaning ‘Not Available’, is to handle the case where specifications to an operation is not complete.
  • NaN, meaning ‘Not A Number’, is another kind of ‘missing’ that is produced by numerical computation when the result cannot be defined sensibly. In other words, the calculation either didn’t make mathematical sense or could not be performed properly.
  • NULL represents that the value in question simply does not exist, rather than being existent but unknown.
  • Inf and -Inf represent positive and negative infinities, respectively, resulting numerical calculations.

Read more Special and missing values in R: NA, NaN, NULL, Inf

Customize environment in R

This tutorial talks about how to customize environment in R.

In R, users can customize their environment in several different ways.

1  Site initialization file

The site initialization file contain the commands that you want to execute every time R is started under your system. The location of this file is determined by following rule:

  1. If the R_PROFILE environment variable is set, then it determines the location of the site initialization file;
  2. Otherwise, the file Rprofile.site in the R home sub-directory etc is used.

Read more Customize environment in R

Introduction to R Language

This tutorial presents an introduction to R programming.

1  Preliminaries

  • R language is case sensitive
  • R names consist of alphanumeric symbols, plus ‘.’ and ‘_’, with a restriction that a name must start with ‘.’ or a letter.
  • Two kinds of basic R commands: (1) expressions (evalue, then print, then value is lost); (2) assignment (evaluate, store the value, but not print).
  • Multiple commands can co-exist in one line, separated by ‘;’. Nultiple commands can also be grouped together into one compound expression by a pair of braces ‘{‘ abd ‘}’.
  • We can run a R file, say example.R, by the command
    > source(“example.R”)
  • By default, R outputs evaluation results to the console. However, the outputs can be re-directed to a file, say output.txt, by the command
    > sink(“output.txt”)
    and such redirection can be stopped to resume normal console output by
    > sink()
  • To get the help of a function, for example, solve(), we can use the commands
    >help(solve)  or
    > ?solve
  • The command
    > objects()
    returns the names of objects in the workspace under current R session, and the rm() function can be used to remove objects from the workspace
    > rm(x,y,z, temp,foo)
  • When exiting a R session, R prompts to ask whether to save the workspace, meaning that all objects will be saved to a .RData file, and all command lines will be saved to a .Rhistory file. Later if R is started from same directory, these history data will be loaded into R session. It is recommended that you should use separate working directory for analyses conducted with R. 

Read more Introduction to R Language