-
Notifications
You must be signed in to change notification settings - Fork 1
/
talk.otl
58 lines (52 loc) · 1.39 KB
/
talk.otl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
CougarCS meeting 2014-10-23
R-extensions and embedding R in Perl
====
R's C API is documented at <http://cran.r-project.org/doc/manuals/r-release/R-exts.html>
Top of your C file
; #include <R.h>
; #include <Rinternals.h>
SEXP
s-expressions
like in Lisp
: (cdr (cdr (cdr '(A B C D))))
> (D)
R's design inspired by Scheme
hold all variable types
http://cran.r-project.org/doc/manuals/r-release/R-exts.html#Details-of-R-types
: SEXPTYPE R equivalent
:===================================================
: REALSXP numeric with storage mode double
: INTSXP integer
: CPLXSXP complex
: LGLSXP logical
: STRSXP character
: VECSXP list (generic vector)
: LISTSXP pairlist
: DOTSXP a ‘…’ object
: NILSXP NULL
: SYMSXP name/symbol
: CLOSXP function or function closure
: ENVSXP environment
refer to variables as SEXP
: SEXP convolve2(SEXP a, SEXP b) {
: /* ... */
: }
; ft=c
you can find out the type of a SEXP variable
: SEXP a;
:
: switch( TYPEOF(a) ) {
: case REALSEXP:
: /* double */
: case INTSEXP:
: /* integer */
: case STRSEXP:
: /* strings */
: }
Two ways of calling
.C("c_fn", arg1, arg2, arg3)
.External("ext_fn", arg1, arg2, arg3)
difference
.C passes in each SEXP argument separately
.External passes in all the arguments as a single SEXP variable
a list of all the arguments