[PYTHON] Try using an object-oriented class in R (R6 method)

Introduction

It may not be a very famous fact, but even in R, if you use the package exclusively, you can use it in an object-oriented manner. In particular, here I will study object-oriented programming using a package called R6. Other similar packages include S3, S4 (this is recommended by Google's internal code conventions), RC methods, and more. However, I heard that R6 can write in the most common OO language style. Torima The following seems to be possible normally.

· Creating classes and instances -Creating member variables and class methods -Invoking the constructor when creating an instance -Use of private methods and public methods (or variables) properly ・ Inheritance (not yet verified by me) · etc

Reference material

A really cohesive SlideShare document presented at TokyoR http://www.slideshare.net/nakamichi/r6-upload?ref= It seems that the field here is basically a member variable. Fields and methods can be Public or Private

However, instead of specifying Public and Private for each variable or method as in Python, Define what you put in Public and Private together http://www.slideshare.net/hiroki84/r6-classes-42764598 http://qiita.com/hoxo_m/items/23b10243b6ffbea33a80

Sample code

First make a class


#Install the R6 package
install.packages("R6")
library(R6)
 
 
Person <-
#Let's create a class called "Person".
  R6Class("Person",
 
 
      public = list(
     #Members that can be called from outside the class are public= list()Describe in list format in
     #Both member variables and class methods can be written in this List.
     #Private variables and private methods are private= list()so
     #Must be defined separately(See below)
 
        name = NA, #Member variable, no default value
        hair = "black",  #Member variables with default values
         
        initialize = function(name, hair){ 
        #The constructor is"initialize"This is the function named. (
        #Member variable is self$Described in
          self$name <- name  
          self$hair <- hair
        },
         
        #Define method
        set_hair = function(hair) self$hair = hair,
        greet    = function(greet) print(paste(greet," I am ",self$name))
      )
)

The point to note is


public = list( 

        name = NA, 
        hair = "black", 

If you do not set the default value of the member variable that appears in the class like That's not good.

Otherwise, you will get the following error: cannot add bindings to a locked environment

Also, the default value specified here becomes the type of the member variable. If you assign a numeric type with the default value, later in the constructor or method You will not be able to substitute character string types.

Create an instance and try to operate it

#The instance is ClassName$new(arg)I will make it with.
#Let's create a Person instance called Mr. Yamada with red hair.
Yamama_Person <- Person$new("yamada","red")
 
print Yamama_Person #You can see the list of members normally with the print statement
 
 
 #Of class methods"greet"Let me say hello using.
Yamama_Person$greet("hello")
Yamama_Person$greet("good morning")
 
 
 #Manipulate hair color
Yamama_Person$hair #Member variables"hair"Check the initial value of
[1] "red"
 
Yamama_Person$set_hair("blue") #Member variables"hair"To change"set"Exercise the method
Yamama_Person$hair #reconfirmation
[1] "blue"

Click here for how to use the private method


Person <-
  R6Class("Person",
     
    private = list(
     #Private members are private= list()Describe in list format in
     #I will describe it in a List separate from Public
 
      age = NA,
      get_age = function()private$age  #When calling a private variable, self$Not private$Call with
      ),
     
    public = list(
     #Members that can be called from outside the class are public= list()Describe in list format in
     #Both member variables and class methods can be written in this List.
     #Private variables and private methods are private= list()Must be defined separately in(See below)
 
      name = NA,
      hair = "black",
      initialize = function(name, hair, age){
        self$name <- name
        self$hair <- hair
        private$age <- age  #When calling a private method, self$Not private$Call with
      },
       
      set_hair = function(hair) self$hair = hair,
      greet    = function(greet) print(paste(greet," I am ",self$name)),
      show_age = function() private$get_age() -5
    )
     
  )

Recommended Posts

Try using an object-oriented class in R (R6 method)
[Statistics] [R] Try using quantile regression.
Try using Spyder included in Anaconda
Try implementing extension method in python
Try using LevelDB in Python (plyvel)
Build and try an OpenCV & Python environment in minutes using Docker
Illustrate class dependencies in an Xcode project
Try using the Wunderlist API in Python
Try using the Kraken API in Python
Try using the HL band in order
Try cluster analysis using the K-means method
Ajax in Django (using generic class view)
Try to separate Controllers using Blueprint in Flask
Try using the BitFlyer Ligntning API in Python
Try drawing contour plots using matplotlib in OpenFOAM
Try to create an HTTP server using Node.js
Try to imitate Rails' _method parameter in webapp2
Solve simultaneous equations in an instant using Python
Try using ChatWork API and Qiita API in Python
Try using the DropBox Core API in Python
Try using Tkinter
Try using docker-py
Try using cookiecutter
Try using PDFMiner
Try using geopandas
Try using scipy
Try using pandas.DataFrame
Class method static method
Try using django-swiftbrowser
Try using matplotlib
Try using tf.metrics
Try using PyODE
Determine the threshold using the P tile method in python
Try to delete tweets in bulk using Twitter API
Try face detection in real time using a webcam
[Python] Create an infrastructure diagram in 3 minutes using diagrams
How to create an instance of a particular class from dict using __new__ () in python