[Rd] Question about Running C code from R

From: Marthews, T. R. <t.marthews_at_abdn.ac.uk>
Date: Fri, 08 Jun 2007 10:24:04 +0100

Dear R-devel,  

Apologies for sending what is probably a very simple question to R-devel: I am definitely missing something very simple and can't work out what it is. I've been trying to find the problem here for about a month and need some help!  

I am trying to work out how to run a C program from an R script. Rather than try the C program directly, I'm trying to get a much simpler piece of code to work as a proof-of-concept. Here's the C code (simplecall.c):  

#include <stdio.h>
#include <Rinternals.h>
#include <R_ext/Rdynload.h>

int Rc(int a) {
 return (a+3);

SEXP Rc2(SEXP a) {
 return(18); //LINE A
// return(ScalarReal(REAL(a)[0]+12)); //LINE B

int main()
 int i,j;
 SEXP m,n;

 printf("\nUsing Rc: %i+3=%i - OK so far.\n",i,j);

 printf("\nUsing Rc2: %i+12=%i - Doing well.\n",m,n);

 printf("\nPress <ENTER> ...\n");
 return 0;

and I've compiled this (using the only C compiler I have, which is a Borland C++ Builder which insists on calling the executable "Project.exe"). I managed to get it to compile correctly with the two R includes at the front and the two functions work OK when I run the C executable. What I want to do is access these functions from R, however. Here's the R code I've written (simplecall.r), which I've put together from various websites and guesses:


RcInR=function(x) {

Rc2InR=function(x) {

cat("\nUsing Rc from R: ",i1,"+3=",j1," - OK so far.\n")

cat("\nUsing Rc2 from R: ",m1,"+12=",n1," - Doing well.\n");

Basically, there are two problems (at least!). If I have LINE A in the C code instead of LINE B then it compiles alright, but if I try to run the R script it says:

Error in FUN(c("Rc", "Rc2")[[1]], ...) : no such symbol Rc in package C:/Documents and Settings/Toby/Desktop/cfiles/Project1.exe

I've read a lot online about the "R CMD INSTALL" command and the ?INSTALL man page, but I can't use that command because I've got Windows-R rather than Linux-R (unfortunately). Somehow, R can't 'see' these C functions in the executable, even though I thought including Rinternals.h ensured that it could. Stuck.

Additionally, if I have LINE B instead of LINE A in the C code then it won't compile at all and gives the errors:

[Linker error] Unresolved external '_REAL' referenced from C:\DOCUMENTS AND SETTINGS\TOBY\DESKTOP\CFILES\SIMPLECALL.OBJ [Linker error] Unresolved external '_Rf_ScalarReal' referenced from C:\DOCUMENTS AND SETTINGS\TOBY\DESKTOP\CFILES\SIMPLECALL.OBJ

which I find odd because these symbols are defined in the .h files included at the start.

I feel like I'm either a) trying to do this in completely the wrong way or b) suffering from having to use a Windows machine or c) both. I tried setting up a package and inserting the code in the /src directory, but because I can't use the R CMD INSTALL command I can't figure out how to make this idea work either.

Can anyone help me here? Does anyone have an actual example of something like this that works?

Toby Marthews


Institut für Umweltwissenschaften (IfU, www.uzh.ch/uwinst <http://www.uzh.ch/uwinst> ), Universität Zürich, Winterthurerstr. 190, 8057 Zürich, CH.

Plant & Soil Science Department (www.abdn.ac.uk/biologicalsci/pss <http://www.abdn.ac.uk/biologicalsci/pss> ), University of Aberdeen, 23 St Machar Dr., Aberdeen AB24 3UU, UK.

¯`··..¸¸·´¯`·.¸¸·´¯`··..¸¸·´¯`··..¸¸·´¯`·.¸ ><(((º> ¸.·´¯`·..¸ ><(((º>

        [[alternative HTML version deleted]]

R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Fri 08 Jun 2007 - 12:35:59 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 Fri 08 Jun 2007 - 16:33:59 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.