SlideShare una empresa de Scribd logo
1 de 42
Descargar para leer sin conexión
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

Más contenido relacionado

La actualidad más candente

Chapter 6 intermediate code generation
Chapter 6   intermediate code generationChapter 6   intermediate code generation
Chapter 6 intermediate code generationVipul Naik
 
Intermediate code generation1
Intermediate code generation1Intermediate code generation1
Intermediate code generation1Shashwat Shriparv
 
Sentence-to-Code Traceability Recovery with Domain Ontologies
Sentence-to-Code Traceability Recovery with Domain OntologiesSentence-to-Code Traceability Recovery with Domain Ontologies
Sentence-to-Code Traceability Recovery with Domain OntologiesShinpei Hayashi
 
Chapter Eight(3)
Chapter Eight(3)Chapter Eight(3)
Chapter Eight(3)bolovv
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler DesignShine Raj
 
C Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpointC Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpointJavaTpoint.Com
 
Object-Oriented Programming in Modern C++. Borislav Stanimirov. CoreHard Spri...
Object-Oriented Programming in Modern C++. Borislav Stanimirov. CoreHard Spri...Object-Oriented Programming in Modern C++. Borislav Stanimirov. CoreHard Spri...
Object-Oriented Programming in Modern C++. Borislav Stanimirov. CoreHard Spri...corehard_by
 
Intermediate code- generation
Intermediate code- generationIntermediate code- generation
Intermediate code- generationrawan_z
 
Generating Assertion Code from OCL: A Transformational Approach Based on Simi...
Generating Assertion Code from OCL: A Transformational Approach Based on Simi...Generating Assertion Code from OCL: A Transformational Approach Based on Simi...
Generating Assertion Code from OCL: A Transformational Approach Based on Simi...Shinpei Hayashi
 
introduction of c langauge(I unit)
introduction of c langauge(I unit)introduction of c langauge(I unit)
introduction of c langauge(I unit)Prashant Sharma
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programminggajendra singh
 
C Programming Language Step by Step Part 1
C Programming Language Step by Step Part 1C Programming Language Step by Step Part 1
C Programming Language Step by Step Part 1Rumman Ansari
 
Top C Language Interview Questions and Answer
Top C Language Interview Questions and AnswerTop C Language Interview Questions and Answer
Top C Language Interview Questions and AnswerVineet Kumar Saini
 
Hands-on Introduction to the C Programming Language
Hands-on Introduction to the C Programming LanguageHands-on Introduction to the C Programming Language
Hands-on Introduction to the C Programming LanguageVincenzo De Florio
 

La actualidad más candente (20)

Chapter 6 intermediate code generation
Chapter 6   intermediate code generationChapter 6   intermediate code generation
Chapter 6 intermediate code generation
 
Back patching
Back patchingBack patching
Back patching
 
Intermediate code generation1
Intermediate code generation1Intermediate code generation1
Intermediate code generation1
 
Sentence-to-Code Traceability Recovery with Domain Ontologies
Sentence-to-Code Traceability Recovery with Domain OntologiesSentence-to-Code Traceability Recovery with Domain Ontologies
Sentence-to-Code Traceability Recovery with Domain Ontologies
 
Chapter Eight(3)
Chapter Eight(3)Chapter Eight(3)
Chapter Eight(3)
 
Interm codegen
Interm codegenInterm codegen
Interm codegen
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler Design
 
Cd2 [autosaved]
Cd2 [autosaved]Cd2 [autosaved]
Cd2 [autosaved]
 
C Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpointC Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpoint
 
Object-Oriented Programming in Modern C++. Borislav Stanimirov. CoreHard Spri...
Object-Oriented Programming in Modern C++. Borislav Stanimirov. CoreHard Spri...Object-Oriented Programming in Modern C++. Borislav Stanimirov. CoreHard Spri...
Object-Oriented Programming in Modern C++. Borislav Stanimirov. CoreHard Spri...
 
Intermediate code- generation
Intermediate code- generationIntermediate code- generation
Intermediate code- generation
 
C language
C languageC language
C language
 
Generating Assertion Code from OCL: A Transformational Approach Based on Simi...
Generating Assertion Code from OCL: A Transformational Approach Based on Simi...Generating Assertion Code from OCL: A Transformational Approach Based on Simi...
Generating Assertion Code from OCL: A Transformational Approach Based on Simi...
 
