[R] Calling R from C - part way there but need a push!

Maximillian Murphy m at de-minimis.co.uk
Tue Apr 29 22:06:01 CEST 2008



I'm afraid I haven't quite got the hang of this yet.  Here's a Hello  
World:


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


extern int R_running_as_main_program;   /* in ../unix/system.c */

int main(int ac, char **av)
{

     SEXP aleph, beth;

  // R_running_as_main_program = 1;         // A
  // Rf_initialize_R(ac, av);               // B
  // Rf_mainloop();                         // C


  // R_running_as_main_program = 0;       // a
  // Rf_initialize_R(ac, av);             // b


  // aleph = PROTECT(allocVector(VECSXP, 2)); // 1
  // aleph = Rf_mkString("hoodunnit");        // 2
  // aleph = allocVector(REALSXP, 2);         // 3

     return 0;
}

The simple example of a front end given in Chapter 8 is obtained by  
uncommenting A, B and C, and that works fine.  It gives an  
interactive shell.

If instead I uncomment any of 1, 2 or 3 I have just a simple  
declaration and assignment similar to several I have found in the R  
source files.  The only difference I can see is that I am running in  
main() rather than in a C function that is called by R.  Yet if I try  
to run this I get a segfault.

I reasoned that when starting R probably sets up some data structures  
that are needed so I tried inserting lines a and b, but I still get a  
segfault.

Do you have any suggestions on how to proceed?

{ I need to be able to create SEXPs to feed eval().  I don't yet know  
what kind of SEXPs but I'll cross that bridge when I come to it! }

Regards, Max



More information about the R-help mailing list