[R] again, a question between R and C++

Whit Armstrong whit at twinfieldscapital.com
Thu Jul 21 14:25:25 CEST 2005


Jordi,

The place to ask this question is probably the r-devel list; it's a
little too heavy for r-help.

This is fairly easy to do using the .Call interface.

Have a look at lapply2 in the Writing R Extensions manual.
http://cran.r-project.org/doc/manuals/R-exts.html#Evaluating-R-expressio
ns-from-C

or just follow this short example.


write a function in C++ as follows:
 
SEXP myFunc(SEXP list1, SEXP list2, SEXP list3, SEXP list4, SEXP
intID_SEXP) {

	// obtain the list length as follows:
	int list1_len = length(list1);

	// to access your integer (I assume it's a scalar, not a vector)
	// you need to grab the first element of this integer vector
	int INTEGER(intID_SEXP)[0]

	// you will want to add some checks to make sure the arguments
are of the right type
	
	...
	...

	SEXP ans = (whatever)
	return ans;
}

you can call it in R as follows:

.Call("myFunc", list1, list2, list3, list4, intID)



-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Molins, Jordi
Sent: Thursday, July 21, 2005 3:25 AM
To: r-help at stat.math.ethz.ch
Cc: Jordi Molins
Subject: [R] again, a question between R and C++


Dear R Users,

I want to make a call from R into C++. My inputs are List1, List2,
List3, IntegerID. The amount of elements of the lists and their type
depend on IntegerID. Typical elements of a given list can be vectors,
doubles, and even other lists. I want to return also a list (whose
nature will depend also, possibly, on IntegerID).

What I want to do is to call these 4 inputs from C++ and then use a
factory pattern (depending on IntegerID) that will perform different
calculations on the lists depending on the IntegerID (of course, I could
also do this with a simple switch statement).

I have been reading the documentation, especially the one regarding
.Call and .External, and it seems that my algorithm could be
implemented, but the examples I have seen up to now are such that what
occupies the place of my lists are just vectors (like in convolve4
example).

Is there an example where I could see how instead of a vector, a set of
lists (with an unkown number of arguments, as well as unkown types) are
used as inputs? I guess that the ideal would be that in the equivalent
of the
convolve4 function, my args would be "variant" type of lists, and then,
after the factory pattern is called, and the correct class is registered
(via IntegerID), this variant type is really "decomposed" into the
individual types that compose the list (ie, vectors, doubles, ...). Of
course, in the factory there should be as many "decomposing" algorithms
as IntegerIDs, each creating a particular decomposition.

Also, how returning a list (whose nature will depend also, possibly, on
IntegerID) should be handled?

Thank you in advance

Jordi



------------------------------------------------------------------------
--------
The information contained herein is confidential and is\ int...{{dropped}}




More information about the R-help mailing list