Hi all! I read R Extensions manual. But still I am not sure how to call R functions from C. Would any of you give me a sample C code to show how to call R functions - for instance, time series functions - from C in the embedded way of C code? [[alternative HTML version deleted]]
On Tue, 23 Nov 2010 01:24:04 +0900, ???<wjlee2002 at naver.com> wrote:> Hi all! > I read R Extensions manual. > But still I am not sure how to call R functions from C. > Would any of you give me a sample C code to show how to call R functions-> for instance, time series functions - from C in the embedded way of Ccode? Look at package xts, among many others, for examples (xts is one of the time series classes that makes extensive use of C code). - Brian -- Brian G. Peterson http://braverock.com/brian/ Ph: 773-459-4973 IM: bgpbraverock
Le 22/11/10 17:24, ??? a ?crit :> > Hi all! > I read R Extensions manual. > But still I am not sure how to call R functions from C. > Would any of you give me a sample C code to show how to call R functions - for instance, time series functions - from C in the embedded way of C code?As Brian said, there are plenty of examples to follow. The basic idea is to create a call and evaluate it. txt <- ' #include <R.h> #include <Rdefines.h> SEXP callrnorm(){ SEXP call = PROTECT( lang2( install( "rnorm"), ScalarInteger(10) ) ) ; SEXP res = PROTECT( eval( call, R_GlobalEnv ) ) ; UNPROTECT(2) ; return res ; } ' writeLines( txt, "test.c" ) system( "R CMD SHLIB test.c" ) dyn.load( "test.so" ) .Call( "callrnorm" ) or using the inline package : require(inline) fx <- cfunction( , ' SEXP call = PROTECT( lang2( install( "rnorm"), ScalarInteger(10) ) ) ; SEXP res = PROTECT( eval( call, R_GlobalEnv ) ) ; UNPROTECT(2) ; return res ; ' , verbose = TRUE) fx() And now for the Rcpp plug ;-) calling R functions is dramatically easy with Rcpp with the Rcpp::Function class. require(inline) fx <- cxxfunction( , ' // grab the function Function rnorm("rnorm") ; // call it return rnorm(10, _["sd"] = 10) ; ' , plugin = "Rcpp" ) fx() Romain -- Romain Francois Professional R Enthusiast +33(0) 6 28 91 30 30 http://romainfrancois.blog.free.fr |- http://bit.ly/9VOd3l : ZAT! 2010 |- http://bit.ly/c6DzuX : Impressionnism with R `- http://bit.ly/czHPM7 : Rcpp Google tech talk on youtube
On Tue, 23 Nov 2010 01:24:04 +0900, ???<wjlee2002 at naver.com> wrote:> Hi all! > I read R Extensions manual. > But still I am not sure how to call R functions from C. > Would any of you give me a sample C code to show how to call R functions-> for instance, time series functions - from C in the embedded way of Ccode? Dirk indicates that I may have misunderstood your question. Perhaps this will help. http://quantitative-ecology.blogspot.com/2008/11/call-c-from-r-and-r-from-c.html If you need further help, please provide a minimal self-contained example of what you're trying to do. - Brian -- Brian G. Peterson http://braverock.com/brian/ Ph: 773-459-4973 IM: bgpbraverock
Thank you!
I tried to build a C code below, and I got a error message.
Thank you for your help in advance.
<C code>
#include "embeddedRCall.h"
int main(int argc, char *argv[])
{
eval_R_command("print", argc, argv);
return(0);
}
========================================
<error message>
--------------------Configuration: Rtest - Win32 Debug--------------------
Compiling...
Rtest.c
Linking...
Rtest.obj : error LNK2001: unresolved external symbol _eval_R_command
Debug/Rtest.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
Rtest.exe - 2 error(s), 0 warning(s)
--
View this message in context:
http://r.789695.n4.nabble.com/How-to-call-R-from-C-tp3053943p3054977.html
Sent from the R devel mailing list archive at Nabble.com.