-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathREADME.Rmd
145 lines (100 loc) · 3.13 KB
/
README.Rmd
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
library(cstructr)
```
# cstructr
<!-- badges: start -->

[](https://github.com/coolbutuseless/cstructr/actions)
<!-- badges: end -->
`{cstructr}` is a demonstration package illustrating how a C struct may be
wrapped and manipulated from within R as an external pointer.
This is a "full-service" implementation which makes the internal values
of the struct accessible from R via getters and setters.
For bespoke solutions, a lot of the code could be excised to just keep
some core functions.
#### Note: The code in this package was auto-generated by parsing the C struct definition and the C library function declaration.
#### C Struct Definition
```{c eval=FALSE}
typedef struct {
int N;
double strength;
} MyCStruct;
```
#### C Library Function Declaration
```{c eval=FALSE}
double multiply(MyCStruct *mycstruct);
```
## Installation
You can install from [GitHub](https://github.com/coolbutuseless/cstructr) with:
``` r
# install.package('remotes')
remotes::install_github('coolbutuseless/cstructr')
```
## Create and Manipulate
```{r example}
library(cstructr)
mycstruct <- MyCStruct(N = 1, strength = 13)
mycstruct
class(mycstruct)
typeof(mycstruct)
mycstruct$N
mycstruct$strength
mycstruct$N <- 3
mycstruct
rm(mycstruct)
gc()
```
Create multiple structs at once
```{r}
lots <- MyCStructs(n = 3)
class(lots)
lots
```
## Call a C library function
1. Write a "bridge function" wrapper (in C) that maps from R objects to C objects
2. Call the C library function from this wrapper
3. Return the result to R in an appropriate container
An example of a C function in a library is as follows. C libraries know
nothing about R or SEXP objects.
```{c eval=FALSE}
double multiply(MyCStruct *mycstruct) {
return mycstruct->N * mycstruct->strength;
}
```
The "bridge" function between R and the C library call is written in C as follows.
This function unpacks the externalptr to a real `MyCStruct` pointer, calls the
C library function, and then returns the results as an R real numeric value.
```{c eval=FALSE}
SEXP multiply_(SEXP mycstruct_ptr_) {
MyCStruct *mycstruct = external_ptr_to_MyCStruct_ptr(mycstruct_ptr_);
double res = multiply(mycstruct);
return ScalarReal(res);
}
```
The call from R is:
```{r eval = FALSE}
multiply <- function(mycstruct) {
.Call(multiply_, mycstruct)
}
```
All these functions combine such that we can create a pointer to a C struct
in R, and then call the C library function as follows
```{r}
library(cstructr)
mycstruct <- MyCStruct(N=2, strength = 4.5)
multiply(mycstruct)
```
## Auto-generation of bridge function
A lot of the code to wrap a C function and make it callable from R is standard boilerplate.
For simple library functions, you could auto-generate a lot of this R + C code from the
C function declaration.