Description Usage Arguments Details Value Note References Examples
Show how to create C++ objects with external pointers from R which are deleted automatically from R.
1 2 3 4 |
str |
Character string. |
p |
Pointer to Foo object. |
This package shows how to create C++ objects from R which are stored in memory until they are deleted from R.
The idea is to be able to call methods of the C++ class before freeing the object from memory. createFoo
create an object of class Foo. getStrFoo
returns the string stored in Foo. setStrFoo
set the
string in Foo. .closeFoo(p)
is the internal function used to free the object from memory.
Have a look in the src folder to see how the C and C++ code are made.
The test package is inspired by the “Interfacing C++ code” section in the R extensions manual [1] and Luke Tierney's page about simple references with finalization [2].
createFoo
returns a pointer to the object of class Foo created in memory.
getStrFoo
returns the string stored in Foo.
setStrFoo
returns NULL.
.closeFoo(p)
return NULL.
Use the extension .cc and .hh for C++ files in the src folder. If you use the extension .c and .h the source code will be compiled using gcc instead of g++!
[1] R Development Core Team. Writing R Extensions. Version 2.8.1 (2008-12-22)
R Foundation for Statistical Computing, Vienna, Austria. ISBN 3-900051-11-9,
http://www.R-project.org.
[2] Simple References with Finalization http://www.stat.uiowa.edu/~luke/R/simpleref.html.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | # Creating an object
p<-createFoo("Testing 1")
getStrFoo(p)
setStrFoo(p,"Testing 2")
getStrFoo(p)
trace(.closeFoo)
rm(p) # remove the object when the garbage collector is called
gc()
# Creating two objects
p1<-createFoo("Testing 1")
p2<-createFoo("Testing 2")
getStrFoo(p1)
getStrFoo(p2)
rm(p1)
rm(p2)
gc()
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.