Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8255943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:42:05+00:00 2026-06-08T01:42:05+00:00

I am a python programmer, and this is my first day working with R.

  • 0

I am a python programmer, and this is my first day working with R.

I am trying to write a class with a constructor and three methods, and I am struggling.

In python, it’s easy:

 class MyClass:
      def __init__(self):
           self.variableA = 1
           self.variableB = 2

      def hello(self):
           return "Hello"

      def goodbye(self):
           return "Goodbye"

      def ohDear(self):
           return "Have no clue"

I can’t find anything that shows me how to do something as simple as this in R. I would appreciate it if someone could show me one way to do this ?

Many thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T01:42:08+00:00Added an answer on June 8, 2026 at 1:42 am

    R actually has lots of different object oriented implementations. The three native ones (as mentioned by @jthetzel) are S3, S4 and Reference Classes.

    S3 is a lightweight system that allows you to overload a function based upon the class of the first argument.

    Reference Classes are designed to more closely resemble classes from other programming languages. They more or less replace S4 classes, which do the same thing but in a more unwieldy fashion.

    The R.oo package provides another system, and the proto package allows prototype-oriented programming, which is like lightweight OOP. There was a sixth system in the OOP package, but that is now defunct. The more recent R6 package “is a simpler, faster, lighter-weight alternative to R’s built-in reference classes”.

    For new projects, you’ll usually only want to use S3 and Reference classes (or possibly R6).

    python classes most easily translate to reference classes. They are relatively new and (until John Chambers finishes his book on them) the best reference is the ?ReferenceClasses page. Here’s an example to get you started.

    To define a class, you call setRefClass. The first argument is the name of the class, and by convention this should be the same as the variable that you assign the result to. You also need to pass lists to the arguments “fields” and “methods”.

    There are a few quirks.

    • If you don’t want to specify what variable type a field should have, pass “ANY” as the value in the list of fields.
    • Any constructor logic needs to be in an optional function called initialize.
    • If the first line of a method is a string, it is interpreted as documentation for that method.
    • Inside a method, if you want to assign to a field, use global assignment (<<-).

    This creates a class generator:

    MyClass <- setRefClass(
      "MyClass",
      fields = list(
        x = "ANY",
        y = "numeric",
        z = "character"
      ),
      methods = list(
        initialize = function(x = NULL, y = 1:10, z = letters)
        {
          "This method is called when you create an instance of the class."
          x <<- x
          y <<- y
          z <<- z
          print("You initialized MyClass!")
        },
        hello = function()
        {
          "This method returns the string 'hello'."
          "hello"
        },
        doubleY = function()
        {
          2 * y
        },
        printInput = function(input)
        {
          if(missing(input)) stop("You must provide some input.")
          print(input)
        }
      )
    )
    

    Then you create instances of the class by calling the generator object.

    obj1 <- MyClass$new()
    obj1$hello()
    obj1$doubleY()
    
    obj2 <- MyClass$new(x = TRUE, z = "ZZZ")
    obj2$printInput("I'm printing a line!")
    

    Further reading: the OO field guide chapter of Advanced R.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a C/Python programmer in C++ land working with the STL for the first
I am a python programmer looking to make my first mobile app. I'd like
I am mainly a Python programmer. Now I am trying to learn C# .NET.
I'm still a neophyte Python programmer and I'm trying to do something that is
First, I'm not a python programmer. I'm an old C dog that's learned new
first of all: I am not a python programmer, i just want to run
I'm a python programmer getting to learn C from the K&R book. This will
First, I did check this post but it is in Python, first, and second
I'm a self-taught programmer who jumped into Python as my first language about 7-8
First of all I am a programmer (Python, C, Java, Delphi, whatever) and not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.