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 8800873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:42:14+00:00 2026-06-14T00:42:14+00:00

I have come across some behaviour from R reference classes I would like to

  • 0

I have come across some behaviour from R reference classes I would like to work around. In the following code, reference class B has two fields of reference class A in it.

These fields in B appear to be instantiated (possibly twice) with a zero-argument (default) versions of reference class A before B’s initialize() method is called. These instances are then replaced with the correct versions of instance A during B’s initialization process. The problem is that if I use lock() from B’s instance generator, the initial empty instantiation’s of A cannot be replaced in B. Another problem is that reference class A needs a default value in initialize [or a missing(c) test].

Help – suggestions – etc. appreciated.

A <- setRefClass('A',
    fields = list(
        count = 'numeric'
    ),

    methods = list(
        initialize = function (c=0) {
            cat('DEBUG: A$initialize(c); where c=');  cat(c); cat('\n')
            count <<- c
        }
    )
)

instance.of.A <- A$new(10)
str(instance.of.A)

B <- setRefClass('B',
    field = list(
        a = 'A',
        b = 'A'
    ),

    methods = list(
        initialize = function(c) {
            a <<- instance.of.A
            b <<- getRefClass('A')$new(c)
        }
    )
)

instance.of.b <- B$new(100)
str(instance.of.b)
  • 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-14T00:42:15+00:00Added an answer on June 14, 2026 at 12:42 am

    Drawing on the above, the solution I am using is (a little long because of the type checking):

    A <- setRefClass('A',
        fields = list(
            count = function(x) {
                if (!missing(x)) {
                    if(class(x) != 'numeric')
                        stop('Class A: count set by non-number')
                    .self$count.private <- x
                }
                .self$count.private
            }
        ),
    
        methods = list(
            initialize = function (c=0) {
                cat('DEBUG: A$initialize(c); where c=');  cat(c); cat('\n')
                count <<- c
            }
        )
    )
    
    instance.of.A <- A$new(10)
    str(instance.of.A)
    
    B <- setRefClass('B',
        field = list(
            a = function(x) {
                if (!missing(x)) {
                    if(!inherits(x, 'envRefClass') || class(x)[1] != 'A')
                        stop('Class B: expecting instance of class A')
                    .self$a.private <- x
                }
                .self$a.private
            },
            b = function(x) {
                if (!missing(x)) {
                    if(!inherits(x, 'envRefClass') || class(x)[1] != 'A')
                        stop('Class B: expecting instance of class A')
                    .self$b.private <- x
                }
                .self$b.private
            }
        ),
    
        methods = list(
            initialize = function(c) {
                a <<- instance.of.A
                b <<- getRefClass('A')$new(c)
            }
        )
    )
    
    instance.of.b <- B$new(100)
    str(instance.of.b)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have taken over some code from a previous developer and have come across
I'm moving some code from Microsoft.Practices.EnterpriseLibrary.Validation.Validators to System.ComponentModel.DataAnnotations, and have come across a more
I have come across some legacy code that has the following type of line:
I am currently refactoring some code for work and I have come across some
I have come across some strange behaviour, and I'm assuming a bug in Firefox,
I have come across an annoying problem while writing some PHP4 code. I renamed
I have come across some java classes used in the spring framework. First, there
I have come across some legacy code that has raised all my heckles as
I have come across some code in a few projects which use if(Is.NotNull(SomeObject)) instead
I have a class called AbstractRManagers which i would like to inheritent from a

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.