[R] Using C code in R

Steve Lianoglou mailinglist.honeypot at gmail.com
Thu Mar 24 05:58:16 CET 2011


Hi,

On Wed, Mar 23, 2011 at 8:12 PM, Jaimin Dave <davejaiminm at gmail.com> wrote:
> Hi,
> I am new to R and I want to know how to use C code which contains two
> functions one called inside another.I know that how to use C code in R if it
> has only one function but dont know how to do it in above case.

There's no special case to consider here.

You would call the "outer" C function from R, and let your C function
call whatever other C functions it wants, ie:

> use the same in R .My C code is as follows.
>
> //#include <R.h>
>
> void sayHello();
>
> void g();
>
>
>
> void sayhello() {
>
> Rprintf("Hello world %d\n",global);
>
> }
>
> void g()
>
> {
>
> int a=9;
>
> int b=1;
>
> Rprintf("Hello world\n %d",(a+b));
>
> sayHello();
>
> }
>
> Help would be greatly appreciated

Assuming you set up your R <--> C stuff correctly, you'd just have R
call your `g` function.

I don't see where you have the `global` variable defined (it's used in
your `sayHello` function) so this will fail for other reasons, but
assuming all of your C stuff is works, then just having R call `g`
correctly will work for you.

-steve

-- 
Steve Lianoglou
Graduate Student: Computational Systems Biology
 | Memorial Sloan-Kettering Cancer Center
 | Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact



More information about the R-help mailing list