Compiler unit 4
Compiler unit 4Compiler unit 4
Compiler unit 4
 
introduction of c langauge(I unit)
introduction of c langauge(I unit)introduction of c langauge(I unit)
introduction of c langauge(I unit)
 
C programming
C programmingC programming
C programming
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
C Programming Language Step by Step Part 1
C Programming Language Step by Step Part 1C Programming Language Step by Step Part 1
C Programming Language Step by Step Part 1
 
Top C Language Interview Questions and Answer
Top C Language Interview Questions and AnswerTop C Language Interview Questions and Answer
Top C Language Interview Questions and Answer
 
Hands-on Introduction to the C Programming Language
Hands-on Introduction to the C Programming LanguageHands-on Introduction to the C Programming Language
Hands-on Introduction to the C Programming Language
 

Destacado

Erasmus grants201112 presentationforbriefings
Erasmus grants201112 presentationforbriefingsErasmus grants201112 presentationforbriefings
Erasmus grants201112 presentationforbriefingslancs-erasmus
 
Presentacion laura
Presentacion lauraPresentacion laura
Presentacion lauralau_kaxon
 
queens, new york
queens, new yorkqueens, new york
queens, new yorklau_kaxon
 
Is Graduate School 4 U
Is Graduate School 4 UIs Graduate School 4 U
Is Graduate School 4 Udlofton
 
Infographics introduction
Infographics introductionInfographics introduction
Infographics introductionawhitfie
 
Tar fvg punto nascita h latisana
Tar fvg punto nascita h latisanaTar fvg punto nascita h latisana
Tar fvg punto nascita h latisanaCarlo Favaretti
 
Stick figure (vision)
Stick figure (vision)Stick figure (vision)
Stick figure (vision)bobelster
 
Sinopsis Pemutaran Film Khanduri Pilem
Sinopsis Pemutaran Film Khanduri PilemSinopsis Pemutaran Film Khanduri Pilem
Sinopsis Pemutaran Film Khanduri PilemDiMas Diliasmara
 
Come distinguere l'innovazione dal nuovo? | sanità24 il sole 24 ore
Come distinguere l'innovazione dal nuovo? | sanità24   il sole 24 oreCome distinguere l'innovazione dal nuovo? | sanità24   il sole 24 ore
Come distinguere l'innovazione dal nuovo? | sanità24 il sole 24 oreCarlo Favaretti
 
GuiducciLorenzo_2010-2011_Esercizio4
GuiducciLorenzo_2010-2011_Esercizio4GuiducciLorenzo_2010-2011_Esercizio4
GuiducciLorenzo_2010-2011_Esercizio4guruska
 
Formasi pengadaan-recruitment-seleksi
Formasi pengadaan-recruitment-seleksiFormasi pengadaan-recruitment-seleksi
Formasi pengadaan-recruitment-seleksiBayu Rizky Aditya
 
Konteks Dasar Sistem Pemerintahan Indonesia
Konteks Dasar Sistem Pemerintahan IndonesiaKonteks Dasar Sistem Pemerintahan Indonesia
Konteks Dasar Sistem Pemerintahan IndonesiaBayu Rizky Aditya
 
اللائحة التنفيذية لمؤسسة شمس البوادي
اللائحة التنفيذية لمؤسسة شمس البوادياللائحة التنفيذية لمؤسسة شمس البوادي
اللائحة التنفيذية لمؤسسة شمس البواديSAB Group Shams Al Bawadi
 
Kunci dasar penulisan karya ilmiah
Kunci dasar penulisan karya ilmiahKunci dasar penulisan karya ilmiah
Kunci dasar penulisan karya ilmiahBayu Rizky Aditya
 
Mediaset vivendi, bolloré va all’offensiva - il foglio
Mediaset vivendi, bolloré va all’offensiva - il foglioMediaset vivendi, bolloré va all’offensiva - il foglio
Mediaset vivendi, bolloré va all’offensiva - il foglioCarlo Favaretti
 
Raggi e il vertice farsa con beppe grillo. qualcuno chiami i carabinieri il...
Raggi e il vertice farsa con beppe grillo. qualcuno chiami i carabinieri   il...Raggi e il vertice farsa con beppe grillo. qualcuno chiami i carabinieri   il...
Raggi e il vertice farsa con beppe grillo. qualcuno chiami i carabinieri il...Carlo Favaretti
 

