際際滷

際際滷Share a Scribd company logo
Extend R with C!!!
   2009/11/22 id:mickey24
id: mickey24 (@mickey24)
id: mickey24 (@mickey24)
Extend R with C!!!
? Tsukuba.R#4
 ?   R    Brainf*ck
? Tsukuba.R#4
 ?   R       Brainf*ck


? Tsukuba.R#5
 ?   Animation with R
Extend R with C!!!
C   R
?
?
C   R
C    R
?C       R
C        R
?C           R

?C   R               C
                 R
Motivation
Motivation
1.
Motivation
1.
     ?
Motivation
1.
     ?
     ?          C   R                     (40   50   )   www
         - Seeking for my unique color.
         http://d.hatena.ne.jp/syou6162/20090117/1232120983
Motivation
1.
     ?
     ?          C   R                     (40   50   )   www
         - Seeking for my unique color.
         http://d.hatena.ne.jp/syou6162/20090117/1232120983

2. R           C
#include <R.h>
#include <Rinternals.h>


SEXP test(SEXP exp) {
    ...
}
C     R

// integer
int n = INTEGER(exp)[0];


// string
PROTECT(exp = AS_CHARACTER(exp));
char* str = R_alloc(strlen(CHAR(
  STRING_ELT(exp, 0))), sizeof(char));
strcpy(str, CHAR(STRING_ELT(exp, 0)));
...
UNPROTECT(1)
C     R

// integer
SEXP exp_int;
PROTECT(exp_int = NEW_INTEGER(1));
int* pi = INTEGER_POINTER(exp_int);
pi[0] = 24;
...
UNPROTECT(1);
return exp_int;
Extend R with C!!!
$ R CMD SHLIB test.c
$ R CMD SHLIB test.c

?   test.so
$ R CMD SHLIB test.c

?   test.so

?   R               R
$ R CMD SHLIB test.c

?   test.so

?   R                  R

?   Make?le Makevars
R            C
dyn.load("test.so")

test <- function(n) {
  .Call("test", integer(n))
}

test(24)



    Writing R Extensions
    http://cran.r-project.org/doc/contrib/manuals-jp/R-exts.jp.pdf
?
?
Extend R with C!!!
?C   R
?C   R

?        C
         R
?C   R

?          C
           R

?        6162
OpenCV
OpenCV
?
OpenCV
?
? BSD
OpenCV
?
? BSD
? C C++   Python
OpenCV
?
? BSD
? C C++   Python


?
Extend R with C!!!
Extend R with C!!!
Extend R with C!!!
Extend R with C!!!
Extend R with C!!!
Extend R with C!!!
Extend R with C!!!
´
´
´




    ´
Extend R with C!!!
Extend R with C!!!
?
?
?C   R
?
?C   R


?
?   C   R                                - Seeking for my unique color.
    http://d.hatena.ne.jp/syou6162/20090117/1232125870


?   Writing R Extensions
    http://cran.r-project.org/doc/contrib/manuals-jp/R-exts.jp.pdf
Extend R with C!!!

More Related Content

Extend R with C!!!