Pointer mechanism and dynamic fields for object in the Rb-OOP system.
ptR(value) key %<-% value
a character string or an object of class "ptR".
any R object or an object of class "ptR".
Thanks to the environment object provided by the R system, it is possible to easily provide in the R system the notion of pointer. Let us assume that a variable
pt1 is assigned to a new R environment and that the variable
value is initialized to the number 1. This content is accessible by typing the code
pt1$value. Then, a new variable
pt2 assigned to
pt1 contains the variable
value as a list does. But, differently from a list, any change of the content of the variable
value in the environment
pt1 is also updated in the environment
pt2. For example, if one executes
pt1$value <- 2 then
pt2$value leads to the output 2. The dynamic nature of environment is definitely different from the static nature of most of the other objects provided by the R system.
ptR related to the notion of pointer is internally represented as an environment containing only one R variable named
The execution of the code
ptR(value) returns a pointer (i.e. object of class
ptR) with content
value. To directly assign a pointer, the user operator
%<-% is provided.
At the creation, the name of the pointer in its character string has to be supplied in the left-handed side. Once the pointer is created, to update the content of the pointer, it is then possible to directly put the pointer in the left-handed side. If the right-handed side refers to an existing pointer, a replacement of the environment is directly applied as expected. However, this last possibility is better performed by directly assigning a pointer with another pointer.
Let us also underline that the notion of pointer has been introduced to provide the notion of dynamic instance variable in the concept of OOP called R-OOP proposed to mimick Ruby-like OOP in the R system. In a such context, the user does not have to know how a pointer is internally represented.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
## pointer famousName <- ptR("Brown") famousName famousName$value # pointer via the operator "%<-%" "b" %<-% c(3,1:4) # equivalent to: b <- ptR(c(3,1:4)) b b$value # since b is created, you could modified it b %<-% c(b$value,2) # append 2 b # equality means that the content is now dynamic! bb <- b bb bb %<-% 5:2 bb # b has changed too since b and bb represent the same environment. b
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.