[Rd] Reference Class import() behaviour

Simon Potter spot023 at aucklanduni.ac.nz
Fri Apr 13 03:56:58 CEST 2012


Dear All,

In a project I've been working on we've been using Reference Classes
and grid extensively. However, something that I have come across is
that when using the import() method on refclass objects, it does not
work as expected with grid grobs and viewports.

I have prepared test cases that illustrate the point but the general
idea is that importing appears to work fine for everything except
viewports and grid grobs.

Code to illustrate the point follows.

# This first case is an example to show that the importing process
# I have been using works for simple cases
test <- setRefClass("classTest", fields = c("nums", "testlist"),
                    methods = list(
    initialize = function(nums = 1:10, testlist = NULL) {
        nums <<- nums
        testlist <<- testlist
    },
    view = function() {
        print(nums)
        print(testlist)
    }))

test2 <- setRefClass("classTest2", contains = "classTest")

t1 <- test$new(11:20, list(test = "hello, world!"))
t1$view()
t2 <- test2$new()
t2$view()
t2$import(t1) # Now lets grab t1's nums and list
t2$view()


# Now observe that when we start dealing with grid grobs and viewports
# the importing process breaks.
a.gen <- setRefClass("classA", fields = c("vps", "gt", "nums"),
                 methods = list(
    initialize = function(vps = NULL, gt = NULL, nums = 1:10) {
        vps <<- vps
        gt <<- gt
        nums <<- nums
    },
    view = function() {
        print(vps)
        print(gt)
        print(nums)
    }))

b.gen <- setRefClass("classB", contains = "classA")

library(grid)
my.vps <- vpTree(viewport(name = "parent"),
                 vpList(viewport(name = "child")))
my.gtree <- gTree(name = "tree",
                  children = gList(rectGrob(vp = "parent"),
                                   textGrob("test", vp = "child")))

a <- a.gen$new(vps = my.vps, gt = my.gtree, nums = 11:20)
b <- b.gen$new()
a$view()
b$view()
# Everything works as expected so far...

# Now lets try importing the fields from a into b
# Note that viewports and gTrees break the import process.
# Numbers and lists work fine, as in the example above.
b$import(a)
b$view()

# A workaround is to perform manual assignment
b$vps <- a$vps
b$gt <- a$gt
b$nums <- a$nums
b$view() # Desired result

I'm not sure what's causing this, or whether I'm going about this in
the wrong way but any assistance would be much appreciated.

-- Simon



More information about the R-devel mailing list