You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
R is an amazing interpreted language, giving a flexible and agile foundation for Data Science.
15
15
Efforts such as Rcpp and reticulate have established that it can be an advantage
16
16
to pair R with another programming language. Sometimes for speed, but most importantly
17
-
to have alternative options of expression.
17
+
to have alternative options of expression.
18
+
19
+
> Maybe mention that we sometimes want to use existing implementations?
18
20
19
21
[Go](https://golang.org) is an open source programming language that makes it easy to
20
22
build simple, reliable and efficient software. It is sometimes said to be the language
@@ -23,21 +25,53 @@ compatibility to C and a taste for complexity.
23
25
24
26
Go is beautiful and simple, its standard library is one of the most impressive
25
27
for a programming language. It comes with concurrency built in, which includes
26
-
(but is not limited to) running code in parallel. The static site generator [hugo](https://gohugo.io)
27
-
and the containerization plaform [docker](https://www.docker.com/) are examples
28
-
of systems that are built on Go.
28
+
(but is not limited to) running code in parallel. The static site generator [hugo](https://gohugo.io),
29
+
the containerization plaform [docker](https://www.docker.com/), and the profiling utility [pprof](https://github.com/google/pprof) are examples
30
+
of systems that are built with Go.
29
31
30
32
There currently is no end to end solution to easily connect R and Go, i.e. invoke Go code
31
33
from R, and this is what the `ergo` project is about, the ability for R packages to
32
-
leverage existing or original Go code. There admittedly are no
33
-
specific use cases in mind, but at the same time it would have been impossible
34
-
to imagine the importance of Rcpp when it was first developed.
35
-
36
-
Having Go as an alternative high performance language will open
34
+
leverage existing or original Go code. Having Go as an alternative high performance language will open
37
35
interesting avenues for R package development.
38
36
37
+
# Prior art
38
+
39
+
## Rcpp
40
+
41
+
The Rcpp package by Eddelbuettel and François is the current state of the practice to connect C++ code with R, used by over 1300 CRAN packages.
42
+
With Rcpp it is very easy to make a C++ function callable from R: mark the function with [attributes](https://cran.r-project.org/web/packages/Rcpp/vignettes/Rcpp-attributes.pdf), and Rcpp generates the glue code that takes care of converting input and output data and propagating errors.
43
+
Rcpp offers the `Vector` class, with specializations like `IntegerVector`, `CharacterVector`, and `List`, that allow accessing R data structures in a way idiomatic to C++.
44
+
Rcpp sugar supports writing C++ code that almost looks like R code but implements the operations internally without roundtripping to R.
45
+
46
+
## rmq
47
+
48
+
The rmq package (https://github.com/glycerine/rmq) by Jason E. Aten is scoped
49
+
as a proof of concept to embed a Go library in an R package.
50
+
The code uses msgpack, a serialization protocol, to pass data between R and Go,
51
+
and implements a client-server system using websockets.
52
+
According to the author, the project is finished.
53
+
Unfortunately, installation of this package requires tweaking the code, so far I
54
+
was unable to install the package and test the code on OS X and Ubuntu.
55
+
56
+
57
+
## r-go proof of concept
58
+
59
+
Independently of rmq, I have written four blog posts at
60
+
https://purrple.cat/tags/go/ that describe how to
61
+
embed a Go library in an R package so that it is compiled when
62
+
the R package is installed.
63
+
I show how to call functions in the library and how to pass data to and from Go.
64
+
Even though the code in the blog posts has been written manually, it is explicitely
65
+
divided in two different categories:
66
+
67
+
- Code that the user would write. This is typical Go code using Go data structures such
68
+
as Go strings and slices.
69
+
- Code that uses both `cgo` and `R` internal `C` apis. This code eventually is supposed to
70
+
be generated automatically at development time.
71
+
39
72
# The plan
40
73
74
+
The previous efforts to connect C++ and Go to R give me enough confidence about the feasability of the project.
41
75
As opposed to `Rcpp` which is a dependency in all stages (development, build, runtime),
42
76
`ergo` will only be *development time dependency* that facilitates the generation of
43
77
code to interface R and Go via their respective C apis. From the point of view of the user of `ergo`,
@@ -49,26 +83,26 @@ the workflow will be:
49
83
50
84
The role of `ergo` is to hide the C layer entirely, so that users can focus on writing Go and R code.
0 commit comments