Aaron J Mackey wrote:
> Oh sure, you can use Inline::C to write C (which calls the R library), but
> I want to use Inline::R to write R.

Of course. 

I was just saying that Inline::C was an easy environment for playing
around with libR.so, while you're trying to piece things together. And I
was giving you an example to get started with. 

Basically this *is* embedding R (and Perl) in C. I picked an easy
function that didn't require any typemapping. Once you start defining
the appropriate R typemaps, you're on your way to having Inline::R.

Exposing an R_eval is useful. Python exposes a Py_eval. But I really
think you want to write the C code to convert a lower level. 

Instead of the representing R objects as array refs, you could define
them as R objects in Perl. You could have Inline::R export an 'R' class
that defined methods for the R objects. The object would hold on to a C
pointer as a read-only int. (See OO example from the C-Cookbook)

    use Inline R;

    my $r = R->new('list_obj');

    $r->set(1..10);

    $r->my_R_function('options');

    print map {"$_\n"} $r->get();

    __END__
    __R__

    # define my_R_function here

It seems that defining an R class would give you much more flexibility,
since it could represent different R data types.

Godspeed, Brian

PS I'm trying to figure out how to R_eval from a Perl string. Try to
make this work if you can.

----8<----
use strict;
my $PREFIX;
BEGIN {$PREFIX='/usr/local'}

use Inline C => DATA => 
           LIBS => "-L$PREFIX/lib/R/bin -lR",
           INC => "-I$PREFIX/lib/R/include/",
           PREFIX => 'my_';

R_init();
R_eval('print("Hello, world")');

__END__
__C__
#include "R.h"
#include "Rinternals.h"

void my_R_init() {
    char *argv[] = {"R_Embedded_Inline", "--silent"};
    int argc = sizeof(argv)/sizeof(argv[0]);
        
    Rf_initEmbeddedR(argc, argv);
}

void my_R_eval(char* R_cmd) {
    printf("%s\n", R_cmd);
}
----8<----

Reply via email to