[R] Fwd: Re: calling R's library using C

Globe Trotter itsme_410 at yahoo.com
Fri Mar 3 04:37:22 CET 2006


Sorry, forgot to switch the header to the R group....

--- Globe Trotter <itsme_410 at yahoo.com> wrote:

> Date: Thu, 2 Mar 2006 19:35:21 -0800 (PST)
> From: Globe Trotter <itsme_410 at yahoo.com>
> Subject: Re: [R] calling R's library using C
> To: Dirk Eddelbuettel <edd at debian.org>
> 
> Hi, Dirk:
> 
> Thanks for all the help. I thought I would clarify certain things. First, I
> did
> not read that section of the manual (no one provided the pertinent link), but
> I
> did try out that example that you suggested. It is also in
> R.2.2.1/src/nmath/standalone. However, and this is where I got misled,
> Professor Ripley's caustic statement that I should have had the functions in
> libR.so (but I did not know where to look) led me astray. I am sure it is all
> my fault. Anyway, the fact of the matter is that because if I include that
> library (and define that MATH_STANDALONE), I get errors: in particular, it
> would not recognize set_seed (which is a function which should work only if
> that is included). So, something was clearly long. After spending all of
> Thursday on this, I decided to post back.
> 
> Thanks again for your missive. While I would appreciate the pertinent manual
> (English is not my strong suit, but I can follow there, I am sure): btw, I do
> not have the /usr/share/doc/r-mathlib/ directory, nor is it anywhere on my
> system (as per the output to locate), I really thank you for all your help! I
> am sure Professor Ripley would also appreciate your support.
> 
> 
> --- Dirk Eddelbuettel <edd at debian.org> wrote:
> 
> > 
> > (whitespace trimmed) 
> > 
> > On 2 March 2006 at 13:42, Globe Trotter wrote:
> > |  Thanks, everyone for all the help! So, here is my calling function in C
> > |  (called
> > |  test.c):
> > |  
> > |  #include<stdio.h>
> > |  #include<stdlib.h>
> > |  #include<Rmath.h>
> > |  
> > |  int main(void) {
> > |    printf("%f \n",pchisq(2.,7., 1, 0));
> > |    printf("%f \n",pnchisq(2.,7.,0., 1, 0));
> > |    return EXIT_SUCCESS;
> > |  }
> > |  
> > |  I compile using:
> > |  
> > |   gcc test.c -I/usr/lib/R/include -L/usr/lib/R/lib -lm -lR 
> > |  
> > |  However, running
> > |  ./a.out
> > |  
> > |  gives me:
> > |  
> > |  1.000000 
> > |  0.040160 
> > |  
> > |  The first is wrong, but the second non-central is correct, and matches
> the
> > |  answer from R.
> > |  
> > |  Incidentally, pgamma (which is what pchisq calls, as per the C program
> > inside
> > |  R) is also wrong and not surprisingly, gives the same answer as above.
> > |  
> > |  Any suggestions?
> > 
> > As Brian Ripley already told you, you are so wrong that it is unclear why
> we
> > bother helping you for matters clearly stated in manuals you continue to
> > ignore.
> > 
> > Anyway -- on my Debian system, your file compiles, builds and runs "fine":
> > 
> > edd at basebud:/tmp> gcc -o globetrotter -I/usr/share/R/include globetrotter.c
> > -lm -lRmath -L/usr/lib/R/lib -lR
> > edd at basebud:/tmp> LD_LIBRARY_PATH=/usr/lib/R/lib ./globetrotter
> > 0.040160
> > 0.040160
> > 
> > That said, I put "fine" in quotes as you shouldn't need either -lR nor the
> > include directive. Witness:
> > 
> > edd at basebud:/tmp> cp /usr/share/doc/r-mathlib/examples/test.c nmtest.c
> > edd at basebud:/tmp> gcc -o nmtest  nmtest.c -lm -lRmath
> > edd at basebud:/tmp> ./nmtest
> > edd at basebud:/tmp> tail -6 nmtest.c
> > main()
> > {
> > /* something to force the library to be included */
> >     qnorm(0.7, 0.0, 1.0, 0, 0);
> >     return 0;
> > }
> > edd at basebud:/tmp>            
> > 
> > The key is the
> > 	#define MATHLIB_STANDALONE 1
> > in the R example. Once you add that before the #include for Rmath.h, you're
> > fine: 
> > edd at basebud:/tmp> gcc -o globetrotter globetrotter.c -lm -lRmath
> > edd at basebud:/tmp> ./globetrotter
> > 0.040160
> > 0.040160
> > edd at basebud:/tmp> cat globetrotter.c
> > #include<stdio.h>
> > #include<stdlib.h>
> > #define MATHLIB_STANDALONE 1
> > #include <Rmath.h>
> > 
> > int main(void) {
> >   printf("%f \n",pchisq(2.,7., 1, 0));
> >   printf("%f \n",pnchisq(2.,7.,0., 1, 0));
> >   return EXIT_SUCCESS;
> > }
> > 
> > As they say, if all else fails you could consider reading the manual that
> > discusses this example.
> > 
> > Dirk
> > 
> > 
> > -- 
> > Hell, there are no rules here - we're trying to accomplish something. 
> >                                                   -- Thomas A. Edison
> > 
> 
> 
> __________________________________________________
> Do You Yahoo!?

> http://mail.yahoo.com 
>




More information about the R-help mailing list