Re: [R] R-functions in C-Code

From: Roger Bivand <Roger.Bivand_at_nhh.no>
Date: Mon 31 Oct 2005 - 20:50:52 EST


On Mon, 31 Oct 2005, Olaf Schenk wrote:

> Dear R users,
>
> I would like to use several R functions from a C-code and I have read
> the "Introduction to the .C Interface to R". Unfortunately, my shared
> library with the C-code works only in cases where I use R-routines that
> are defined Rmath.h, eg.

Please see Chapter 5, the R API, and check which C functions are in the API. dnorm() is - though you've been lucky (or unlucky) because the arguments appear to agree. So your example seems to work.

However, the mean() function is an R function, not in the R API. For executing R code from C, see Chapter 4, section 9, "Evaluating R expressions from C".

>
> *************************************
> less test.c:
> #include <R.h>
> #include <Rinternals.h>
> #include <Rmath.h>
> void test(int *nrows, SEXP init,SEXP fac1, SEXP typeordi, SEXP bgratio)
> {
> double sum = dnorm(1.0/1, 0, 1, 0);
> }
>
> Compiling and building shared library:
> gcc -I/usr/lib64/R/include -fPIC -O2 -c test.o test.c
> gcc -shared -L/usr/local/lib -o test.so test.o -L/usr/lib64/R/lib -lR
> Calling R and loading test.so works fine:
> >dyn.load("test.so")
> >
> *************************************
>
> Now, I would like to use other R functions e.g. the mean function from
> the base package:
>
> *************************************
> #include <R.h>
> #include <Rinternals.h>
> #include <Rmath.h>
> void test(int *nrows, SEXP init,SEXP fac1, SEXP typeordi, SEXP bgratio)
> {
> double sum = mean(1.0);
> }
>
> Compiling and building shared library:
> gcc -I/usr/lib64/R/include -fPIC -O2 -c test.o test.c
> gcc -shared -L/usr/local/lib -o test.so test.o -L/usr/lib64/R/lib -lR
>
> Calling R and loading test.so results in an error:
> > dyn.load("test.so")
> Error in dyn.load(x, as.logical(local), as.logical(now)) :
> unable to load shared library '/home/obc/test.so':
>
>
> Any hint is welcome.
>
> Olaf Schenk
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand@nhh.no

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Mon Oct 31 20:54:28 2005

This archive was generated by hypermail 2.1.8 : Tue 01 Nov 2005 - 00:18:03 EST