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

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

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