21

What is the simplest possible C function for starting the R interpreter, passing in a small expression (eg, 2+2), and getting out the result? I'm trying to compile with MingW on Windows.

1
  • good point... clarified.
    – jsight
    Commented Mar 17, 2010 at 15:39

4 Answers 4

12

You want to call R from C?

Look at section 8.1 in the Writing R Extensions manual. You should also look into the "tests" directory (download the source package extract it and you'll have the tests directory). A similar question was previously asked on R-Help and here was the example:

#include <Rinternals.h> 
#include <Rembedded.h> 

SEXP hello() { 
  return mkString("Hello, world!\n"); 
} 

int main(int argc, char **argv) { 
  SEXP x; 
  Rf_initEmbeddedR(argc, argv); 
  x = hello(); 
  return x == NULL;             /* i.e. 0 on success */ 
} 

The simple example from the R manual is like so:

 #include <Rembedded.h>

 int main(int ac, char **av)
 {
     /* do some setup */
     Rf_initEmbeddedR(argc, argv);
     /* do some more setup */

     /* submit some code to R, which is done interactively via
         run_Rmainloop();

         A possible substitute for a pseudo-console is

         R_ReplDLLinit();
         while(R_ReplDLLdo1() > 0) {
           add user actions here if desired
         }
      */
     Rf_endEmbeddedR(0);
     /* final tidying up after R is shutdown */
     return 0;
 }

Incidentally, you might want to consider using Rinside instead: Dirk provides a nice "hello world" example on the project homepage.

In you're interested in calling C from R, here's my original answer:

This isn't exactly "hello world", but here are some good resources:

1
  • Many thanks... I was missing the R init function calls... oops. The examples from you (and others) have been extremely help (too bad I can't give accepted answers to a few others too... :) ).
    – jsight
    Commented Mar 17, 2010 at 21:20
10

Here you go. It's the main function, but you should be able to adapt it to a more general purpose function. This example builds an R expression from C calls and also from a C string. You're on your own for the compiling on windows, but I've provided compile steps on linux:

 /* simple.c */
 #include <Rinternals.h>
 #include <Rembedded.h>
 #include <R_ext/Parse.h>
 int
 main(int argc, char *argv[])
 {
    char *localArgs[] = {"R", "--no-save","--silent"};
    SEXP e, tmp, ret;
    ParseStatus status;
    int i;

    Rf_initEmbeddedR(3, localArgs);

    /* EXAMPLE #1 */

    /* Create the R expressions "rnorm(10)" with the R API.*/
    PROTECT(e = allocVector(LANGSXP, 2));
    tmp = findFun(install("rnorm"), R_GlobalEnv);
    SETCAR(e, tmp);
    SETCADR(e, ScalarInteger(10));

    /* Call it, and store the result in ret */
    PROTECT(ret = R_tryEval(e, R_GlobalEnv, NULL));

    /* Print out ret */
    printf("EXAMPLE #1 Output: ");
    for (i=0; i<length(ret); i++){
        printf("%f ",REAL(ret)[i]);
    }
    printf("\n");

    UNPROTECT(2);


    /* EXAMPLE 2*/

    /* Parse and eval the R expression "rnorm(10)" from a string */
    PROTECT(tmp = mkString("rnorm(10)"));
    PROTECT(e = R_ParseVector(tmp, -1, &status, R_NilValue));
    PROTECT(ret = R_tryEval(VECTOR_ELT(e,0), R_GlobalEnv, NULL));

    /* And print. */
    printf("EXAMPLE #2 Output: ");
    for (i=0; i<length(ret); i++){
        printf("%f ",REAL(ret)[i]);
    }
    printf("\n");

    UNPROTECT(3);
    Rf_endEmbeddedR(0);
    return(0);
 }

Compile steps:

$ gcc -I/usr/share/R/include/ -c -ggdb simple.c
$ gcc -o simple simple.o  -L/usr/lib/R/lib -lR
$ LD_LIBRARY_PATH=/usr/lib/R/lib R_HOME=/usr/lib/R ./simple
EXAMPLE #1 Output: 0.164351 -0.052308 -1.102335 -0.924609 -0.649887 0.605908 0.130604 0.243198 -2.489826 1.353731
EXAMPLE #2 Output: -1.532387 -1.126142 -0.330926 0.672688 -1.150783 -0.848974 1.617413 -0.086969 -1.334659 -0.313699
1
  • Can it be compiled under VS2008 (MS Compiler)? What are the arguments?
    – Sergey
    Commented May 31, 2012 at 5:22
8

I don't think any of the above has answered the question - which was to evaluate 2 + 2 ;). To use a string expression would be something like:

#include <Rinternals.h>
#include <R_ext/Parse.h>
#include <Rembedded.h>

int main(int argc, char **argv) {
    SEXP x;
    ParseStatus status;
    const char* expr = "2 + 2";

    Rf_initEmbeddedR(argc, argv);

    x = R_ParseVector(mkString(expr), 1, &status, R_NilValue);
    if (TYPEOF(x) == EXPRSXP) { /* parse returns an expr vector, you want the first */
        x = eval(VECTOR_ELT(x, 0), R_GlobalEnv);
        PrintValue(x);
    }

    Rf_endEmbeddedR(0);

    return 0;
}

This lacks error checking, obviously, but works:

Z:\>gcc -o e.exe e.c -IC:/PROGRA~1/R/R-213~1.0/include -LC:/PROGRA~1/R/R-213~1.0/bin/i386 -lR
Z:\>R CMD e.exe
[1] 4

(To get the proper commands for your R use R CMD SHLIB e.c which gives you the relevant compiler flags)

You can also construct the expression by hand if it's simple enough - e.g., for rnorm(10) you would use

SEXP rnorm = install("rnorm");
SEXP x = eval(lang2(rnorm, ScalarInteger(10)), R_GlobalEnv);
3
  • Can it be compiler under Visual Studio (MS compiler)? What are the parameters?
    – Sergey
    Commented May 31, 2012 at 5:24
  • No, VC is not officially supported, the native compiler is MinGW. There are may be ways to combine VC code with R (e.g. through DLLs), but it is not guaranteed to work. Commented Jun 5, 2012 at 23:49
  • I've compiled the code under Visual Studio (MS Compiler) :). To do so, the R.lib library should be created manually.
    – Sergey
    Commented Jun 28, 2012 at 18:13
3

I think you can't do much better than the inline package (which supports C, C++ and Fortran):

library(inline)
fun <- cfunction(signature(x="ANY"), 
                 body='printf("Hello, world\\n"); return R_NilValue;')
res <- fun(NULL)

which will print 'Hello, World' for you. And you don't even know where / how / when the compiler and linker are invoked. [ The R_NilValue is R's NULL version of a SEXP and the .Call() signature used here requires that you return a SEXP -- see the 'Writing R Extensions' manual which you can't really avoid here. ]

You will then take such code and wrap it in a package. We had great success with using inline for the Rcpp unit tests (over 200 and counting now) and some of the examples.

Oh, and this inline example will work on any OS. Even Windoze provided you have the R package building tool chain installed, in the PATH etc pp.

Edit: I misread the question. What you want is essentially what the littler front-end does (using pure C) and what the RInside classes factored-out for C++.

Jeff and I never bothered with porting littler to Windoze, but RInside did work there in most-recent release. So you should be able to poke around the build recipes and create a C-only variant of RInside so that you can feed expression to an embedded R process. I suspect that you still want something like Rcpp for the clue as it gets tedious otherwise.

Edit 2: And as Shane mentions, there are indeed a few examples in the R sources in tests/Embedding/ along with a Makefile.win. Maybe that is the simplest start if you're willing to learn about R internals.

4
  • True, but that is C++... I'd rather a C example for the moment. Rcpp looks very simple to use, though.
    – jsight
    Commented Mar 17, 2010 at 15:57
  • Yes, I edited accordingly. Inline works fine without Rcpp though. But once you actually want to exchange data, I'd go Rcpp even though you may feel that C++ is more complex than C. It doesn't have to be. Commented Mar 17, 2010 at 16:02
  • 1
    That's interesting, but the opposite of what I am looking for. I am interested in calling R from (possibly legacy) C code, not the other way around... I'm sorry if the original question wasn't clear. This info is helpful too, though. :)
    – jsight
    Commented Mar 17, 2010 at 16:35
  • Oh, I misunderstood. In that case, try 'littler' and 'RInside' and I will amend my answer. Commented Mar 17, 2010 at 16:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.