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

2006-03-02 Thread Globe Trotter
> I'd do it now. Really. Not later. *Now*. Like the Posting Guide recommends.
> 
> You may have heard of the sites www.r-project.org and cran.r-project.org. If
> you look closely, you will discover a link 'Manuals' on the left. You'd be
> surprised.  All that you are griping about here is in Section 5.15 of the
> 'Writing R Extension' manual.

Of course! Thanks! I did look at this some years ago: there were no examples
then (that I could decipher, at least) and so it is time to look again!


> No problem here -- see below for you altered program and output.

Thanks; I did figure it out, before I sent the e-mail. (I was just relating my
Thursday experiences.) But thanks! It validates exactly, what I did (except
that I used runif, which actually calls unif_rand(). Fabulous!!

Thanks a lot again, Dirk! This has, really really helped!! 

Best wishes!


> 
> | > (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
> 
> Because you're on Red Hat, and I am on Debian.
> 
> | > 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.
> 
> He really doesn't need my help, but I am flattered by the suggestion. 
> 
> Hope this helps, Dirk
> 
> [EMAIL PROTECTED]:/tmp> cat globetrotter.c
> #include
> #include
> #define MATHLIB_STANDALONE 1
> #include 
> 
> int main(void) {
> 
>   set_seed(42, 43);
>   printf("Random with fixed seed: %f\n", unif_rand());
> 
>   printf("%f \n",pchisq(2.,7., 1, 0));
>   printf("%f \n",pnchisq(2.,7.,0., 1, 0));
>   return EXIT_SUCCESS;
> }
> [EMAIL PROTECTED]:/tmp> gcc -o globetrotter globetrotter.c -lm -lRmath
> [EMAIL PROTECTED]:/tmp> ./globetrotter
> Random with fixed seed: 0.692304
> 0.040160
> 0.040160
> [EMAIL PROTECTED]:/tmp> ./globetrotter
> Random with fixed seed: 0.692304
> 0.040160
> 0.040160
> [EMAIL PROTECTED]:/tmp>   
> 
> 
> -- 
> Hell, there are no rules here - we're trying to accomplish something. 
>   -- Thomas A. Edison
>

__
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


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

2006-03-02 Thread Dirk Eddelbuettel

On 2 March 2006 at 19:37, Globe Trotter wrote:
| > 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),

I'd do it now. Really. Not later. *Now*. Like the Posting Guide recommends.

You may have heard of the sites www.r-project.org and cran.r-project.org. If
you look closely, you will discover a link 'Manuals' on the left. You'd be
surprised.  All that you are griping about here is in Section 5.15 of the
'Writing R Extension' manual.

| > 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

No problem here -- see below for you altered program and output.

| > (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

Because you're on Red Hat, and I am on Debian.

| > 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.

He really doesn't need my help, but I am flattered by the suggestion. 

Hope this helps, Dirk

[EMAIL PROTECTED]:/tmp> cat globetrotter.c
#include
#include
#define MATHLIB_STANDALONE 1
#include 

int main(void) {

  set_seed(42, 43);
  printf("Random with fixed seed: %f\n", unif_rand());

  printf("%f \n",pchisq(2.,7., 1, 0));
  printf("%f \n",pnchisq(2.,7.,0., 1, 0));
  return EXIT_SUCCESS;
}
[EMAIL PROTECTED]:/tmp> gcc -o globetrotter globetrotter.c -lm -lRmath
[EMAIL PROTECTED]:/tmp> ./globetrotter
Random with fixed seed: 0.692304
0.040160
0.040160
[EMAIL PROTECTED]:/tmp> ./globetrotter
Random with fixed seed: 0.692304
0.040160
0.040160
[EMAIL PROTECTED]:/tmp>   


-- 
Hell, there are no rules here - we're trying to accomplish something. 
  -- Thomas A. Edison

__
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


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

2006-03-02 Thread Globe Trotter
Sorry, forgot to switch the header to the R group

--- Globe Trotter <[EMAIL PROTECTED]> wrote:

> Date: Thu, 2 Mar 2006 19:35:21 -0800 (PST)
> From: Globe Trotter <[EMAIL PROTECTED]>
> Subject: Re: [R] calling R's library using C
> To: Dirk Eddelbuettel <[EMAIL PROTECTED]>
> 
> 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 <[EMAIL PROTECTED]> 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
> > |  #include
> > |  #include
> > |  
> > |  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.00 
> > |  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":
> > 
> > [EMAIL PROTECTED]:/tmp> gcc -o globetrotter -I/usr/share/R/include 
> > globetrotter.c
> > -lm -lRmath -L/usr/lib/R/lib -lR
> > [EMAIL PROTECTED]:/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:
> > 
> > [EMAIL PROTECTED]:/tmp> cp /usr/share/doc/r-mathlib/examples/test.c nmtest.c
> > [EMAIL PROTECTED]:/tmp> gcc -o nmtest  nmtest.c -lm -lRmath
> > [EMAIL PROTECTED]:/tmp> ./nmtest
> > [EMAIL PROTECTED]:/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;
> > }
> > [EMAIL PROTECTED]:/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: 
> > [EMAIL PROTECTED]:/tmp> gcc -o globetrotter globetrotter.c -lm -lRmath
> > [EMAIL PROTECTED]:/tmp> ./globetrotter
> > 0.040160
> > 0.040160
> > [EMAIL PROTECTED]:/tmp> cat globetrotter.c
> > #include
> > #include
> > #define MATHLIB_STANDALONE 1
> > #include 
> > 
> > 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 
>

__
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html