DEV Community

Cover image for R is Heavily Functional and Classy too
Oluwafemi Paul Adeyemi
Oluwafemi Paul Adeyemi

Posted on

R is Heavily Functional and Classy too

I believe that R was primarily made functional because it was intended to be used by Statisticians who are used to functions. For instance, whereas python gives a pandas dataframe object a head method, base R uses a head function to get the first few rows of the dataset been worked on. By base R, it is meant the basic codes, the ground functionalities of R upon which other programs (or packages) can be built.

# Python
data_head = data.head()
Enter fullscreen mode Exit fullscreen mode
# R
data_head = head(data)
Enter fullscreen mode Exit fullscreen mode

This is the case with most of the base R in-built features, however, R still allows for object oriented programming too. Meaning that you can also create classes and create objects from them. But suppose you wanted to make a class, how do you do that?

First, the easiest way to write a class is to use the reference class.

# R
library(methods)

Vehicle = setRefClass("Vehicle", 
  fields = list(
     name = "character", 
     engine = "character", 
     weight = "numeric"
  ), 
  methods = list(
     horn = function(){
        # some codes
     },
     move = function(){
        # some codes
     }
  )
)
car1 = Vehicle(name = "Toyota", engine="s8", weight=45)
# print the name of the vehicle
car1$name
# Let the vehicle horn
car1$horn()
# Let the vehicle move
car1$move()
Enter fullscreen mode Exit fullscreen mode

Another variant of classes in R is the R6 class illustrated below.

# R
library(R6)

Vehicle <- R6Class("Vehicle",
  public = list(
    name = NULL,
    engine = NULL,
    weight = NULL
    initialize = function(name = NA, engine = NA, weight = NA) {
      self$name <- name
      self$engine = engine
      self$weight = weight
      self$horn()
    },
    horn = function(val) {
      # some codes
    move = function() {
      # some codes
    }
  )
)

car1 = Vehicle$new(name = "Toyota", engine="s8", weight=45)
# print the name of the vehicle
car1$name
# Let the vehicle horn
car1$horn()
# Let the vehicle move
car1$move()
Enter fullscreen mode Exit fullscreen mode

I guess you understood that in both cases instead of using the . operator as in data.head() in Python, we are using the $ operator in R: as in car1$name, car1$horn() e.t.c. and that the major differences are that Vehicle$new was used to create a new Vehicle object ( a car named Toyota), a public access modifier was used and an initializer (constructor) was added in the R6 class. while that was not the case with the reference class. An object oriented version of Shiny (for building web apps) called Tidymodules uses the R6 class in its design. See 1 and 2 for more details on the classes available in R.

R may not have the Python, Java, C++, C# e.t.c type of classes, but similar concepts are addressed in its classes. R still very much supports object oriented programming, even though it is heavily functional.

Top comments (2)

Collapse
 
respect17 profile image
Kudzai Murimi

Thanks!

Collapse
 
opaul profile image
Oluwafemi Paul Adeyemi

I appreciate your comment @respect17. Thank you.