[R] embedding R?

Aaron J Mackey ajm6q at virginia.edu
Sun Feb 11 18:47:04 CET 2001

My apologies if this is a FAQ, I searched the mailing list archives before

Background: I am a long time user of SPlus, and a recent user of R.  My
work normally involves converting the raw output of something interesting
into data to be analyzed in S/R, for which I use Perl extensively.  I then
import the data into S/R, perform the analyses I need, dumping the values
into a new file, further Perl processing, eventual database storage.

I've recently become acquainted with a new Perl module called "Inline"
which allows you to embed C/C++/Python/etc code within your Perl program
and use those functions as part of your regular perl program.  Briefly,
Inline is taking care of all the code creation, compiling and dynamic
loading necessary to make this work "transparently".  I am hoping to write
a similar "filter" for R, and so have just begun to delve into the R api
(remember, I've only been a command-line/batch script user previously).

I see that I can write extensions in C/Fortran, which can be dynamically
loaded into R, and that the extension code can access R functions and
data.  But that's all still running under R.  What about a program that
dynamically loads R itself (trivial example follows):

#include <stdio.h>
#include <R.h>

#define MAXNUM = 100;

int main(int argc, char **argv) {

    int n;
    int i[MAXNUM];

    for(n = 1 ; n < argc && n <= MAXNUM ; n++) {
        i[n-1] = atoi(argv[n]);

    printf("mean: %4.2f\n", R_mean(i, n - 1));

% gcc -lR mean.c -o mean
% mean 4 5 6 7
mean: 5.5
% exit

Where "R_mean" is calling the R function mean() ???

Any help is appreciated.  And I'm not going to worry about graphical
functions just yet.  Just need a pointer or two into the right piece of


r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list