Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R - C interface: extracting an object from an environment

Tags:

c

r

Say that you pass an environment R object to an internal C routine through the .Call interface. Said enviromnent has (by design) a someObject object which I want to extract and manipulate from the C side. How to do it?

To simplify my question, I just want to write a C function that returns someObject. Like this:

en <- new.env()
en$someObject <- someValue
.Call("extractObject",en)
#the above should return en$someObject

Guess the C code should then look something like

SEXP extractObject(SEXP env) {
   return SOMEMACROORFUNCTION(env, "someObject");
}

Unfortunately, I was not able to find the real SOMEMACROORFUNCTION.

like image 305
nicola Avatar asked Nov 25 '25 11:11

nicola


1 Answers

After a little bit of googling and searching, I've found the solution:

findVar(install("someObject"),env)

in the C code is basically the equivalent of get("someObject",env) in R.

like image 181
nicola Avatar answered Nov 27 '25 02:11

nicola



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!