[R] simple example of C interface to R

Huntsinger, Reid
Tue, 08 Feb 2005 21:19:10 -0000

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!