Wei-Chen Chen | About me | pbdR Tech | HPSC | Phyloclustering | R_note |

R_note -- The Exploration of Statistical Software R (統計軟體 R 深度歷險)
About R_note
MS Windows

Batch jobs

Remark Lines
S4 Methods
Batch more

Good Coding
Fast Loop

PHP Call R
Basic C
R Call Fortran/C
R Call GSL
C Call R API
C Call R Objects

Make Packages
C Pointer

Section: Standalone

Basic Ideas:

For standardlone, it may usually requires "R.dll", "Rmath.dll", "Rblas.dll", and "Rlapack.dll" for Windows, or "libR.a", "libRmath.a", "libRblas.a", and "libR.a" for Linux shared library, or "libR.so", "libRmath.so", "libRblas.so", and "libR.so" for Linux static library. "Rmath", "Rblas", and "Rlapac" may not necessary for every situation which is dependent on what functions are called inside C or Fortan codes.

For Windows:
  • Tools
    The tools in the section Windows are all required. Also, "mingw32-make.exe" for MinGW32 is also required and located at "Rtools\MinGW\bin\make.exe". (my mirror here)

  • Reference
    This part is only testing on R-2.7.1 and Windows XP version. See the section 9.2 in the R Installation and Administration (my mirror here) for detail.

  • Recompile
    After you install the R from the executable file, there are "R.dll", "Rblas.dll", "Rlapack.dll" inside the "C:\Progra~1\R\R-2.7.1\bin\", and also "R.h", "Rmath.h" inside the "C:\Progra~1\R\R-2.7.1\include". We need a further file "Rmath.dll" for building standardlone codes on Windows. Donload the R source code, and uncompress the source.
         At "R-2.7.1\src\include\", type
         SHELL> make -f Makefile.win config.h Rconfig.h Rmath.h
         At "R-2.7.1\src\nmath\standatdlone\", type
         SHELL> make -f Makefile.win

    After previous steps, the shared library and static library, "Rmath.dll" and "libRmath.a" are located at "R-2.7.1\src\nmath\standatdlone\" Copy them to "C:\Progra~1\R\R-2.7.1\bin\". Then, you can do the same compiling procedure as Linux version at the following.

For Linux:
  • Reference
    For the detail, read the web page The R Manuals and the PDF document Writing R Extensions (Chapter 5.13) in The R Manuals (my mirror here).

  • Recompile
    The default installation (RPM or compile from source) of R doesn't build the standalone library, so you have to do it by yourself. Download the source R-1.7.1.tgz (9MB) (my mirror here).

       SHELL> tar zxvf R-1.7.1.tgz
    SHELL> cd R-1.7.1/
    SHELL> ./configure; make
    SHELL> cd src/nmath/standalone/
    SHELL> make shared
    SHELL> cp libRmath.so /usr/lib/R/bin/
    SHELL> cd /usr/lib
    SHELL> ln -s /usr/lib/libRmath.so ./R/bin/libRmath.so

    1. The subdirectory "src/nmath/standalone/" is the source of Mathlib for standalone library "libRmath".
    2. The "Rmath.h" (in /usr/lib/R/include/) is the R set of mathematical functions.
    3. The fils "libRmath.so" is the sared library for Linux.
    4. Suggest to copy the library files to "/usr/lib/R/bin/".

    Related information for this topic:
    1. The file "libRmath.a" is the static library for Linux. Use "make static" to subsitute "make shared".
    2. The file "Rmath.dll" is the dynamic loaded library for MS Windows.

  • Main code
    The C code "stand.c" will set the seed and generate 10 random variables from standard normal (mu = 0, sigma = 1) and compute the cumulative probability.

    #include <Rmath.h>
    int main(){
      int i;
      unsigned int SEED1, SEED2;
      double mu, sigma, PHI_X, *X;
      mu = 0;
      sigma = 1;
      SEED1 = 12345;
      SEED2 = 67890;
      set_seed(SEED1, SEED2);
      X = (double *) malloc(10);
      for(i = 0; i < 10; i++){
        X[i] = rnorm(mu, sigma);
        PHI_X = pnorm(X[i], mu, sigma, 1, 0);
        printf("X: %f, PHI(X): %f\n", X[i], PHI_X);

  • Compile
         SHELL> gcc -o stand stand.c -I/usr/lib/R/include/ -lRmath -lm

    Make sure the requried libraries are all in the path, otherwise you have to do the followings.
         SHELL> gcc -o stand stand.c -I/usr/lib/R/include/ -L/usr/lib/R/lib/ -lRmath -lm

    The output will be a executable file "stand" (for linux only).

  • Output
    The output is as the following.

    X: 0.999948, PHI(X): 0.841332
    X: -1.768380, PHI(X): 0.038499
    X: 0.623938, PHI(X): 0.733666
    X: 2.167754, PHI(X): 0.984911
    X: 0.749400, PHI(X): 0.773192
    X: -1.253019, PHI(X): 0.105099
    X: 0.640719, PHI(X): 0.739147
    X: 0.567502, PHI(X): 0.714814
    X: -1.744883, PHI(X): 0.040503
    X: 0.140647, PHI(X): 0.555925

[ Go to top ]

Created: Oct 06 2003
Last Revised: May 09 2015, 07:18 (CST Taipei, Taiwan)
Maintained: Wei-Chen Chen
E-Mail: wccsnow @ gmail.com
free counters Best Resolution
small font