Use of and Using R as an Object Oriented Language John James

Slides:



Advertisements
Similar presentations
Agenda Definitions Evolution of Programming Languages and Personal Computers The C Language.
Advertisements

Python Objects and Classes
Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
Road Map Introduction to object oriented programming. Classes
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
Algorithms and Problem Solving-1 Algorithms and Problem Solving.
Algorithms and Problem Solving. Learn about problem solving skills Explore the algorithmic approach for problem solving Learn about algorithm development.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
ASP.NET Programming with C# and SQL Server First Edition
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
The chapter will address the following questions:
18-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming in Java Topic : Objects and Classes (cont)
1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming.
A First Program Using C#
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
© Janice Regan, CMPT 128, Jan CMPT 128 Introduction to Computing Science for Engineering Students Creating a program.
Introduction to Object-oriented programming and software development Lecture 1.
An Object-Oriented Approach to Programming Logic and Design
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
The Java Programming Language
CSCI-383 Object-Oriented Programming & Design Lecture 13.
CSE 219 Computer Science III Program Design Principles.
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Developing an Algorithm
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
Object-Oriented Programming. An object is anything that can be represented by data in a computer’s memory and manipulated by a computer program.
P Chapter 2 introduces Object Oriented Programming. p OOP is a relatively new approach to programming which supports the creation of new data types and.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Chapter 12 Object Oriented Design.  Complements top-down design  Data-centered view of design  Reliable  Cost-effective.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Java The Java programming language was created by Sun Microsystems, Inc. It was introduced in 1995 and it's popularity has grown quickly since A programming.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Designing Classes CS239 – Jan 26, Key points from yesterday’s lab  Enumerated types are abstract data types that define a set of values.  They.
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
Introduction to Object-Oriented Programming Lesson 2.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
More about Java Classes Writing your own Java Classes More about constructors and creating objects.
1 CSE Programming in C++. 2 Overview Sign roster list Syllabus and Course Policies Introduction to C++ About Lab 1 Fill Questionnaire.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
CIT 590 Intro to Programming Lecture 10 (object oriented programming)
1.1: Objects and Classes msklug.weebly.com. Agenda: Attendance Let’s get started What is Java? Work Time.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
What is an object?. What Makes an Object? An object has identity (it acts as a single whole). Every object has a name that identifies what it is. Ex.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 26, 2009.
ENCAPSULATION. WHY ENCAPSULATE? So far, the objects we have designed have all of their methods and variables visible to any part of the program that has.
Object Oriented Programming Some Interesting Genes.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Programming paradigms
Lecture 1 Introduction Richard Gesick.
Chapter 3: Using Methods, Classes, and Objects
About the Presentations
Computer Programming.
Object-Oriented Programming
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Object-Oriented Programming
Object-Oriented Programming
C++ Object Oriented 1.
Presentation transcript:

Use of and Using R as an Object Oriented Language John James

Why? Encapsulation –It is good to think in nouns and not verbs. Thinking in nouns forces a definition of what are the concepts of the problem. Verbs are about solutions. Run from these. Abstraction –The more generally we think about a problem the more we see its actual structure. The initial view we have of a problem is generally wrong and often muddled. Much problem solving is about determining the correct view to solve it.

Object Orientation Object-oriented programming is a style of programming that has become popular in recent years. Much of the popularity comes from the fact that it makes it easier to write and maintain complicated systems. It does this through several different mechanisms. Central to any object-oriented language are the concepts of class and of methods. A class is a definition of an object. Typically a class contains several slots that are used to hold class-specific information. An object in the language must be an instance of some class. Programming is based on objects or instances of classes. Computations are carried out via methods. Methods are basically functions that are specialized to carry out specific calculations on objects, usually of a specific class. This is what makes the language object oriented. In R, generic functions are used to determine the appropriate method. The generic function is responsible for determining the class of its argument(s) and uses that information to select the appropriate method.

S3 Generic Methods Users can easily write their own methods and generic functions. A generic function is simply a function with a call to UseMethod. A method is simply a function that has been invoked via method dispatch. This can be as a result of a call to either UseMethod or NextMethod. It is worth remembering that methods can be called directly. That means that they can be entered without a call to UseMethod having been made and hence the special variables.Generic,.Class and.Method will not have been instantiated. In that case the default rules detailed above will be used to determine these. The most common use of generic functions is to provide print and summary methods for statistical objects, generally the output of some model fitting process. To do this, each model attaches a class attribute to its output and then provides a special method that takes that output and provides a nice readable version of it. The user then needs only remember that print or summary will provide nice output for the results of any analysis

S4 Generic Methods setGeneric(name, def=, group=list(), valueClass=character(), where=, package=, signature=, useAsDefault=, genericFunction=, simpleInheritanceOnly = ) When a call to a generic function is evaluated, a method is selected corresponding to the classes of the actual arguments in the signature. s information about the actual classes. setMethod(f, signature=character(), definition, where = topenv(parent.frame()), valueClass = NULL, sealed = FALSE)

C++ Templates Function templates are special functions that can operate with generic types. This allows us to create a function template whose functionality can be adapted to more than one type or class without repeating the entire code for each type. template myType GetMax (myType a, myType b) { return (a>b?a:b); }

C++ Templates Class Templates are used where we have multiple copies of code for different data types with the same logic.. template class MyQueue { std::vector<Tdata; public: void Add(T const &d); void Remove(); void Print(); };

What’s the problem? Easy to create a class in R and encapsulate methods, and data setClass("S1", representation(X ="numeric", I="integer")) [1] "S1" inst1 <- new("S1", x=pi, i=as.integer(12)) inst2 <- new("S1", x=log(10), i=as.integer(4)) Default methods and functions need to be separately defined inst1[1] Error in inst1[1] : object of type 'S4' is not subsettable

Code Generators method.skeleton("show", "s1") setMethod("show", signature(object = "s1"), function (object) { stop("Need a definition for the method here") } )

Example data <s1 xmlns:xsi=" XMLSchema-instance">

XML Package > s1 <- xmlRoot(xmlTreeParse('c:/My Eclipse/rplate/rplate/inst/xml/s1.xml')) > xmlName(s1) [1] "s1" > xmlChildren(s1)[[1]] > makeClassTemplate(xmlChildren(s1)[[1]], types='numeric') $name [1] "x" $slots text "ANY" $def [1] "setClass('x',\n representation('text' = 'ANY'))" $coerce [1] "setAs('XMLAbstractNode', 'x', function(from) xmlToS4(from))" > > new('x') An object of class "x" Slot "text": NULL

Understand the data

rplate extension Work from the xsd

Using templates require(rplate) XSD <- xmlRoot(xmlTreeParse('c:/My Eclipse/rplate/rplate/inst/xml/S1.xsd')) xsdTemplate(node=xmlChildren(XSD)[[1]]) xsdTemplate(node=xmlChildren(XSD)[[2]]) xsdTemplate(node=xmlChildren(XSD)[[3]]) s <- new('S1', Real=new('X', text=3.14)) > s An object of class "S1" list() Slot "Real": An object of class "X" list() Slot "text": [1] 3.14 Slot "Int": An object of class "I" list() > S4Toxml(s) 3.14

Conclusions It is both useful and practical to generate R classes from a data definition Templates reduces the volume of R code that has the be defined. Checking of methods and programming is easier, as it produces readable code: the unreadable part is generated from data description developed within common tools Need: always use ‘setters’ (get, set) and (try to) hide