i'd like to use the C interface to R in a program i'm writing. as a starting point, i'm trying to create a very simple C program that uses R. i've read the R documentation on this, but i'm having trouble figuring out where SEXP is defined and how to use it. i noticed someone else on this list also tried to use the C interface, but they ran into similar problems: http://maths.newcastle.edu.au/~rking/R/help/03b/1942.html could someone show me a simple example of how to use the R interface to C? thank you, jason dunsmore
See if this helps: http://www.ci.tuwien.ac.at/Conferences/useR-2004/Keynotes/Dalgaard.pdf Andy> From: jbdunsmo at utmb.edu > > > i'd like to use the C interface to R in a program i'm writing. as a > starting point, i'm trying to create a very simple C program that uses > R. i've read the R documentation on this, but i'm having trouble > figuring out where SEXP is defined and how to use it. > > i noticed someone else on this list also tried to use the C interface, > but they ran into similar problems: > http://maths.newcastle.edu.au/~rking/R/help/03b/1942.html > > could someone show me a simple example of how to use the R interface > to C? > > thank you, > jason dunsmore > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >
On Fri, 4 Feb 2005 jbdunsmo at utmb.edu wrote:> i'd like to use the C interface to R in a program i'm writing. as a > starting point, i'm trying to create a very simple C program that uses > R. i've read the R documentation on this, but i'm having trouble > figuring out where SEXP is defined and how to use it. > > i noticed someone else on this list also tried to use the C interface, > but they ran into similar problems: > http://maths.newcastle.edu.au/~rking/R/help/03b/1942.html > > could someone show me a simple example of how to use the R interface > to C? >Well, it is documented in the Writing R Extensions manual: http://cran.r-project.org/doc/manuals/R-exts.html#System-and-foreign-language-interfaces and in the source code of many contributed packages - they may give you good examples - look in their src/ directories. But DO read the manual, please, that's what it is for!> thank you, > jason dunsmore > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >-- Roger Bivand Economic Geography Section, Department of Economics, Norwegian School of Economics and Business Administration, Breiviksveien 40, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 93 93 e-mail: Roger.Bivand at nhh.no
On Fri, Feb 04, 2005 at 09:09:37PM +0100, Roger Bivand wrote:> > Well, it is documented in the Writing R Extensions manual: > > http://cran.r-project.org/doc/manuals/R-exts.html#System-and-foreign-language-interfaces >thanks. reading through that a second time made all the difference. i still think a complete example (a very simple C program that uses R and compiles without errors) would be good to have in the documentation. jason
Hi, -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of jbdunsmo at utmb.edu Sent: Friday, February 04, 2005 10:40 PM To: Roger Bivand Cc: r-help at stat.math.ethz.ch Subject: Re: [R] simple example of C interface to R On Fri, Feb 04, 2005 at 09:09:37PM +0100, Roger Bivand wrote:> > Well, it is documented in the Writing R Extensions manual: > >http://cran.r-project.org/doc/manuals/R-exts.html#System-and-foreign-lan guage-interfaces>thanks. reading through that a second time made all the difference. i still think a complete example (a very simple C program that uses R and compiles without errors) would be good to have in the documentation. jason do you know already the page of Roger D. Peng? He has a document entitled "An Introduction to the .C Interface to R". It is located at: http://www.biostat.jhsph.edu/~rpeng/docs/interface.pdf Does this help you? Best, Roland +++++ This mail has been sent through the MPI for Demographic Rese...{{dropped}}
Rtest.o is just an object file, and the second call to gcc is really a call to ld and creates Rtest.so, a shared library. Neither is executable. Probably you want to compile Rtest.c into an executable; it does have the "main" function after all. Perhaps gcc -L/usr/local/lib -o Rtest Rtest.o -L/usr/lib/R/bin -lR would work. Reid Huntsinger -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of jbdunsmo at utmb.edu Sent: Tuesday, February 08, 2005 12:14 PM To: Rau, Roland; r-help at stat.math.ethz.ch Subject: Re: [R] simple example of C interface to R On Tue, Feb 08, 2005 at 10:55:01AM +0100, Rau, Roland wrote:> > do you know already the page of Roger D. Peng? > He has a document entitled "An Introduction to the .C Interface to R". > It is located at: > http://www.biostat.jhsph.edu/~rpeng/docs/interface.pdf >thanks. that's a nice tutorial, but what i'd really like to do is write a program with R embedded in it (and not have to load the interpreter). i found some documentation for doing that here: http://cran.r-project.org/doc/manuals/R-exts.html#Embedding-R-under-Unix_002 dalikes now i'm going through the tests/Embedding directory (included with the R source distribution). i've gotten Rtest.c to compile with: $ R CMD SHLIB Rtest.c gcc -I/usr/lib/R/include -I/usr/local/include -D__NO_MATH_INLINES -mieee-fp -fPIC -march=pentium4 -O3 -pipe -fomit-frame-pointer -c Rtest.c -o Rtest.o gcc -shared -L/usr/local/lib -o Rtest.so Rtest.o -L/usr/lib/R/bin -lR then when i try to run Rtest.o, i get: -/bin/bash: ./Rtest.o: cannot execute binary file Rtest.c contains: #include "embeddedRCall.h" int main(int argc, char *argv[]) { eval_R_command("print", argc, argv); return(0); } embeddedRCall.h contains: #ifndef EMBEDDED_R_CALL_H #define EMBEDDED_R_CALL_H #include <Rinternals.h> int eval_R_command(const char *funcName, int argc, char *argv[]); SEXP Test_tryEval(SEXP expression, int *errorOccurred); void init_R(int argc, char **argv); #endif i also get the "cannot execute binary file" error when trying to compile and run the following code from the documentation: int Rf_initEmbeddedR(int argc, char **argv) { /* This is already compiled into R */ Rf_initialize_R(argc, argv); setup_Rmainloop(); return(1); } int main(int ac, char **av) { /* do some setup */ // Rf_initEmbeddedR(argc, argv); /* do some more setup */ char *argv[]= {"REmbeddedPostgres", "--gui=none", "--silent"}; Rf_initEmbeddedR(sizeof(argv)/sizeof(argv[0]), argv); /* submit some code to R, which is done interactively via run_Rmainloop(); */ end_Rmainloop(); return 0; } what am i doing wrong? ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html