From edd at debian.org  Tue May 14 15:47:33 2013
From: edd at debian.org (Dirk Eddelbuettel)
Date: Tue, 14 May 2013 08:47:33 -0500
Subject: [Rd] call R function from C code
In-Reply-To: <kmiqmv$mv9$1@ger.gmane.org>
References: <kme57t$sca$1@ger.gmane.org>
	<2DC85D00-8F28-4EFA-BBE7-D5488B1DC894@r-project.org>
	<CADwqtCNZrAsF+j5NCqiAnqz7Go4eGSE5ctw0Hb0nYOjP6vswdA@mail.gmail.com>
	<kmiev2$qj2$1@ger.gmane.org>
	<AD1B2862-34DB-42E4-BCDF-52FCEE80ACC5@r-project.org>
	<kmiqmv$mv9$1@ger.gmane.org>
Message-ID: <20882.16501.602851.955871@max.nulle.part>


If you are fine with another package doing the legwork for you, calling an R
function from C++ is very easy:

R> library(Rcpp)
R> cppFunction('NumericVector fun(NumericMatrix X, NumericVector y, Function s) { return s(X, y); }')
R> set.seed(42); solve(matrix(rnorm(9),3,3), rep(1,3))
[1] -0.778649  1.553893  0.717221
R> set.seed(42); fun(matrix(rnorm(9),3,3), rep(1,3), solve)
[1] -0.778649  1.553893  0.717221
R> 

So the C++ function 'fun' we created using Rcpp, and which just calls the
supplied function on the first two arguments, returns us the same answer from
C++ as we get when we call solve(X, y) in R.

Dirk

-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com



[Rd] call R function from C code
Dirk Eddelbuettel