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

From: Maximillian Murphy <>
Date: Tue, 29 Apr 2008 21:06:01 +0100

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 mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Tue 29 Apr 2008 - 21:55:56 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 29 Apr 2008 - 22:30:37 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive