SlideShare a Scribd company logo
Introduction to .Call interface

                                         Hao Chai

                       Department of Statistics and Actuarial Science
                                 The University of Iowa


                                 Productive Seminar
                                   March 6, 2012




Hao Chai (U of Iowa)              Introduction to .Call interface       1 / 41
Outline


1   Introduction

2   C part
      .C interface
      .Call interface

3   R part

4   Examples

5   Other



     Hao Chai (U of Iowa)   Introduction to .Call interface   2 / 41
Introduction


Outline


1   Introduction

2   C part
      .C interface
      .Call interface

3   R part

4   Examples

5   Other



     Hao Chai (U of Iowa)   Introduction to .Call interface   3 / 41
Introduction




.C and .Call are two interfaces in R to call C routines. Similar foreign
language interface functions are .Fortran and .External.




    Hao Chai (U of Iowa)     Introduction to .Call interface               4 / 41
Introduction




Advantages of R
       Straight forward syntax.
       Easy to learn, to use, to track the results and to debug.
       Over 3,000 packages available to carry out many kinds of statistical
       analysis and produce professional graphics.
       Vectorized language.
       ...
Disadvantage of R
       Loops.




Hao Chai (U of Iowa)        Introduction to .Call interface                   5 / 41
Introduction




Advantages of R
       Straight forward syntax.
       Easy to learn, to use, to track the results and to debug.
       Over 3,000 packages available to carry out many kinds of statistical
       analysis and produce professional graphics.
       Vectorized language.
       ...
Disadvantage of R
       Loops.




Hao Chai (U of Iowa)        Introduction to .Call interface                   5 / 41
Introduction


Toy example
Our goal is to generate a variance-covariance matrix Σ = (σij ), where
        a · b|i−j| , if i = j;
σij =
        1,           if i = j.
We have at least three methods:
    A simple R function matgen R:
    matgen_R = function(P, a, b)
    {
      Covmatrix = matrix(rep(1, P * P), nrow = P)
      for (i in 1 : P)
        for (j in 1 : P)
          if (i != j)
            Covmatrix[i, j] = exp(log(a) + log(b)
            * abs(i - j))
      Covmatrix
    }
    Hao Chai (U of Iowa)    Introduction to .Call interface              6 / 41
Introduction


Toy example


   An R function using matrix indexing matgen Rind:
   matgen_Rind = function(P, a, b)
   {
     Covmatrix = matrix(rep(1, P * P), nrow = P)
     for (i in 1 : (P - 1))
       {Covmatrix[cbind((1 + i) : P, 1 : (P - i))] =
       exp(log(a) + log(b) * abs(i))
        Covmatrix[cbind(1 : (P - i), (1 + i) : P)] =
        exp(log(a) + log(b) * abs(i))}
     Covmatrix
   }



   Hao Chai (U of Iowa)   Introduction to .Call interface   7 / 41
Introduction




    An R function calling C routines matgen RC:
    matgen_RC = function(P, a, b)
    {
      .Call("matrix_gen", as.integer(P), as.double(a),
            as.double(b))
    }
Also we can use R compiler to compile the first two pure R functions and
get matgen Rcmp and matgen Rindcmp.




    Hao Chai (U of Iowa)   Introduction to .Call interface           8 / 41
Introduction


Toy example


Result
> P = 1000

           test                                            elapsed     user.self   relative
     5     matgen          Rindcmp(P, 0.5, 0.9)               0.80         0.75       1.00
     4     matgen          Rind(P, 0.5, 0.9)                  0.97         0.86       1.21
     1     matgen          RC(P, 0.5, 0.9)                    1.04         1.01       1.30
     2     matgen          Rcmp(P, 0.5, 0.9)                 19.98       19.51       24.98
     3     matgen          R(P, 0.5, 0.9)                    65.44       64.45       81.80




    Hao Chai (U of Iowa)             Introduction to .Call interface                          9 / 41
C part


Outline


1   Introduction

2   C part
      .C interface
      .Call interface

3   R part

4   Examples

5   Other



     Hao Chai (U of Iowa)   Introduction to .Call interface   10 / 41
C part


Header files



To enable the communication between R and C, the following header files
are needed in the C code.

#include      <R.h>
#include      <Rinternals.h> /% For .C or .Call %/
#include      <Rdefines.h> /% For .External %/
#include      <R_ext/Rdynload.h>




    Hao Chai (U of Iowa)   Introduction to .Call interface         11 / 41
C part     .C interface




To use .C interface, main() function in C code needs to be replaced by

static void CFuncName(arg1, arg2, ...)

    Arguments above have to be of pointer type. Therefore, we need to
    coerce the scalar arguments to scalars in C.
    C function can only return values to R through its arguments. In
    other words, there are two kinds of arguments: arguments that pass
    values from R to C and arguments that return values from C to R.
The C code using .C interface in the toy example is




    Hao Chai (U of Iowa)    Introduction to .Call interface              12 / 41
C part     .C interface


.C interface in toy example

#include <R.h>
#include <Rinternals.h>
#include <R_ext/Rdynload.h>
#include <math.h>
static void matrix_gen(double *Rp, double *Ra, double *Rb,
                       double *Rmatrix)
{
    R_len_t i, j, p = Rp[0];
    double a = Ra[0], b = Rb[0];
    for (i = 0; i < p; i++)
        for (j = 0; j < p; j++)
            if (i == j) Rmatrix[i * p + j] = 1;
            else Rmatrix[i * p + j] = exp(log(a) + abs(i - j)
            * log(b));
}
   Hao Chai (U of Iowa)   Introduction to .Call interface   13 / 41
C part     .C interface


Mapping types between R and C




                          Table: Comparison of R and C data types.1
                               R storage mode               C types
                                    logical                  int *
                                    integer                  int *
                                    double                 double *
                                  character                 char **
                                   complex                Rcomplex *




  1
      Cited from “Writing R Extensions”
       Hao Chai (U of Iowa)          Introduction to .Call interface   14 / 41
C part     .Call interface


What can we expect from .Call?2




       Pass R objects to C
       Create R objects in C
       Manipulate R objects in C
       Return R objects from C
       Call R functions from C




  2
      See Brian Caffo
      Hao Chai (U of Iowa)       Introduction to .Call interface    15 / 41
C part     .Call interface




To use .Call interface, main() function in C code needs to be replaced by

SEXP CFuncName(SEXP arg1, SEXP arg2, ...)

    SEXP is a structure in C standing for S expression.
    The returned value from C will also be SEXP type.
    If new SEXP objects are defined within the C function, use
    PROTECT to protect them in the memory so that it will not be
    cleaned by R as garbage.




    Hao Chai (U of Iowa)    Introduction to .Call interface            16 / 41
C part     .Call interface


Basic structure of C functions


SEXP CFuncName(SEXP arg1, arg2, ...)
{
  [arg1 = coerceVector(arg1, INTSXP)]
  # Coerce the vector to the right type.
  SEXP Robj, return_val;   # Define new R objects.
  PROTECT(return_val = allocMatrix(REALSXP, arg1, arg2))
  # Protect return_val.
  [body of function]
  UNPROTECT(1)
  return(return_val) # Return the values to R.
}



   Hao Chai (U of Iowa)   Introduction to .Call interface    17 / 41
C part     .Call interface


Useful data types in Rinternals.h



                                                                         3
                              Table: SEXPTYPE and R equivalent
                   SEXPTYPE                      R equivalent
                    REALSXP          numeric with storage mode double
                     INTSXP                         integer
                     LGLSXP                          logical
                    STRSXP                         character
                    VECSXP                  list (generic vector)
                    DOTSXP                       a ‘...’ object
                    ENVSXP                       environment



  3
      Cited from “Writing R Extensions”
       Hao Chai (U of Iowa)           Introduction to .Call interface        18 / 41
C part     .Call interface


Useful macros in Rinternals.h
There are plenty of useful functions defined in Rinternals.h. Useful
functions can be found in “Writing R Extensions” or “R Internals”.

                        Table: List of useful functions in Rinternals.h4


     Name                                 Usage                                    Example
 coerceVector                 Coerce R object to SEXPTYPE                  arg1=..(arg1,INTSXP)
  allocVector                  Allocate memory for R vector                 Robj=..(STRSXP,2)
  allocMatrix                  Allocate memory for R matrix                Robj=..(STRSXP,2,2)
     nrows                    Get the No. of rows of a matrix                 ..nrow(Rmatrix)
      ncols                   Get the No. of cols of a matrix                  ..ncol(Rmatrix)
  PROTECT                           Protect an R object                         See examples.
 UNPROTECT                     UNPROTECT some R objects                         See examples.

  4
      .. represents the function name in the same row.
       Hao Chai (U of Iowa)             Introduction to .Call interface                    19 / 41
C part     .Call interface


Useful functions in Rinternals.h



                    Table: List of useful functions in Rinternals.h Cont.5


       Name                                   Usage                             Example
   STRING ELT                  Returns the memo. add. of R string             ..(Rstr, 0)
   VECTOR ELT                   Returns the memo. add. of R list              ..(Rlist, 1)
     INTEGER                    Return the memo. add. of R int                   ..(Rint)
       REAL                     Return the memo. add. of R real                ..(Rdoub)
       CHAR                       Convert a Rchar to characters                 ..(Rchar)
 SET VECTOR ELT                Assign value to an element of R list          See example



  5
      .. represents the function name in the same row.
       Hao Chai (U of Iowa)          Introduction to .Call interface                  20 / 41
C part     .Call interface


Special Values



There are some constants for special values in R
    R NaN
    R PosInf
    R NegInf
    R NaReal
    R NilValue
There are also some macros for error checking in C ISNA, ISNAN,
R FINITE, NA INTEGER, NA LOGICAL, NA STRING, NA REAL




    Hao Chai (U of Iowa)    Introduction to .Call interface       21 / 41
C part     .Call interface


.Call interface in toy example


#include <R.h>
#include <Rinternals.h>
#include <R_ext/Rdynload.h>
#include "math.h"
SEXP matrix_gen(SEXP RP, SEXP Ra, SEXP Rb)
{
    R_len_t i, j, p = INTEGER(RP)[0];
    SEXP Rmatrix;
    RP = coerceVector(RP, INTSXP);
    Ra = coerceVector(Ra, REALSXP);
    Rb = coerceVector(Rb, REALSXP);
    PROTECT(Rmatrix = allocMatrix(REALSXP, p, p));


    Hao Chai (U of Iowa)   Introduction to .Call interface    22 / 41
C part     .Call interface


.Call interface in toy example



    double *mat, a = REAL(Ra)[0], b = REAL(Rb)[0];
    mat = REAL(Rmatrix);
    for (i = 0; i < p; i++)
        for (j = 0; j < p; j++)
            if (i == j) mat[i * p + j] = 1;
            else mat[i * p + j] = exp(log(a) +
            abs(i - j) * log(b));
    UNPROTECT(1);
    return(Rmatrix);
}




    Hao Chai (U of Iowa)   Introduction to .Call interface    23 / 41
R part


Outline


1   Introduction

2   C part
      .C interface
      .Call interface

3   R part

4   Examples

5   Other



     Hao Chai (U of Iowa)   Introduction to .Call interface   24 / 41
R part


Interfaces in R

To create a dynamic library, run the following code in the command line:

R CMD SHLIB CFileName.c

CFileName.dll or CFileName.so will be generated in the same directory.
Then load the dynamic library which is generated by C code using
> dyn.load("CFileName.so")
in Linux/MacOS, or
> dyn.load("CFileName.dll")
in Windows, or a universal solution
> dyn.load(paste("CFileName", .Platform$dynlib.ext,
+                sep = ""))


    Hao Chai (U of Iowa)    Introduction to .Call interface              25 / 41
R part




There are three interfaces in R to load the dynamic library generated by C.
  1   .C(”CFuncName” arg1, arg2, ...)
                    ,
  2   .Call(”CFuncName” arg1, arg2, ...)
                       ,
  3   .External(”CFuncName”, arg1, arg2, ...)

      There are some options when using .C, such as NAOK and
      PACKAGE. For detailed information, refer to “Writing R Extensions”.




      Hao Chai (U of Iowa)   Introduction to .Call interface            26 / 41
Examples


Outline


1   Introduction

2   C part
      .C interface
      .Call interface

3   R part

4   Examples

5   Other



     Hao Chai (U of Iowa)   Introduction to .Call interface   27 / 41
Examples


Pass numeric values to C
func1 is a function with two arguments Rn and Rvec. It returns the largest
element of the first Rn elements of vector Rvec.
SEXP func1(SEXP Rvec, SEXP Rn)
{
  Rvec = coerceVector(Rvec, REALSXP);
  R_len_t i = 0, n = INTEGER(Rn)[0];
  double *vec, max = REAL(Rvec)[0];
  SEXP return_val;
  PROTECT(return_val = allocVector(REALSXP, 1));
  vec = REAL(Rvec);
  for (i = 1; i < n; i++)
    if (vec[i] >= max) max = vec[i];
  REAL(return_val)[0] = max;
  UNPROTECT(1);
  return(return_val);
}
    Hao Chai (U of Iowa)    Introduction to .Call interface           28 / 41
Examples


Pass numeric values to C




In R, we do the following

a = c(7, 4, 8, 9, 2, 5)
n = 4
dyn.load("f:/presentation/intro2dotCall/illust.dll")
.Call("func1", as.double(a), as.integer(n))




    Hao Chai (U of Iowa)    Introduction to .Call interface   29 / 41
Examples


Pass strings, return a list




func2 is able to calculate the quadratic form or the linear product of a
matrix and a vector. It has three arguments: Rstr, Ry and RM. Ry is a
vector, RM is a matrix. If Rstr is ”quadratic”, then t(Ry) %*% RM %*%
Ry is returned. If Rstr is ”linear”, then RM %*% Ry is returned. If Rstr is
”both” then both of the above will be returned as an R list.
      ,




    Hao Chai (U of Iowa)     Introduction to .Call interface             30 / 41
Examples


Pass strings, return a list

SEXP func2(SEXP RM, SEXP Ry, SEXP Rstr)
{
  Rstr = coerceVector(Rstr, STRSXP);
  RM = coerceVector(RM, REALSXP);
  Ry = coerceVector(Ry, REALSXP);
  SEXP return_lst, vec, num;
  double *M = REAL(RM), *y = REAL(Ry);
  double result1 = 0, *result2;
  R_len_t i, j, nr, nc, l;
  const char *method;
  method = CHAR(STRING_ELT(Rstr, 0));
  nr = nrows(RM);
  nc = ncols(RM);
  l = length(Ry);

    Hao Chai (U of Iowa)   Introduction to .Call interface   31 / 41
Examples




if (((strcmp(method, "both") == 0) ||
     (strcmp(method,"linear") == 0)) && (nc != l))
 error("The matrix and the vector need to be conformable!");
if (((strcmp(method, "both") == 0) ||
     (strcmp(method, "quadratic") == 0)) &&
     ((nc != l) || (nr != l)))
 error("The matrix and the vector need to be conformable!");
if (strcmp(method, "both") == 0)
  {
      PROTECT(return_lst = allocVector(VECSXP, 2));
      PROTECT(vec = allocVector(REALSXP, nr));
      PROTECT(num = allocVector(REALSXP, 1));
      result2 = REAL(vec);
      result1 = REAL(num)[0];
  }
else PROTECT(return_lst = allocVector(VECSXP, 1));

 Hao Chai (U of Iowa)   Introduction to .Call interface   32 / 41
Examples




if (strcmp(method, "linear") == 0)
  {
   PROTECT(vec = allocVector(REALSXP, nr));
   result2 = REAL(vec);
  }
if (strcmp(method, "quadratic") == 0)
  {
   PROTECT(num = allocVector(REALSXP, 1));
   result1 = REAL(num)[0];
  }
if ((strcmp(method, "quadratic") == 0) ||
    (strcmp(method, "both") == 0))
  for (i = 0; i < nr; i++)
      for (j = 0; j < nc; j++)
          result1 = result1 + M[i + j * nr] * y[i] * y[j];


 Hao Chai (U of Iowa)   Introduction to .Call interface   33 / 41
Examples




if ((strcmp(method, "linear") == 0) ||
      (strcmp(method, "both") == 0))
    for (i = 0; i < nr; i++)
    {
        result2[i] = 0;
        for (j = 0; j < nc; j++)
            result2[i] = result2[i] + M[i + j * nr] * y[j];
    }




   Hao Chai (U of Iowa)   Introduction to .Call interface   34 / 41
Examples




    if (strcmp(method, "quadratic") == 0)
      SET_VECTOR_ELT(return_lst, 0, num);
    if (strcmp(method, "linear") == 0)
      SET_VECTOR_ELT(return_lst, 0, vec);
    if (strcmp(method, "both") == 0)
    {
        SET_VECTOR_ELT(return_lst, 0, num);
        SET_VECTOR_ELT(return_lst, 1, vec);
    }
    UNPROTECT(2);
    if (strcmp(method, "both") == 0)
    UNPROTECT(1);
    return(return_lst);
}



     Hao Chai (U of Iowa)   Introduction to .Call interface   35 / 41
Examples


Pass strings, return a list




The R code to call the function func2 is

dyn.load("f:/presentation/intro2dotCall/illust.dll")
M <- matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), nrow = 3)
y <- c(1, 0, -1)
.Call("func2", M, as.double(y), "both")




    Hao Chai (U of Iowa)    Introduction to .Call interface   36 / 41
Other


Outline


1   Introduction

2   C part
      .C interface
      .Call interface

3   R part

4   Examples

5   Other



     Hao Chai (U of Iowa)   Introduction to .Call interface   37 / 41
Other


Evaluating R expressions in C



It is also possible to evaluate R expressions in C code. The following
functions/macros are useful in this case. More details can be found in
“Writing R extensions”  .
     defineVar
     findVar
     findFun
     install




     Hao Chai (U of Iowa)    Introduction to .Call interface             38 / 41
Other




Some misc.
    The “Writing R Extensions” manual is the main source for this talk.
    More functions and macros can be found in Rinternals.h, or
    alternatively Rdefines.h. Choose one and stick to it.
    Several times, when ran .Call in R, R crashed. Very likely, it was
    caused by the difference between number of arguments in .Call and
    number of arguments in the original C functions.
    Related C and R files are available at Related files.




    Hao Chai (U of Iowa)    Introduction to .Call interface           39 / 41
Other


Refrences




Writing R Extensions
R Internals
Using .Call in R by Brian Caffo.
dot Call Interface by Gopi Goswami.
Rinternals.h




    Hao Chai (U of Iowa)   Introduction to .Call interface   40 / 41
Other




                       Thank you!




Hao Chai (U of Iowa)    Introduction to .Call interface   41 / 41

More Related Content

What's hot (20)

PPT
Chapter 6 intermediate code generation
Vipul Naik
 
PPTX
Back patching
santhiya thavanthi
 
PPTX
Intermediate code generation1
Shashwat Shriparv
 
PDF
Sentence-to-Code Traceability Recovery with Domain Ontologies
Institute of Science Tokyo
 
PPT
Chapter Eight(3)
bolovv
 
PPT
Interm codegen
Anshul Sharma
 
PPTX
Three address code In Compiler Design
Shine Raj
 
PPT
Cd2 [autosaved]
BBDITM LUCKNOW
 
PPTX
C Programming Language Tutorial for beginners - JavaTpoint
JavaTpoint.Com
 
PDF
Object-Oriented Programming in Modern C++. Borislav Stanimirov. CoreHard Spri...
corehard_by
 
PPTX
Intermediate code- generation
rawan_z
 
PPT
C language
spatidar0
 
PDF
Generating Assertion Code from OCL: A Transformational Approach Based on Simi...
Institute of Science Tokyo
 
PDF
Compiler unit 4
BBDITM LUCKNOW
 
PPTX
introduction of c langauge(I unit)
Prashant Sharma
 
PPT
Introduction to c programming
gajendra singh
 
PPTX
C Programming Language Step by Step Part 1
Rumman Ansari
 
PDF
Top C Language Interview Questions and Answer
Vineet Kumar Saini
 
PDF
Hands-on Introduction to the C Programming Language
Vincenzo De Florio
 
Chapter 6 intermediate code generation
Vipul Naik
 
Back patching
santhiya thavanthi
 
Intermediate code generation1
Shashwat Shriparv
 
Sentence-to-Code Traceability Recovery with Domain Ontologies
Institute of Science Tokyo
 
Chapter Eight(3)
bolovv
 
Interm codegen
Anshul Sharma
 
Three address code In Compiler Design
Shine Raj
 
Cd2 [autosaved]
BBDITM LUCKNOW
 
C Programming Language Tutorial for beginners - JavaTpoint
JavaTpoint.Com
 
Object-Oriented Programming in Modern C++. Borislav Stanimirov. CoreHard Spri...
corehard_by
 
Intermediate code- generation
rawan_z
 
C language
spatidar0
 
Generating Assertion Code from OCL: A Transformational Approach Based on Simi...
Institute of Science Tokyo
 
Compiler unit 4
BBDITM LUCKNOW
 
introduction of c langauge(I unit)
Prashant Sharma
 
Introduction to c programming
gajendra singh
 
C Programming Language Step by Step Part 1
Rumman Ansari
 
Top C Language Interview Questions and Answer
Vineet Kumar Saini
 
Hands-on Introduction to the C Programming Language
Vincenzo De Florio
 

Viewers also liked (20)

PPTX
Erasmus grants201112 presentationforbriefings
lancs-erasmus
 
ODP
Presentacion laura
lau_kaxon
 
ODP
queens, new york
lau_kaxon
 
PDF
SAB cristal 410 concentrate
SAB Group Shams Al Bawadi
 
PPT
Is Graduate School 4 U
dlofton
 
PDF
Infographics introduction
awhitfie
 
PDF
Tar fvg punto nascita h latisana
Carlo Favaretti
 
PDF
Stabilità 2017
Carlo Favaretti
 
PPTX
Stick figure (vision)
bobelster
 
PDF
Sinopsis Pemutaran Film Khanduri Pilem
DiMas Diliasmara
 
PDF
Come distinguere l'innovazione dal nuovo? | sanità24 il sole 24 ore
Carlo Favaretti
 
ODP
GuiducciLorenzo_2010-2011_Esercizio4
guruska
 
PPTX
Formasi pengadaan-recruitment-seleksi
Bayu Rizky Aditya
 
PPTX
Orang miskin dilarang sakit
Bayu Rizky Aditya
 
PPTX
Konteks Dasar Sistem Pemerintahan Indonesia
Bayu Rizky Aditya
 
DOCX
اللائحة التنفيذية لمؤسسة شمس البوادي
SAB Group Shams Al Bawadi
 
PPT
Sistem politik indonesia
Bayu Rizky Aditya
 
PPT
Kunci dasar penulisan karya ilmiah
Bayu Rizky Aditya
 
PDF
Mediaset vivendi, bolloré va all’offensiva - il foglio
Carlo Favaretti
 
PDF
Raggi e il vertice farsa con beppe grillo. qualcuno chiami i carabinieri il...
Carlo Favaretti
 
Erasmus grants201112 presentationforbriefings
lancs-erasmus
 
Presentacion laura
lau_kaxon
 
queens, new york
lau_kaxon
 
SAB cristal 410 concentrate
SAB Group Shams Al Bawadi
 
Is Graduate School 4 U
dlofton
 
Infographics introduction
awhitfie
 
Tar fvg punto nascita h latisana
Carlo Favaretti
 
Stabilità 2017
Carlo Favaretti
 
Stick figure (vision)
bobelster
 
Sinopsis Pemutaran Film Khanduri Pilem
DiMas Diliasmara
 
Come distinguere l'innovazione dal nuovo? | sanità24 il sole 24 ore
Carlo Favaretti
 
GuiducciLorenzo_2010-2011_Esercizio4
guruska
 
Formasi pengadaan-recruitment-seleksi
Bayu Rizky Aditya
 
Orang miskin dilarang sakit
Bayu Rizky Aditya
 
Konteks Dasar Sistem Pemerintahan Indonesia
Bayu Rizky Aditya
 
اللائحة التنفيذية لمؤسسة شمس البوادي
SAB Group Shams Al Bawadi
 
Sistem politik indonesia
Bayu Rizky Aditya
 
Kunci dasar penulisan karya ilmiah
Bayu Rizky Aditya
 
Mediaset vivendi, bolloré va all’offensiva - il foglio
Carlo Favaretti
 
Raggi e il vertice farsa con beppe grillo. qualcuno chiami i carabinieri il...
Carlo Favaretti
 
Ad

Similar to Dot Call interface (20)

PDF
Native interfaces for R
Seth Falcon
 
PPTX
functions
Makwana Bhavesh
 
PPT
functions
teach4uin
 
PDF
An Analytics Toolkit Tour
Rory Winston
 
PDF
Rcpp: Seemless R and C++
Romain Francois
 
PPT
C plus plus
Tarun Nayak
 
PPT
27 c
Amit Dhiman
 
PDF
Los Angeles R users group - July 12 2011 - Part 2
rusersla
 
PPTX
About the C Libraries which are essential
KakashiiHatake3
 
PDF
Introduction to r bddsil meetup
Nimrod Priell
 
PDF
Rcpp: Seemless R and C++
Romain Francois
 
PDF
Rcpp: Seemless R and C++
Romain Francois
 
PPTX
UNIT3.pptx
NagasaiT
 
PPTX
C functions
University of Potsdam
 
PPTX
User defined functions
Rokonuzzaman Rony
 
PPTX
Lecture 4: Functions
Vivek Bhargav
 
PDF
Functions
Learn By Watch
 
PPTX
Built in function
MD. Rayhanul Islam Sayket
 
PDF
C standard library functions
Vaishnavee Sharma
 
PPT
Stdlib functions lesson
teach4uin
 
Native interfaces for R
Seth Falcon
 
functions
Makwana Bhavesh
 
functions
teach4uin
 
An Analytics Toolkit Tour
Rory Winston
 
Rcpp: Seemless R and C++
Romain Francois
 
C plus plus
Tarun Nayak
 
Los Angeles R users group - July 12 2011 - Part 2
rusersla
 
About the C Libraries which are essential
KakashiiHatake3
 
Introduction to r bddsil meetup
Nimrod Priell
 
Rcpp: Seemless R and C++
Romain Francois
 
Rcpp: Seemless R and C++
Romain Francois
 
UNIT3.pptx
NagasaiT
 
User defined functions
Rokonuzzaman Rony
 
Lecture 4: Functions
Vivek Bhargav
 
Functions
Learn By Watch
 
Built in function
MD. Rayhanul Islam Sayket
 
C standard library functions
Vaishnavee Sharma
 
Stdlib functions lesson
teach4uin
 
Ad

Recently uploaded (20)

PPTX
SAMPLING: DEFINITION,PROCESS,TYPES,SAMPLE SIZE, SAMPLING ERROR.pptx
PRADEEP ABOTHU
 
PPTX
Mrs Mhondiwa Introduction to Algebra class
sabinaschimanga
 
PPT
digestive system for Pharm d I year HAP
rekhapositivity
 
PPTX
Optimizing Cancer Screening With MCED Technologies: From Science to Practical...
i3 Health
 
PPTX
Modern analytical techniques used to characterize organic compounds. Birbhum ...
AyanHossain
 
PPTX
PPT on the Development of Education in the Victorian England
Beena E S
 
PPTX
ROLE OF ANTIOXIDANT IN EYE HEALTH MANAGEMENT.pptx
Subham Panja
 
PPTX
Blanket Order in Odoo 17 Purchase App - Odoo Slides
Celine George
 
PDF
BÀI TẬP BỔ TRỢ THEO LESSON TIẾNG ANH - I-LEARN SMART WORLD 7 - CẢ NĂM - CÓ ĐÁ...
Nguyen Thanh Tu Collection
 
PPTX
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
PPSX
Health Planning in india - Unit 03 - CHN 2 - GNM 3RD YEAR.ppsx
Priyanshu Anand
 
PPTX
Nutrition Month 2025 TARP.pptx presentation
FairyLouHernandezMej
 
PDF
IMP NAAC-Reforms-Stakeholder-Consultation-Presentation-on-Draft-Metrics-Unive...
BHARTIWADEKAR
 
PPTX
CONVULSIVE DISORDERS: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
PPTX
Presentation: Climate Citizenship Digital Education
Karl Donert
 
PPTX
Accounting Skills Paper-I, Preparation of Vouchers
Dr. Sushil Bansode
 
PPTX
Nutri-QUIZ-Bee-Elementary.pptx...................
ferdinandsanbuenaven
 
PPTX
Optimizing Cancer Screening With MCED Technologies: From Science to Practical...
i3 Health
 
PPTX
How to Configure Prepayments in Odoo 18 Sales
Celine George
 
SAMPLING: DEFINITION,PROCESS,TYPES,SAMPLE SIZE, SAMPLING ERROR.pptx
PRADEEP ABOTHU
 
Mrs Mhondiwa Introduction to Algebra class
sabinaschimanga
 
digestive system for Pharm d I year HAP
rekhapositivity
 
Optimizing Cancer Screening With MCED Technologies: From Science to Practical...
i3 Health
 
Modern analytical techniques used to characterize organic compounds. Birbhum ...
AyanHossain
 
PPT on the Development of Education in the Victorian England
Beena E S
 
ROLE OF ANTIOXIDANT IN EYE HEALTH MANAGEMENT.pptx
Subham Panja
 
Blanket Order in Odoo 17 Purchase App - Odoo Slides
Celine George
 
BÀI TẬP BỔ TRỢ THEO LESSON TIẾNG ANH - I-LEARN SMART WORLD 7 - CẢ NĂM - CÓ ĐÁ...
Nguyen Thanh Tu Collection
 
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
Health Planning in india - Unit 03 - CHN 2 - GNM 3RD YEAR.ppsx
Priyanshu Anand
 
Nutrition Month 2025 TARP.pptx presentation
FairyLouHernandezMej
 
IMP NAAC-Reforms-Stakeholder-Consultation-Presentation-on-Draft-Metrics-Unive...
BHARTIWADEKAR
 
CONVULSIVE DISORDERS: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
Presentation: Climate Citizenship Digital Education
Karl Donert
 
Accounting Skills Paper-I, Preparation of Vouchers
Dr. Sushil Bansode
 
Nutri-QUIZ-Bee-Elementary.pptx...................
ferdinandsanbuenaven
 
Optimizing Cancer Screening With MCED Technologies: From Science to Practical...
i3 Health
 
How to Configure Prepayments in Odoo 18 Sales
Celine George
 

Dot Call interface

  • 1. Introduction to .Call interface Hao Chai Department of Statistics and Actuarial Science The University of Iowa Productive Seminar March 6, 2012 Hao Chai (U of Iowa) Introduction to .Call interface 1 / 41
  • 2. Outline 1 Introduction 2 C part .C interface .Call interface 3 R part 4 Examples 5 Other Hao Chai (U of Iowa) Introduction to .Call interface 2 / 41
  • 3. Introduction Outline 1 Introduction 2 C part .C interface .Call interface 3 R part 4 Examples 5 Other Hao Chai (U of Iowa) Introduction to .Call interface 3 / 41
  • 4. Introduction .C and .Call are two interfaces in R to call C routines. Similar foreign language interface functions are .Fortran and .External. Hao Chai (U of Iowa) Introduction to .Call interface 4 / 41
  • 5. Introduction Advantages of R Straight forward syntax. Easy to learn, to use, to track the results and to debug. Over 3,000 packages available to carry out many kinds of statistical analysis and produce professional graphics. Vectorized language. ... Disadvantage of R Loops. Hao Chai (U of Iowa) Introduction to .Call interface 5 / 41
  • 6. Introduction Advantages of R Straight forward syntax. Easy to learn, to use, to track the results and to debug. Over 3,000 packages available to carry out many kinds of statistical analysis and produce professional graphics. Vectorized language. ... Disadvantage of R Loops. Hao Chai (U of Iowa) Introduction to .Call interface 5 / 41
  • 7. Introduction Toy example Our goal is to generate a variance-covariance matrix Σ = (σij ), where a · b|i−j| , if i = j; σij = 1, if i = j. We have at least three methods: A simple R function matgen R: matgen_R = function(P, a, b) { Covmatrix = matrix(rep(1, P * P), nrow = P) for (i in 1 : P) for (j in 1 : P) if (i != j) Covmatrix[i, j] = exp(log(a) + log(b) * abs(i - j)) Covmatrix } Hao Chai (U of Iowa) Introduction to .Call interface 6 / 41
  • 8. Introduction Toy example An R function using matrix indexing matgen Rind: matgen_Rind = function(P, a, b) { Covmatrix = matrix(rep(1, P * P), nrow = P) for (i in 1 : (P - 1)) {Covmatrix[cbind((1 + i) : P, 1 : (P - i))] = exp(log(a) + log(b) * abs(i)) Covmatrix[cbind(1 : (P - i), (1 + i) : P)] = exp(log(a) + log(b) * abs(i))} Covmatrix } Hao Chai (U of Iowa) Introduction to .Call interface 7 / 41
  • 9. Introduction An R function calling C routines matgen RC: matgen_RC = function(P, a, b) { .Call("matrix_gen", as.integer(P), as.double(a), as.double(b)) } Also we can use R compiler to compile the first two pure R functions and get matgen Rcmp and matgen Rindcmp. Hao Chai (U of Iowa) Introduction to .Call interface 8 / 41
  • 10. Introduction Toy example Result > P = 1000 test elapsed user.self relative 5 matgen Rindcmp(P, 0.5, 0.9) 0.80 0.75 1.00 4 matgen Rind(P, 0.5, 0.9) 0.97 0.86 1.21 1 matgen RC(P, 0.5, 0.9) 1.04 1.01 1.30 2 matgen Rcmp(P, 0.5, 0.9) 19.98 19.51 24.98 3 matgen R(P, 0.5, 0.9) 65.44 64.45 81.80 Hao Chai (U of Iowa) Introduction to .Call interface 9 / 41
  • 11. C part Outline 1 Introduction 2 C part .C interface .Call interface 3 R part 4 Examples 5 Other Hao Chai (U of Iowa) Introduction to .Call interface 10 / 41
  • 12. C part Header files To enable the communication between R and C, the following header files are needed in the C code. #include <R.h> #include <Rinternals.h> /% For .C or .Call %/ #include <Rdefines.h> /% For .External %/ #include <R_ext/Rdynload.h> Hao Chai (U of Iowa) Introduction to .Call interface 11 / 41
  • 13. C part .C interface To use .C interface, main() function in C code needs to be replaced by static void CFuncName(arg1, arg2, ...) Arguments above have to be of pointer type. Therefore, we need to coerce the scalar arguments to scalars in C. C function can only return values to R through its arguments. In other words, there are two kinds of arguments: arguments that pass values from R to C and arguments that return values from C to R. The C code using .C interface in the toy example is Hao Chai (U of Iowa) Introduction to .Call interface 12 / 41
  • 14. C part .C interface .C interface in toy example #include <R.h> #include <Rinternals.h> #include <R_ext/Rdynload.h> #include <math.h> static void matrix_gen(double *Rp, double *Ra, double *Rb, double *Rmatrix) { R_len_t i, j, p = Rp[0]; double a = Ra[0], b = Rb[0]; for (i = 0; i < p; i++) for (j = 0; j < p; j++) if (i == j) Rmatrix[i * p + j] = 1; else Rmatrix[i * p + j] = exp(log(a) + abs(i - j) * log(b)); } Hao Chai (U of Iowa) Introduction to .Call interface 13 / 41
  • 15. C part .C interface Mapping types between R and C Table: Comparison of R and C data types.1 R storage mode C types logical int * integer int * double double * character char ** complex Rcomplex * 1 Cited from “Writing R Extensions” Hao Chai (U of Iowa) Introduction to .Call interface 14 / 41
  • 16. C part .Call interface What can we expect from .Call?2 Pass R objects to C Create R objects in C Manipulate R objects in C Return R objects from C Call R functions from C 2 See Brian Caffo Hao Chai (U of Iowa) Introduction to .Call interface 15 / 41
  • 17. C part .Call interface To use .Call interface, main() function in C code needs to be replaced by SEXP CFuncName(SEXP arg1, SEXP arg2, ...) SEXP is a structure in C standing for S expression. The returned value from C will also be SEXP type. If new SEXP objects are defined within the C function, use PROTECT to protect them in the memory so that it will not be cleaned by R as garbage. Hao Chai (U of Iowa) Introduction to .Call interface 16 / 41
  • 18. C part .Call interface Basic structure of C functions SEXP CFuncName(SEXP arg1, arg2, ...) { [arg1 = coerceVector(arg1, INTSXP)] # Coerce the vector to the right type. SEXP Robj, return_val; # Define new R objects. PROTECT(return_val = allocMatrix(REALSXP, arg1, arg2)) # Protect return_val. [body of function] UNPROTECT(1) return(return_val) # Return the values to R. } Hao Chai (U of Iowa) Introduction to .Call interface 17 / 41
  • 19. C part .Call interface Useful data types in Rinternals.h 3 Table: SEXPTYPE and R equivalent SEXPTYPE R equivalent REALSXP numeric with storage mode double INTSXP integer LGLSXP logical STRSXP character VECSXP list (generic vector) DOTSXP a ‘...’ object ENVSXP environment 3 Cited from “Writing R Extensions” Hao Chai (U of Iowa) Introduction to .Call interface 18 / 41
  • 20. C part .Call interface Useful macros in Rinternals.h There are plenty of useful functions defined in Rinternals.h. Useful functions can be found in “Writing R Extensions” or “R Internals”. Table: List of useful functions in Rinternals.h4 Name Usage Example coerceVector Coerce R object to SEXPTYPE arg1=..(arg1,INTSXP) allocVector Allocate memory for R vector Robj=..(STRSXP,2) allocMatrix Allocate memory for R matrix Robj=..(STRSXP,2,2) nrows Get the No. of rows of a matrix ..nrow(Rmatrix) ncols Get the No. of cols of a matrix ..ncol(Rmatrix) PROTECT Protect an R object See examples. UNPROTECT UNPROTECT some R objects See examples. 4 .. represents the function name in the same row. Hao Chai (U of Iowa) Introduction to .Call interface 19 / 41
  • 21. C part .Call interface Useful functions in Rinternals.h Table: List of useful functions in Rinternals.h Cont.5 Name Usage Example STRING ELT Returns the memo. add. of R string ..(Rstr, 0) VECTOR ELT Returns the memo. add. of R list ..(Rlist, 1) INTEGER Return the memo. add. of R int ..(Rint) REAL Return the memo. add. of R real ..(Rdoub) CHAR Convert a Rchar to characters ..(Rchar) SET VECTOR ELT Assign value to an element of R list See example 5 .. represents the function name in the same row. Hao Chai (U of Iowa) Introduction to .Call interface 20 / 41
  • 22. C part .Call interface Special Values There are some constants for special values in R R NaN R PosInf R NegInf R NaReal R NilValue There are also some macros for error checking in C ISNA, ISNAN, R FINITE, NA INTEGER, NA LOGICAL, NA STRING, NA REAL Hao Chai (U of Iowa) Introduction to .Call interface 21 / 41
  • 23. C part .Call interface .Call interface in toy example #include <R.h> #include <Rinternals.h> #include <R_ext/Rdynload.h> #include "math.h" SEXP matrix_gen(SEXP RP, SEXP Ra, SEXP Rb) { R_len_t i, j, p = INTEGER(RP)[0]; SEXP Rmatrix; RP = coerceVector(RP, INTSXP); Ra = coerceVector(Ra, REALSXP); Rb = coerceVector(Rb, REALSXP); PROTECT(Rmatrix = allocMatrix(REALSXP, p, p)); Hao Chai (U of Iowa) Introduction to .Call interface 22 / 41
  • 24. C part .Call interface .Call interface in toy example double *mat, a = REAL(Ra)[0], b = REAL(Rb)[0]; mat = REAL(Rmatrix); for (i = 0; i < p; i++) for (j = 0; j < p; j++) if (i == j) mat[i * p + j] = 1; else mat[i * p + j] = exp(log(a) + abs(i - j) * log(b)); UNPROTECT(1); return(Rmatrix); } Hao Chai (U of Iowa) Introduction to .Call interface 23 / 41
  • 25. R part Outline 1 Introduction 2 C part .C interface .Call interface 3 R part 4 Examples 5 Other Hao Chai (U of Iowa) Introduction to .Call interface 24 / 41
  • 26. R part Interfaces in R To create a dynamic library, run the following code in the command line: R CMD SHLIB CFileName.c CFileName.dll or CFileName.so will be generated in the same directory. Then load the dynamic library which is generated by C code using > dyn.load("CFileName.so") in Linux/MacOS, or > dyn.load("CFileName.dll") in Windows, or a universal solution > dyn.load(paste("CFileName", .Platform$dynlib.ext, + sep = "")) Hao Chai (U of Iowa) Introduction to .Call interface 25 / 41
  • 27. R part There are three interfaces in R to load the dynamic library generated by C. 1 .C(”CFuncName” arg1, arg2, ...) , 2 .Call(”CFuncName” arg1, arg2, ...) , 3 .External(”CFuncName”, arg1, arg2, ...) There are some options when using .C, such as NAOK and PACKAGE. For detailed information, refer to “Writing R Extensions”. Hao Chai (U of Iowa) Introduction to .Call interface 26 / 41
  • 28. Examples Outline 1 Introduction 2 C part .C interface .Call interface 3 R part 4 Examples 5 Other Hao Chai (U of Iowa) Introduction to .Call interface 27 / 41
  • 29. Examples Pass numeric values to C func1 is a function with two arguments Rn and Rvec. It returns the largest element of the first Rn elements of vector Rvec. SEXP func1(SEXP Rvec, SEXP Rn) { Rvec = coerceVector(Rvec, REALSXP); R_len_t i = 0, n = INTEGER(Rn)[0]; double *vec, max = REAL(Rvec)[0]; SEXP return_val; PROTECT(return_val = allocVector(REALSXP, 1)); vec = REAL(Rvec); for (i = 1; i < n; i++) if (vec[i] >= max) max = vec[i]; REAL(return_val)[0] = max; UNPROTECT(1); return(return_val); } Hao Chai (U of Iowa) Introduction to .Call interface 28 / 41
  • 30. Examples Pass numeric values to C In R, we do the following a = c(7, 4, 8, 9, 2, 5) n = 4 dyn.load("f:/presentation/intro2dotCall/illust.dll") .Call("func1", as.double(a), as.integer(n)) Hao Chai (U of Iowa) Introduction to .Call interface 29 / 41
  • 31. Examples Pass strings, return a list func2 is able to calculate the quadratic form or the linear product of a matrix and a vector. It has three arguments: Rstr, Ry and RM. Ry is a vector, RM is a matrix. If Rstr is ”quadratic”, then t(Ry) %*% RM %*% Ry is returned. If Rstr is ”linear”, then RM %*% Ry is returned. If Rstr is ”both” then both of the above will be returned as an R list. , Hao Chai (U of Iowa) Introduction to .Call interface 30 / 41
  • 32. Examples Pass strings, return a list SEXP func2(SEXP RM, SEXP Ry, SEXP Rstr) { Rstr = coerceVector(Rstr, STRSXP); RM = coerceVector(RM, REALSXP); Ry = coerceVector(Ry, REALSXP); SEXP return_lst, vec, num; double *M = REAL(RM), *y = REAL(Ry); double result1 = 0, *result2; R_len_t i, j, nr, nc, l; const char *method; method = CHAR(STRING_ELT(Rstr, 0)); nr = nrows(RM); nc = ncols(RM); l = length(Ry); Hao Chai (U of Iowa) Introduction to .Call interface 31 / 41
  • 33. Examples if (((strcmp(method, "both") == 0) || (strcmp(method,"linear") == 0)) && (nc != l)) error("The matrix and the vector need to be conformable!"); if (((strcmp(method, "both") == 0) || (strcmp(method, "quadratic") == 0)) && ((nc != l) || (nr != l))) error("The matrix and the vector need to be conformable!"); if (strcmp(method, "both") == 0) { PROTECT(return_lst = allocVector(VECSXP, 2)); PROTECT(vec = allocVector(REALSXP, nr)); PROTECT(num = allocVector(REALSXP, 1)); result2 = REAL(vec); result1 = REAL(num)[0]; } else PROTECT(return_lst = allocVector(VECSXP, 1)); Hao Chai (U of Iowa) Introduction to .Call interface 32 / 41
  • 34. Examples if (strcmp(method, "linear") == 0) { PROTECT(vec = allocVector(REALSXP, nr)); result2 = REAL(vec); } if (strcmp(method, "quadratic") == 0) { PROTECT(num = allocVector(REALSXP, 1)); result1 = REAL(num)[0]; } if ((strcmp(method, "quadratic") == 0) || (strcmp(method, "both") == 0)) for (i = 0; i < nr; i++) for (j = 0; j < nc; j++) result1 = result1 + M[i + j * nr] * y[i] * y[j]; Hao Chai (U of Iowa) Introduction to .Call interface 33 / 41
  • 35. Examples if ((strcmp(method, "linear") == 0) || (strcmp(method, "both") == 0)) for (i = 0; i < nr; i++) { result2[i] = 0; for (j = 0; j < nc; j++) result2[i] = result2[i] + M[i + j * nr] * y[j]; } Hao Chai (U of Iowa) Introduction to .Call interface 34 / 41
  • 36. Examples if (strcmp(method, "quadratic") == 0) SET_VECTOR_ELT(return_lst, 0, num); if (strcmp(method, "linear") == 0) SET_VECTOR_ELT(return_lst, 0, vec); if (strcmp(method, "both") == 0) { SET_VECTOR_ELT(return_lst, 0, num); SET_VECTOR_ELT(return_lst, 1, vec); } UNPROTECT(2); if (strcmp(method, "both") == 0) UNPROTECT(1); return(return_lst); } Hao Chai (U of Iowa) Introduction to .Call interface 35 / 41
  • 37. Examples Pass strings, return a list The R code to call the function func2 is dyn.load("f:/presentation/intro2dotCall/illust.dll") M <- matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), nrow = 3) y <- c(1, 0, -1) .Call("func2", M, as.double(y), "both") Hao Chai (U of Iowa) Introduction to .Call interface 36 / 41
  • 38. Other Outline 1 Introduction 2 C part .C interface .Call interface 3 R part 4 Examples 5 Other Hao Chai (U of Iowa) Introduction to .Call interface 37 / 41
  • 39. Other Evaluating R expressions in C It is also possible to evaluate R expressions in C code. The following functions/macros are useful in this case. More details can be found in “Writing R extensions” . defineVar findVar findFun install Hao Chai (U of Iowa) Introduction to .Call interface 38 / 41
  • 40. Other Some misc. The “Writing R Extensions” manual is the main source for this talk. More functions and macros can be found in Rinternals.h, or alternatively Rdefines.h. Choose one and stick to it. Several times, when ran .Call in R, R crashed. Very likely, it was caused by the difference between number of arguments in .Call and number of arguments in the original C functions. Related C and R files are available at Related files. Hao Chai (U of Iowa) Introduction to .Call interface 39 / 41
  • 41. Other Refrences Writing R Extensions R Internals Using .Call in R by Brian Caffo. dot Call Interface by Gopi Goswami. Rinternals.h Hao Chai (U of Iowa) Introduction to .Call interface 40 / 41
  • 42. Other Thank you! Hao Chai (U of Iowa) Introduction to .Call interface 41 / 41