Destacado (20)

Erasmus grants201112 presentationforbriefings
Erasmus grants201112 presentationforbriefingsErasmus grants201112 presentationforbriefings
Erasmus grants201112 presentationforbriefings
 
Presentacion laura
Presentacion lauraPresentacion laura
Presentacion laura
 
queens, new york
queens, new yorkqueens, new york
queens, new york
 
SAB cristal 410 concentrate
SAB cristal 410 concentrateSAB cristal 410 concentrate
SAB cristal 410 concentrate
 
Is Graduate School 4 U
Is Graduate School 4 UIs Graduate School 4 U
Is Graduate School 4 U
 
Infographics introduction
Infographics introductionInfographics introduction
Infographics introduction
 
Tar fvg punto nascita h latisana
Tar fvg punto nascita h latisanaTar fvg punto nascita h latisana
Tar fvg punto nascita h latisana
 
Stabilità 2017
Stabilità 2017Stabilità 2017
Stabilità 2017
 
Stick figure (vision)
Stick figure (vision)Stick figure (vision)
Stick figure (vision)
 
Sinopsis Pemutaran Film Khanduri Pilem
Sinopsis Pemutaran Film Khanduri PilemSinopsis Pemutaran Film Khanduri Pilem
Sinopsis Pemutaran Film Khanduri Pilem
 
Come distinguere l'innovazione dal nuovo? | sanità24 il sole 24 ore
Come distinguere l'innovazione dal nuovo? | sanità24   il sole 24 oreCome distinguere l'innovazione dal nuovo? | sanità24   il sole 24 ore
Come distinguere l'innovazione dal nuovo? | sanità24 il sole 24 ore
 
GuiducciLorenzo_2010-2011_Esercizio4
GuiducciLorenzo_2010-2011_Esercizio4GuiducciLorenzo_2010-2011_Esercizio4
GuiducciLorenzo_2010-2011_Esercizio4
 
Formasi pengadaan-recruitment-seleksi
Formasi pengadaan-recruitment-seleksiFormasi pengadaan-recruitment-seleksi
Formasi pengadaan-recruitment-seleksi
 
Orang miskin dilarang sakit
Orang miskin dilarang sakitOrang miskin dilarang sakit
Orang miskin dilarang sakit
 
Konteks Dasar Sistem Pemerintahan Indonesia
Konteks Dasar Sistem Pemerintahan IndonesiaKonteks Dasar Sistem Pemerintahan Indonesia
Konteks Dasar Sistem Pemerintahan Indonesia
 
اللائحة التنفيذية لمؤسسة شمس البوادي
اللائحة التنفيذية لمؤسسة شمس البوادياللائحة التنفيذية لمؤسسة شمس البوادي
اللائحة التنفيذية لمؤسسة شمس البوادي
 
Sistem politik indonesia
Sistem politik indonesiaSistem politik indonesia
Sistem politik indonesia
 
Kunci dasar penulisan karya ilmiah
Kunci dasar penulisan karya ilmiahKunci dasar penulisan karya ilmiah
Kunci dasar penulisan karya ilmiah
 
Mediaset vivendi, bolloré va all’offensiva - il foglio
Mediaset vivendi, bolloré va all’offensiva - il foglioMediaset vivendi, bolloré va all’offensiva - il foglio
Mediaset vivendi, bolloré va all’offensiva - il foglio
 
Raggi e il vertice farsa con beppe grillo. qualcuno chiami i carabinieri il...
Raggi e il vertice farsa con beppe grillo. qualcuno chiami i carabinieri   il...Raggi e il vertice farsa con beppe grillo. qualcuno chiami i carabinieri   il...
Raggi e il vertice farsa con beppe grillo. qualcuno chiami i carabinieri il...
 

Similar a Dot Call interface

Native interfaces for R
Native interfaces for RNative interfaces for R
Native interfaces for RSeth Falcon
 
Integrating R with C++: Rcpp, RInside and RProtoBuf
Integrating R with C++: Rcpp, RInside and RProtoBufIntegrating R with C++: Rcpp, RInside and RProtoBuf
Integrating R with C++: Rcpp, RInside and RProtoBufRomain Francois
 
R Programming Language
R Programming LanguageR Programming Language
R Programming LanguageNareshKarela1
 
Rcpp: Seemless R and C++
Rcpp: Seemless R and C++Rcpp: Seemless R and C++
Rcpp: Seemless R and C++Romain Francois
 
