R.h and Rmath.h in native C program

前端 未结 1 431
梦毁少年i
梦毁少年i 2020-12-30 14:34

\"R.h\" and \"Rmath.h\" are header files for an interface between R.app and C. But, they seems to be readable only through a R command \'R CMD SHLIB something.c\'

I

相关标签:
1条回答
  • 2020-12-30 15:02

    Please see the 'Writing R Extensions' manual about details, you can easily compile and link against Rmath.h and the standalone R Math library -- but not R.h. (Which you can use via Rcpp / RInside but that is a different story.)

    There are a number of examples floating around for use of libRmath, one is in the manual itself. Here is one I ship in the Debian package r-mathlib containing this standalone math library:

    /* copyright header omitted here for brevity */
    
    #define MATHLIB_STANDALONE 1
    #include <Rmath.h>
    
    #include <stdio.h>
    typedef enum {
        BUGGY_KINDERMAN_RAMAGE,
        AHRENS_DIETER,
        BOX_MULLER,
        USER_NORM,
        INVERSION,
        KINDERMAN_RAMAGE
    } N01type;
    
    int
    main(int argc, char** argv)
    {
    /* something to force the library to be included */
        qnorm(0.7, 0.0, 1.0, 0, 0);
        printf("*** loaded '%s'\n", argv[0]);
        set_seed(123, 456);
        N01_kind = AHRENS_DIETER;
        printf("one normal %f\n", norm_rand());
        set_seed(123, 456);
        N01_kind = BOX_MULLER;
        printf("normal via BM %f\n", norm_rand());
    
        return 0;
    }
    

    and on Linux you simply build like this (as I place the library and header in standard locations in the package; add -I and -L as needed on OS X)

    /tmp $ cp -vax /usr/share/doc/r-mathlib/examples/test.c mathlibtest.c
    `/usr/share/doc/r-mathlib/examples/test.c' -> `mathlibtest.c'
    /tmp $ gcc -o mathlibtest mathlibtest.c -lRmath -lm
    /tmp $ ./mathlibtest
    *** loaded '/tmp/mathlibtest'
    one normal 1.119638
    normal via BM -1.734578
    /tmp $ 
    
    0 讨论(0)
提交回复
热议问题