2
2
title : Call Python from R through Rcpp
3
3
author : Wush Wu
4
4
license : GPL (>= 2)
5
- tags : featured boost
5
+ tags : featured boost python
6
6
summary : Integrate Python into R via Rcpp and Boost.Python
7
7
layout : post
8
8
src : 2014-04-06-rcpp-python.Rmd
9
9
---
10
10
11
11
12
12
13
-
14
13
## Introduction
15
14
16
15
This post provides a brief introduction to calling Python from R through Rcpp. The
@@ -49,7 +48,6 @@ py_ldflags <- system("python2.7-config --ldflags", intern=TRUE)
49
48
Sys.setenv("PKG_LIBS"=sprintf("%s %s %s", Sys.getenv("PKG_CFLAGS"), "-lboost_python-py27", py_ldflags))
50
49
{% endhighlight %}
51
50
52
-
53
51
The following ` hello world ` should then work:
54
52
55
53
@@ -81,9 +79,6 @@ void hello_python() {
81
79
82
80
83
81
84
-
85
-
86
-
87
82
Let's call them in R:
88
83
89
84
@@ -93,12 +88,10 @@ hello_python()
93
88
{% endhighlight %}
94
89
95
90
96
-
97
91
<pre class =" output " >
98
- Today is Sun Apr 6 09:03:02 2014
92
+ Today is Thu Apr 2 11:32:17 2015
99
93
</pre >
100
94
101
-
102
95
It shows that the ` hello_python ` function successfully initializes the Python
103
96
engine and runs the Python script through ` PyRun_SimpleString ` .
104
97
@@ -132,18 +125,16 @@ SEXP IntVec_to_py_list(IntegerVector src) {
132
125
{% endhighlight %}
133
126
134
127
135
-
136
128
{% highlight r %}
137
129
IntVec_to_py_list(1:10)
138
130
{% endhighlight %}
139
131
140
132
141
133
142
134
<pre class =" output " >
143
- < ; pointer: 0x19d1f80 > ;
135
+ < ; pointer: 0x8e0b410 > ;
144
136
</pre >
145
137
146
-
147
138
The pointer refers to the memory of the transformed Python object.
148
139
149
140
## Call Python Function
@@ -180,7 +171,6 @@ void pyfun(std::string fun_name, SEXP fun_argument) {
180
171
{% endhighlight %}
181
172
182
173
183
-
184
174
{% highlight r %}
185
175
pycall("
186
176
def print_list(src):
@@ -206,7 +196,6 @@ pyfun("print_list", a)
206
196
10
207
197
</pre >
208
198
209
-
210
199
## Error Handling
211
200
212
201
Errors in the Python engine can be handled easily by the C++ ` try/catch `
@@ -240,7 +229,6 @@ void pyfun(std::string fun_name, SEXP fun_argument) {
240
229
{% endhighlight %}
241
230
242
231
243
-
244
232
{% highlight r %}
245
233
pycall("
246
234
def print_list(src):
@@ -257,21 +245,20 @@ pyfun("print_lists", a) # a typo of the function name
257
245
KeyError: 'print_lists'
258
246
Error in sys.excepthook:
259
247
Traceback (most recent call last):
260
- File " ; /usr/lib/python2.7/dist-packages/apport_python_hook.py" ; , line 64 , in apport_excepthook
248
+ File " ; /usr/lib/python2.7/dist-packages/apport_python_hook.py" ; , line 63 , in apport_excepthook
261
249
from apport.fileutils import likely_packaged, get_recent_crashes
262
250
File " ; /usr/lib/python2.7/dist-packages/apport/__init__.py" ; , line 5, in < ; module> ;
263
251
from apport.report import Report
264
252
File " ; /usr/lib/python2.7/dist-packages/apport/report.py" ; , line 16, in < ; module> ;
265
253
from xml.parsers.expat import ExpatError
266
254
File " ; /usr/lib/python2.7/xml/parsers/expat.py" ; , line 4, in < ; module> ;
267
255
from pyexpat import *
268
- ImportError: /usr/lib/python2.7/lib-dynload/pyexpat.x86_64 -linux-gnu.so: undefined symbol: _Py_ZeroStruct
256
+ ImportError: /usr/lib/python2.7/lib-dynload/pyexpat.i386 -linux-gnu.so: undefined symbol: _Py_ZeroStruct
269
257
270
258
Original exception was:
271
259
KeyError: 'print_lists'
272
260
</pre >
273
261
274
-
275
262
## Summary
276
263
277
264
These examples show how to integrate Python and R with Rcpp and Boost.Python.
0 commit comments