Since you are using Borland C++, the examples in the online complement to 'S Programming' are relevant, as is README.packages.

Basically, you have not managed to export any entry points, and to do so you will need to make a DLL and not a .exe.

*However* it is much easier to make use of the MinGW compiler used to make R.

You are trying too hard using getNativeSymbolInfo etc, and R CMD INSTALL does work under Windows.

I would study 'Writing R Extensions' and 'R Installation and Administration Manual', install MinGW and use

R CMD SHLIB

to make a DLL.  It really is a lot easier to follow a well-trodden path
than to create your own. (In Roger Bivand's analogy: you need stepping stones across this morass/mire/bog.)


On Fri, 8 Jun 2007, Marthews, T. R. wrote:

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;

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

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

printf("\nPress <ENTER> ...\n");
getchar();
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:

***********************************************************************************************
dll=dyn.load("Project1.exe")
syms=getNativeSymbolInfo(c("Rc","Rc2"),dll)

RcInR=function(x) {
.Call(dll$Rc,as.numeric(x))
}

Rc2InR=function(x) {
.Call(dll$Rc2,as.numeric(x))
}

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

m1=6;n1=Rc2(m1)
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]]



--
Brian D. Ripley,                  [EMAIL PROTECTED]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595
______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Reply via email to