Best corporate-r-programming-training-in-mumbai
Best corporate-r-programming-training-in-mumbaiBest corporate-r-programming-training-in-mumbai
Best corporate-r-programming-training-in-mumbaiUnmesh Baile
 
Rcpp: Seemless R and C++
Rcpp: Seemless R and C++Rcpp: Seemless R and C++
Rcpp: Seemless R and C++Romain Francois
 
C_Programming_Language_tutorial__Autosaved_.pptx
C_Programming_Language_tutorial__Autosaved_.pptxC_Programming_Language_tutorial__Autosaved_.pptx
C_Programming_Language_tutorial__Autosaved_.pptxLikhil181
 
R basics for MBA Students[1].pptx
R basics for MBA Students[1].pptxR basics for MBA Students[1].pptx
R basics for MBA Students[1].pptxrajalakshmi5921
 
Compiler Design lab manual for Computer Engineering .pdf
Compiler Design lab manual for Computer Engineering .pdfCompiler Design lab manual for Computer Engineering .pdf
Compiler Design lab manual for Computer Engineering .pdfkalpana Manudhane
 
Advanced Web Technology ass.pdf
Advanced Web Technology ass.pdfAdvanced Web Technology ass.pdf
Advanced Web Technology ass.pdfsimenehanmut
 
1_Introduction.pptx
1_Introduction.pptx1_Introduction.pptx
1_Introduction.pptxranapoonam1
 
Reduce course notes class xii
Reduce course notes class xiiReduce course notes class xii
Reduce course notes class xiiSyed Zaid Irshad
 

Similar a Dot Call interface (20)

Native interfaces for R
Native interfaces for RNative interfaces for R
Native interfaces for R
 
Integrating R with C++: Rcpp, RInside and RProtoBuf
Integrating R with C++: Rcpp, RInside and RProtoBufIntegrating R with C++: Rcpp, RInside and RProtoBuf
Integrating R with C++: Rcpp, RInside and RProtoBuf
 
Inroduction to r
Inroduction to rInroduction to r
Inroduction to r
 
Introduction to R for beginners
Introduction to R for beginnersIntroduction to R for beginners
Introduction to R for beginners
 
DBMS CS3
DBMS CS3DBMS CS3
DBMS CS3
 
Programming Exam Help
 Programming Exam Help Programming Exam Help
Programming Exam Help
 
R Programming Language
R Programming LanguageR Programming Language
R Programming Language
 
Rcpp: Seemless R and C++
Rcpp: Seemless R and C++Rcpp: Seemless R and C++
Rcpp: Seemless R and C++
 
Best corporate-r-programming-training-in-mumbai
Best corporate-r-programming-training-in-mumbaiBest corporate-r-programming-training-in-mumbai
Best corporate-r-programming-training-in-mumbai
 
Lecture_R.ppt
Lecture_R.pptLecture_R.ppt
Lecture_R.ppt
 
Rcpp: Seemless R and C++
Rcpp: Seemless R and C++Rcpp: Seemless R and C++
Rcpp: Seemless R and C++
 
Lecture1
Lecture1Lecture1
Lecture1
 
C_Programming_Language_tutorial__Autosaved_.pptx
C_Programming_Language_tutorial__Autosaved_.pptxC_Programming_Language_tutorial__Autosaved_.pptx
C_Programming_Language_tutorial__Autosaved_.pptx
 
R basics for MBA Students[1].pptx
R basics for MBA Students[1].pptxR basics for MBA Students[1].pptx
R basics for MBA Students[1].pptx
 
3.5
3.53.5
3.5
 
R language
R languageR language
R language
 
Compiler Design lab manual for Computer Engineering .pdf
Compiler Design lab manual for Computer Engineering .pdfCompiler Design lab manual for Computer Engineering .pdf
Compiler Design lab manual for Computer Engineering .pdf
 
Advanced Web Technology ass.pdf
Advanced Web Technology ass.pdfAdvanced Web Technology ass.pdf
Advanced Web Technology ass.pdf
 
1_Introduction.pptx
1_Introduction.pptx1_Introduction.pptx
1_Introduction.pptx
 
Reduce course notes class xii
Reduce course notes class xiiReduce course notes class xii
Reduce course notes class xii
 

Último

Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...RKavithamani
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 

Último (20)

Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 

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